can run
This commit is contained in:
@@ -1,46 +1,31 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
#include <QApplication>
|
#include <QApplication>
|
||||||
#include <QTimer>
|
#include <QTimer>
|
||||||
|
|
||||||
#include "qt_dialog.h"
|
#include "qt_dialog.h"
|
||||||
|
|
||||||
|
// Qt 头文件必须在 Motif/X11 之前
|
||||||
|
#include <X11/Intrinsic.h>
|
||||||
#include <Xm/Xm.h>
|
#include <Xm/Xm.h>
|
||||||
#include <Xm/PushB.h>
|
#include <Xm/PushB.h>
|
||||||
#include <X11/Intrinsic.h>
|
#include <unistd.h> // for usleep
|
||||||
|
|
||||||
// 全局 Qt 对话框指针
|
// 全局 Qt 应用和对话框指针
|
||||||
QtDialog *g_dialog = nullptr;
|
QApplication *g_app = nullptr;
|
||||||
|
|
||||||
// 每次 Motif 回调中处理 Qt 事件
|
// 打开 Qt 对话框
|
||||||
void processQtEvents()
|
|
||||||
{
|
|
||||||
auto app = QApplication::instance();
|
|
||||||
if (app)
|
|
||||||
{
|
|
||||||
app->processEvents(); // 处理 Qt 事件,不阻塞
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 回调函数:点击按钮弹出 Qt Dialog
|
|
||||||
void openQtDialog(Widget w, XtPointer client_data, XtPointer call_data)
|
void openQtDialog(Widget w, XtPointer client_data, XtPointer call_data)
|
||||||
{
|
{
|
||||||
auto app = QApplication::instance();
|
auto dialog = new QtDialog();
|
||||||
int argc = 0;
|
dialog->setAttribute(Qt::WA_DeleteOnClose); // 关闭时自动删除
|
||||||
|
dialog->show(); // 非阻塞显示
|
||||||
|
}
|
||||||
|
|
||||||
if (!app)
|
// Motif 主循环中处理 Qt 事件
|
||||||
|
void processQtEvents()
|
||||||
|
{
|
||||||
|
if (g_app)
|
||||||
{
|
{
|
||||||
app = new QApplication(argc, nullptr);
|
g_app->processEvents();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!g_dialog)
|
|
||||||
{
|
|
||||||
g_dialog = new QtDialog();
|
|
||||||
}
|
|
||||||
|
|
||||||
g_dialog->show(); // 非阻塞显示
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
@@ -48,30 +33,38 @@ int main(int argc, char **argv)
|
|||||||
XtAppContext app_context;
|
XtAppContext app_context;
|
||||||
Widget toplevel, button;
|
Widget toplevel, button;
|
||||||
|
|
||||||
|
// 创建 QApplication(必须在 Qt 头文件之后)
|
||||||
|
g_app = new QApplication(argc, nullptr);
|
||||||
|
|
||||||
|
// 创建 Motif 主窗口
|
||||||
toplevel = XtVaAppInitialize(&app_context, "MotifQtApp", NULL, 0,
|
toplevel = XtVaAppInitialize(&app_context, "MotifQtApp", NULL, 0,
|
||||||
&argc, argv, NULL, NULL);
|
&argc, argv, NULL, NULL);
|
||||||
|
|
||||||
button = XtVaCreateManagedWidget("Open Qt Dialog",
|
button = XtVaCreateManagedWidget("Open Qt Dialog",
|
||||||
xmPushButtonWidgetClass,
|
xmPushButtonWidgetClass,
|
||||||
toplevel, NULL);
|
toplevel, NULL);
|
||||||
|
|
||||||
XtAddCallback(button, XmNactivateCallback, openQtDialog, NULL);
|
XtAddCallback(button, XmNactivateCallback, openQtDialog, NULL);
|
||||||
|
|
||||||
XtRealizeWidget(toplevel);
|
XtRealizeWidget(toplevel);
|
||||||
|
|
||||||
// Motif 主循环中周期性处理 Qt 事件
|
XEvent event;
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
while (XtAppPending(app_context))
|
while (XtAppPending(app_context))
|
||||||
{
|
{
|
||||||
XEvent event;
|
|
||||||
XtAppNextEvent(app_context, &event);
|
XtAppNextEvent(app_context, &event);
|
||||||
XtDispatchEvent(&event);
|
XtDispatchEvent(&event);
|
||||||
}
|
}
|
||||||
|
|
||||||
processQtEvents(); // 处理 Qt 事件
|
// 每次空闲处理 Qt 事件
|
||||||
usleep(1000); // 避免 CPU 空转
|
processQtEvents();
|
||||||
|
|
||||||
|
// 避免 CPU 空转
|
||||||
|
usleep(1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 清理(一般不会执行到这里)
|
||||||
|
delete g_app;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user