From fef67a02c91b8289dd8818986d784fd9a66f34f3 Mon Sep 17 00:00:00 2001 From: lili <1307862086@qq.com> Date: Sun, 31 Aug 2025 00:03:00 +0800 Subject: [PATCH] Init crash --- .gitignore | 1 + CMakeLists.txt | 38 +++++++++++++++++++++++++ motif_app.cpp | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++ qt_dialog.cpp | 13 +++++++++ qt_dialog.h | 8 ++++++ 5 files changed, 137 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 motif_app.cpp create mode 100644 qt_dialog.cpp create mode 100644 qt_dialog.h diff --git a/.gitignore b/.gitignore index a61a7ea..e49fcaf 100644 --- a/.gitignore +++ b/.gitignore @@ -67,4 +67,5 @@ compile_commands.json CTestTestfile.cmake _deps CMakeUserPresets.json +/build diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..69a3c14 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,38 @@ +cmake_minimum_required(VERSION 3.10) +project(MotifQtApp LANGUAGES CXX) + +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +set(CMAKE_AUTOUIC ON) +set(CMAKE_AUTOMOC ON) +set(CMAKE_AUTORCC ON) + +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +find_package(Qt5 COMPONENTS Widgets REQUIRED) + +# Motif +find_package(X11 REQUIRED) +find_library(MOTIF_LIB Xm) + +message(STATUS + "MOTIF_LIBRARIES = ${MOTIF_LIBRARIES}\n" + "X11_LIBRARIES = ${X11_LIBRARIES}\n" + "X11_Xt_LIB = ${X11_Xt_LIB}" +) + +# 源文件 +set(SOURCES + motif_app.cpp + qt_dialog.cpp + qt_dialog.h +) + +# 可执行文件 +add_executable(MotifQtApp ${SOURCES}) + +# 链接库 +target_link_libraries(MotifQtApp PRIVATE Qt5::Widgets ${MOTIF_LIB} X11::X11 ${X11_Xt_LIB}) + +target_include_directories(MotifQtApp PRIVATE ${X11_INCLUDE_DIR}) diff --git a/motif_app.cpp b/motif_app.cpp new file mode 100644 index 0000000..e16aca3 --- /dev/null +++ b/motif_app.cpp @@ -0,0 +1,77 @@ +#include +#include +#include + +#include +#include + +#include "qt_dialog.h" + +#include +#include +#include + +// 全局 Qt 对话框指针 +QtDialog *g_dialog = nullptr; + +// 每次 Motif 回调中处理 Qt 事件 +void processQtEvents() +{ + auto app = QApplication::instance(); + if (app) + { + app->processEvents(); // 处理 Qt 事件,不阻塞 + } +} + +// 回调函数:点击按钮弹出 Qt Dialog +void openQtDialog(Widget w, XtPointer client_data, XtPointer call_data) +{ + auto app = QApplication::instance(); + int argc = 0; + + if (!app) + { + app = new QApplication(argc, nullptr); + } + + if (!g_dialog) + { + g_dialog = new QtDialog(); + } + + g_dialog->show(); // 非阻塞显示 +} + +int main(int argc, char **argv) +{ + XtAppContext app_context; + Widget toplevel, button; + + toplevel = XtVaAppInitialize(&app_context, "MotifQtApp", NULL, 0, + &argc, argv, NULL, NULL); + + button = XtVaCreateManagedWidget("Open Qt Dialog", + xmPushButtonWidgetClass, + toplevel, NULL); + + XtAddCallback(button, XmNactivateCallback, openQtDialog, NULL); + + XtRealizeWidget(toplevel); + + // Motif 主循环中周期性处理 Qt 事件 + while (1) + { + while (XtAppPending(app_context)) + { + XEvent event; + XtAppNextEvent(app_context, &event); + XtDispatchEvent(&event); + } + + processQtEvents(); // 处理 Qt 事件 + usleep(1000); // 避免 CPU 空转 + } + + return 0; +} diff --git a/qt_dialog.cpp b/qt_dialog.cpp new file mode 100644 index 0000000..4364daf --- /dev/null +++ b/qt_dialog.cpp @@ -0,0 +1,13 @@ +#include "qt_dialog.h" +#include +#include + +QtDialog::QtDialog(QWidget* parent) + : QDialog(parent) +{ + setWindowTitle("Non-blocking Qt Dialog"); + QVBoxLayout* layout = new QVBoxLayout(this); + QLabel* label = new QLabel("Hello from Qt (non-blocking)!", this); + layout->addWidget(label); + setLayout(layout); +} diff --git a/qt_dialog.h b/qt_dialog.h new file mode 100644 index 0000000..5913f25 --- /dev/null +++ b/qt_dialog.h @@ -0,0 +1,8 @@ +#pragma once +#include + +class QtDialog : public QDialog { + Q_OBJECT +public: + QtDialog(QWidget* parent = nullptr); +};