1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- #include "mainwindow.h"
- #include <QApplication>
- #include <QWindow>
- int main(int argc, char *argv[])
- {
- if(argc == 3)
- {
- QApplication a(argc, argv);
- QString strcid = argv[2];
- MainWindow w(strcid);
- WId wid = WId(QString(argv[1]).toInt());//通过参数列表获取父进程窗口的WinId
- if(wid == 0)
- {
- w.show();
- }
- else
- {
- QWindow *window = QWindow::fromWinId(wid);//获取父进程窗口
- if(window == NULL)
- {
- w.show();
- }
- else
- {
- w.setProperty("_q_embedded_native_parent_handle", QVariant(wid));//设置属性,这句是必须的
- w.winId();//必须调用一次,生成winId
- w.windowHandle()->setParent(window);//设置父窗口
- w.show();//最后调用show,提前调用qt会为其生成窗口控件,这样就会和你原本想要嵌入进的父进程界面产生冲突
- fprintf(stderr, "%lld", w.winId());//写入标准错误输出,stderr能立即输出,stdout则不行
- }
- }
- return a.exec();
- }
- QApplication a(argc, argv);
- MainWindow w("");
- w.show();
- return a.exec();
- }
|