c++ - 当我按 alt-tab 键进入其中任何一个窗口时,如何将所有窗口强制置于最前面?

标签 c++ qt

我正在使用 C++ 在 QT5 中创建一个 GUI 应用程序。我想这样做,假设所有的窗口都不在最前面(比如,我已经 alt-tab 到不同的应用程序或者它们都被最小化了),如果我 alt-tab 进入几个窗口中的任何一个我的应用程序的窗口,那么它们都应该脱颖而出并且可见。

最佳答案

关键术语是“ transient 窗口”。我在维基百科上找到了这个(对我来说合适的)解释 Transient (computer programming)

In the X Window System, a window is said to be transient for another window if it belongs to that other window and may not outlast it: a dialog box, such as an alert message, is a common example.

恕我直言,这不仅限于 X11。

在 Qt 中,如果对话框的父窗口设置为另一个窗口,则该对话框会变为 transient 。

我做了一个小样本:

#include <QtWidgets>

int main(int argc, char **argv)
{
  qDebug() << "Qt Version: " << QT_VERSION_STR;
  // main application
  QApplication app(argc, argv);
  // setup GUI
  QWidget qWin;
  QVBoxLayout qVBoxWin;
  QLabel qLblWin(QString::fromUtf8("Main Window (QWidget)"));
  qVBoxWin.addWidget(&qLblWin, 1);
  qWin.setLayout(&qVBoxWin);
  qWin.show();
#define MAKE_DIALOG(I) \
  QDialog qDlg##I; \
  qDlg##I.setWindowTitle(QString::fromUtf8("Dialog"#I)); \
  QVBoxLayout qVBoxDlg##I; \
  QLabel qLblDlg##I(QString::fromUtf8("Dialog"#I" (QDialog)")); \
  qVBoxDlg##I.addWidget(&qLblDlg##I, 1); \
  qDlg##I.setLayout(&qVBoxDlg##I)
  // dialog 1 without parent
  MAKE_DIALOG(1);
  // dialog 2 with parent
  MAKE_DIALOG(2);
  qDlg2.setParent(&qWin, Qt::Dialog);
#undef MAKE_DIALOG
  qWin.show();
  qDlg1.show();
  qDlg2.show();
  // run-time loop
  return app.exec();
}

在 Windows 10(64 位)上使用 VS2013 和 Qt 5.9.2 编译和测试:

snapshot of testQMainWin-Dlgs

该应用程序由一个主窗口 (qWin) 和两个对话窗口 (qDlg1qDlg2) 组成。

appearance of testQMainWin-Dlgs in the Windows 10 taskbar

对话框 1(qDlg1 没有父窗口小部件)在 Windows 10 任务栏中显示为单独的条目(在主窗口旁边)。对话框 2 (qDlg2) 是暂时的,不会出现。

snapshot of testQMainWin-Dlgs after Alt+Tab

对话框1可以在任务选择中单独选择(Alt+Tab)。选择主窗口,主窗口和对话框 2 被置于顶部。因此,对话框 2 始终位于主窗口的顶部。

注意:

我不确定 qDlg2.setParent(&qWin, Qt::Dialog); 中的第二个nd 参数 Qt::Dialog;是必要的。好像是。 (当我把它去掉时,对话框 2 变得根本不可见。)

关于c++ - 当我按 alt-tab 键进入其中任何一个窗口时,如何将所有窗口强制置于最前面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47119950/

相关文章:

c++ - 进程 "mingw32-make.exe"崩溃错误

Qt 如何使用 x() 和 y() 位置在小部件上查找对象

qt - QList:length() 和 count() 函数之间的区别?

c++ - 收到错误 : floating point exception

c++ - 为什么指向基类的指针优于引用?

c++ - 错误: invalid conversion from ‘int*’ to ‘int’ [-fpermissive]

c++ - 导入 csv 时出现段错误 11 错误

c++ - 将小部件放在 QScrollArea 的中心

qt - 如何使用诺基亚 Qt SDK 为桌面环境开发 Qt 应用程序?

c++ - 如何修复此子类的初始化程序列表错误?