当前位置:网站首页>QT实现窗口置顶、置顶状态切换、多窗口置顶优先关系
QT实现窗口置顶、置顶状态切换、多窗口置顶优先关系
2022-07-06 09:28:00 【Larry_Yanan】
我们使用QT进行界面开发时,可能会遇到需要将窗口置顶的情况。最常见的就是,需要制作一个悬浮工具栏,悬浮菜单,甚至是悬浮的画板。这就意味这我们需要将这个窗口置顶于“系统”以及我们自己“软件”的窗口之上。
其实实现的方法很简单,就是在创建这个窗口类的时候,在构造函数中的加一个Qt::WindowFlags
枚举标识
Widget::Widget(QWidget *parent)
: QWidget(parent,Qt::Widget | Qt::WindowStaysOnTopHint)
, ui(new Ui::Widget)
{
ui->setupUi(this);
}
Qt::WindowStaysOnTopHint的意思就是窗口置顶的意思,设置了之后就能一直悬浮在桌面上面了。
有关其他的枚举标识,可以参考这个。Qt::WindowFlags枚举类型解析 像取消顶部工具条、无边框、弹窗化什么的,都可以实现。
现在有一种情况是,我们需要灵活切换窗口的置顶状态,那就可以直接调用相关的设置接口来实现。setWindowFlags();
具体的话是这样:
void Widget::on_pushButton_clicked()
{
if(!istop)
{
Qt::WindowFlags m_flags = windowFlags();
setWindowFlags(m_flags | Qt::WindowStaysOnTopHint);
show();
qDebug()<<"always top";
}else{
//Qt::WindowFlags m_flags = windowFlags();
//setWindowFlags(Qt::Widget);
setWindowFlags(NULL);
show();
qDebug()<<"no always top";
}
istop=!istop;
}
setWindowFlags(Qt::Widget);和setWindowFlags(NULL);都可以取消置顶状态,只要重新设置就可以了。但这样设置之后,窗口是默认不显示的,所以需要show一下。但show的时候,窗口会有明显的闪烁。这个还没能很好的解决。
另外还要一个问题,两个置顶窗口之间的优先级无法设置和判定,也就是说用同样的方法设置两个置顶窗口,他们之间的拖动,是可以覆盖住对方的,取决于当前焦点点击再哪个窗口里面。
这个问题,在某些场合里面是互斥违和的。
目前有一种方法是,把a对象设置成b对象的父类,那么b对象就始终置顶与a对象
Widget::Widget(QWidget *parent)
: QWidget(parent,Qt::Widget | Qt::WindowStaysOnTopHint)
, ui(new Ui::Widget)
{
ui->setupUi(this);
son_widget_1 = new QWidget(this);
son_widget_2 = new QWidget(son_widget_1);
son_widget_1->setWindowFlags(Qt::Dialog | Qt::WindowStaysOnTopHint);
son_widget_2->setWindowFlags(Qt::Dialog | Qt::WindowStaysOnTopHint);
son_widget_1->setWindowTitle("son_widget_1");
son_widget_2->setWindowTitle("son_widget_2");
son_widget_1->show();
son_widget_2->show();
}
这三个置顶窗口是互相叠加的,跟父子关系有关。(son_widget_1/2设置了Qt::Dialog,如果是Qt::Widget,在设置了父类的情况下,是父类的子部件)。
2022.5.25补充
感谢评论区的朋友提供更好的解决方式,解决了切换置顶时必须show所导致的闪烁问题,以下是验证过可行的代码:
void Widget::on_pushButton_clicked()
{
if(!istop)
{
// Qt::WindowFlags m_flags = windowFlags();
// setWindowFlags(m_flags | Qt::WindowStaysOnTopHint);
// show();
qDebug()<<"always top";
QWindow* pWin = this->windowHandle();
pWin->setFlags(Qt::Widget | Qt::WindowStaysOnTopHint);
}else{
//Qt::WindowFlags m_flags = windowFlags();
// setWindowFlags(Qt::Widget);
// //setWindowFlags(NULL);
// show();
QWindow* pWin = this->windowHandle();
pWin->setFlags(Qt::Widget);
qDebug()<<"no always top";
}
istop=!istop;
}
边栏推荐
- Auto. Getting started with JS
- [analysis of teacher Gao's software needs] collection of exercises and answers for level 20 cloud class
- 【练习-3】(Uva 442)Matrix Chain Multiplication(矩阵链乘)
- 969. Pancake sorting
- Information security - threat detection engine - common rule engine base performance comparison
- socket通讯
- C basic grammar
- Penetration test (4) -- detailed explanation of meterpreter command
- Penetration test (7) -- vulnerability scanning tool Nessus
- 1013. Divide the array into three parts equal to and
猜你喜欢
Sword finger offer II 019 Delete at most one character to get a palindrome
7-1 understand everything (20 points)
Information security - threat detection - Flink broadcast stream broadcaststate dual stream merging application in filtering security logs
读取和保存zarr文件
X-forwarded-for details, how to get the client IP
滲透測試 ( 1 ) --- 必備 工具、導航
Basic Q & A of introductory C language
window11 conda安装pytorch过程中遇到的一些问题
Flask框架配置loguru日志庫
树莓派4B安装opencv3.4.0
随机推荐
Borg maze (bfs+ minimum spanning tree) (problem solving report)
Gartner:关于零信任网络访问最佳实践的五个建议
QNetworkAccessManager实现ftp功能总结
[exercise-1] (UVA 673) parentheses balance/ balanced brackets (stack)
PySide6 信号、槽
读取和保存zarr文件
Auto. Getting started with JS
渗透测试 ( 1 ) --- 必备 工具、导航
1013. Divide the array into three parts equal to and
frida hook so层、protobuf 数据解析
渗透测试 ( 7 ) --- 漏洞扫描工具 Nessus
B - Code Party (girls' competition)
树莓派4B安装opencv3.4.0
Alice and Bob (2021 Niuke summer multi school training camp 1)
Analysis of protobuf format of real-time barrage and historical barrage at station B
信息安全-威胁检测引擎-常见规则引擎底座性能比较
C language is the watershed between low-level and high-level
【练习-4】(Uva 11988)Broken Keyboard(破损的键盘) ==(链表)
Shell Scripting
Sword finger offer II 019 Delete at most one character to get a palindrome