子窗口在父窗口上方浮动显示,点击父窗口,子窗口不会被父窗口覆盖,有如下3种方法:
1、继承自QDialog,使用show()方法显示窗口。
dialog->show();
2、继承自QWidget,设置窗口标志Qt::Tool。
widget->setWindowFlags(Qt::Tool);
3、继承自QWidget,设置窗口标志Qt::WindowStaysOnTopHint。这个标志是对于所有程序的窗口都置顶。也就是说其他程序打开后也是会被置顶的窗口所遮盖。
widget->setWindowFlags(Qt::WindowStaysOnTopHint);
//或者同时设置两个
widget->setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint);
下方摘自qt是说明文档:
Qt::Tool是一个工具窗口,有父窗口时会一直置于父窗口的上方。如果没有父窗口时可以设置Qt::WindowStaysOnTopHint标志。
Constant | Value | Description |
---|---|---|
Qt::Tool | Popup | Dialog | Indicates that the widget is a tool window. A tool window is often a small window with a smaller than usual title bar and decoration, typically used for collections of tool buttons. If there is a parent, the tool window will always be kept on top of it. If there isn’t a parent, you may consider using Qt::WindowStaysOnTopHint as well. If the window system supports it, a tool window can be decorated with a somewhat lighter frame. It can also be combined with Qt::FramelessWindowHint. On macOS, tool windows correspond to the NSPanel class of windows. This means that the window lives on a level above normal windows making it impossible to put a normal window on top of it. By default, tool windows will disappear when the application is inactive. This can be controlled by the Qt::WA_MacAlwaysShowToolWindow attribute. |