Files
motif-qt/motif_app.cpp
2025-08-31 00:12:26 +08:00

71 lines
1.6 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <QApplication>
#include <QTimer>
#include "qt_dialog.h"
// Qt 头文件必须在 Motif/X11 之前
#include <X11/Intrinsic.h>
#include <Xm/Xm.h>
#include <Xm/PushB.h>
#include <unistd.h> // for usleep
// 全局 Qt 应用和对话框指针
QApplication *g_app = nullptr;
// 打开 Qt 对话框
void openQtDialog(Widget w, XtPointer client_data, XtPointer call_data)
{
auto dialog = new QtDialog();
dialog->setAttribute(Qt::WA_DeleteOnClose); // 关闭时自动删除
dialog->show(); // 非阻塞显示
}
// Motif 主循环中处理 Qt 事件
void processQtEvents()
{
if (g_app)
{
g_app->processEvents();
}
}
int main(int argc, char **argv)
{
XtAppContext app_context;
Widget toplevel, button;
// 创建 QApplication必须在 Qt 头文件之后)
g_app = new QApplication(argc, nullptr);
// 创建 Motif 主窗口
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);
XEvent event;
while (1)
{
while (XtAppPending(app_context))
{
XtAppNextEvent(app_context, &event);
XtDispatchEvent(&event);
}
// 每次空闲处理 Qt 事件
processQtEvents();
// 避免 CPU 空转
usleep(1000);
}
// 清理(一般不会执行到这里)
delete g_app;
return 0;
}