如何在自定义Windows桌面(HDESK)上显示QDialog
解决方案
在几次测试和社区的良好潜在客户之后,我现在可以确认必须在创建qapplication之前应用
。这是允许QT在新桌面中显示Qwidgets的唯一方法。setThreadDesktop
但是,switchdesktop
可以在qapplication之前或之后调用,只要在创建qapplication之前已调用setThreadDesktop
,就没关系。
感谢大家!我希望这个话题对他人有用。
问题
我正在使用Windows上的QT项目,我需要使用API来创建自定义桌面( hdesk ),例如 createSktop , opendesktop and switchdesktop 。在这个新桌面上,我需要显示一个qdialog(使用 exec 或 show 取决于我的需求),但是qdialog并未在新桌面上显示,而是在“默认”上显示“ 一。
我的代码看起来像这样:
/* Starting the QApplication from the main function */
QApplication qApp;
qApp.exec();
/* [...] An HEVENT is fired or a Win32 COM call is made telling me than i need to display the QDialog */
/* Creating the new desktop and switching to it */
HDESK hNewDesktop=NULL;
hNewDesktop=CreateDesktop(L"_MyNewDesktopName", NULL, NULL, DF_ALLOWOTHERACCOUNTHOOK, GENERIC_ALL, NULL);
SwitchDesktop(hNewDesktop);
/* Creating the QDialog and showing it */
QDialog *pqdlgMyDialog=NULL;
pqdlgMyDialog=new QDialog(NULL);
pqdlgMyDialog.show(); // or .exec() depending on the my needs
这样做,创建并显示带有黑色背景的新桌面,但qdialog显示在默认桌面上(我们启动Windows时看到的一个)。
我必须尝试将父母设置为我的Qdialog做这样的事情,但也行不通:
QWindow *pqwParentWindow=NULL;
QWidget *pwqParentWidget=NULL;
HWND hwndParentWindow=NULL;
hwndParentWindow=GetTopWindow(NULL); // I have also tried GetDesktopWindow, FindWindowEx etc.
pqwParentWindow=QWindow::fromWinId((WId)hwndParentWindow);
pwqParentWidget=QWidget::createWindowContainer(pqwParentWindow);
[...]
QDialog *pqdlgMyDialog=NULL;
pqdlgMyDialog=new QDialog(pwqParentWidget);
pqdlgMyDialog.show(); // or .exec() depending on the my needs
如果有人有想法,我愿意尝试一切! 我已经阅读了QT文档和MSDN的大量阅读,寻找诸如“ Qapplication链接到桌面”之类的东西,但没有成功...
Solution
After several tests and good leads from the community, I can now confirm that it is mandatory to apply SetThreadDesktop
before creating the QApplication
. This is the only way to allow Qt to display QWidgets in the new desktop.
However, SwitchDesktop
can be called before or after the QApplication, it doesn't matter as long as SetThreadDesktop
has been called before the creation of the QApplication.
Thanks to all of you! I hope this topic will be useful for other people.
Problem
I'm working on a Qt project on Windows where i need to create a custom desktop (HDESK) using API such as CreateDesktop, OpenDesktop and SwitchDesktop. On this new Desktop, i need to show a QDialog (using exec or show depending on my needs) but the QDialog is not displayed on the new desktop but on the "default" one.
My code looks something like this :
/* Starting the QApplication from the main function */
QApplication qApp;
qApp.exec();
/* [...] An HEVENT is fired or a Win32 COM call is made telling me than i need to display the QDialog */
/* Creating the new desktop and switching to it */
HDESK hNewDesktop=NULL;
hNewDesktop=CreateDesktop(L"_MyNewDesktopName", NULL, NULL, DF_ALLOWOTHERACCOUNTHOOK, GENERIC_ALL, NULL);
SwitchDesktop(hNewDesktop);
/* Creating the QDialog and showing it */
QDialog *pqdlgMyDialog=NULL;
pqdlgMyDialog=new QDialog(NULL);
pqdlgMyDialog.show(); // or .exec() depending on the my needs
Doing this, create and display a new desktop with a black background but the QDialog is displayed on the default desktop (the one we see when we start Windows).
I have to tried to set a parent to my QDialog doing something like this but it does not work either :
QWindow *pqwParentWindow=NULL;
QWidget *pwqParentWidget=NULL;
HWND hwndParentWindow=NULL;
hwndParentWindow=GetTopWindow(NULL); // I have also tried GetDesktopWindow, FindWindowEx etc.
pqwParentWindow=QWindow::fromWinId((WId)hwndParentWindow);
pwqParentWidget=QWidget::createWindowContainer(pqwParentWindow);
[...]
QDialog *pqdlgMyDialog=NULL;
pqdlgMyDialog=new QDialog(pwqParentWidget);
pqdlgMyDialog.show(); // or .exec() depending on the my needs
If someone has an idea, i'm willing to try everything !
I have done a lot of reading of Qt Documentation and MSDN, looking for stuff such as "Is a QApplication linked to a desktop" but without success...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
HWND与螺纹绑定,带有HWND的线程与台式机绑在一起。
我不知道Qapplication是否会创建一个隐藏的窗口,或者您是否在那里有另一个HWND,但是很容易证明
SetThreadDesktop
如果线程上已经有一个窗口:HWNDs are tied to threads and threads with HWNDs are tied to desktops.
I don't know if QApplication creates a hidden window or if you have another HWND there but it is easy to demonstrate that
SetThreadDesktop
will fail if there is already a window on the thread: