main.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #include "mainwindow.h"
  2. #include <QApplication>
  3. #include <QWindow>
  4. int main(int argc, char *argv[])
  5. {
  6. if(argc == 3)
  7. {
  8. QApplication a(argc, argv);
  9. QString strcid = argv[2];
  10. MainWindow w(strcid);
  11. WId wid = WId(QString(argv[1]).toInt());//通过参数列表获取父进程窗口的WinId
  12. if(wid == 0)
  13. {
  14. w.show();
  15. }
  16. else
  17. {
  18. QWindow *window = QWindow::fromWinId(wid);//获取父进程窗口
  19. if(window == NULL)
  20. {
  21. w.show();
  22. }
  23. else
  24. {
  25. w.setProperty("_q_embedded_native_parent_handle", QVariant(wid));//设置属性,这句是必须的
  26. w.winId();//必须调用一次,生成winId
  27. w.windowHandle()->setParent(window);//设置父窗口
  28. w.show();//最后调用show,提前调用qt会为其生成窗口控件,这样就会和你原本想要嵌入进的父进程界面产生冲突
  29. fprintf(stderr, "%lld", w.winId());//写入标准错误输出,stderr能立即输出,stdout则不行
  30. }
  31. }
  32. return a.exec();
  33. }
  34. QApplication a(argc, argv);
  35. MainWindow w("");
  36. w.show();
  37. return a.exec();
  38. }