Files
motif-qt/motif_app.cpp
2025-08-31 15:16:25 +08:00

104 lines
2.8 KiB
C++
Raw Permalink 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 <QX11Info>
#include "qt_dialog.h"
#include <xcb/xcb.h>
// Qt 头文件必须在 Motif/X11 之前
#include <X11/Intrinsic.h>
#include <Xm/Xm.h>
#include <Xm/PushB.h>
#include <unistd.h> // for usleep
#include <iostream>
// 全局 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);
// int screen_num;
// xcb_connection_t *connection = xcb_connect(NULL, &screen_num);
xcb_connection_t *connection = QX11Info::connection();
// Qt 内部管理的 screen_num
int screen_num = QX11Info::appScreen();
if (xcb_connection_has_error(connection))
{
fprintf(stderr, "无法连接到 X 服务器\n");
return -1;
}
const xcb_setup_t *setup = xcb_get_setup(connection);
xcb_screen_iterator_t iter = xcb_setup_roots_iterator(setup);
for (int i = 0; i <= screen_num; i++)
{
xcb_screen_t *screen = iter.data;
xcb_screen_next(&iter);
printf("屏幕根窗口: %u, 尺寸: %dx%d\n",
screen->root, screen->width_in_pixels, screen->height_in_pixels);
std::cout << "screen_num: " << screen_num << std::endl;
std::cout << "screen root window: " << screen->root << std::endl;
std::cout << "screen size: " << screen->width_in_pixels
<< "x" << screen->height_in_pixels << std::endl;
}
// 创建 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;
// xcb_disconnect(connection);
return 0;
}