(47条消息) Qt之如何获取主窗口的指针_草上爬的博客-CSDN博客_qt获取mainwindow窗口的指针
在插件开发时,插件的对话框需要设置parent为应用的主界面。
否则在任务栏切换时操作系统会将对话框与应用主界面放到 平等的位置上,形成一个新的独立窗。
需要注意的是:
qApp->topLevelWidgwt() 获得所有的顶级窗口。都是Qwidget
qApp->topLevelWindow ()获得所有的QWindow
看起来用qApp->topLevelWindow()更为合适。但其实QMainWindow ,QDilaog 等所有用于界面显示的 都继承自QWidget.实际Qt 不会直接使用QWindow。
static getMainWindow()
{
static QMainWindow * mainwindow=nullptr;
if(mainwindow)
return mainwindow;
for(const auto& it:qApp->topLevelWidget())
{
mainwindow=qobject_cast<QMainWindow *>(it);
if(mainwindow)
return mainwindow;
}
return nullptr;
}