当前位置:网站首页>QT realizes window topping, topping state switching, and multi window topping priority relationship
QT realizes window topping, topping state switching, and multi window topping priority relationship
2022-07-06 16:16:00 【Larry_ Yanan】
We use QT During interface development , You may encounter the need to put the window on the top . The most common is , You need to make a floating toolbar , Suspension menu , Even a floating drawing board . This means that we need to put this window on the top “ System ” And ourselves “ Software ” On top of the window .
In fact, the implementation method is very simple , When creating this window class , Add one in the constructor Qt::WindowFlags
Enumeration identification
Widget::Widget(QWidget *parent)
: QWidget(parent,Qt::Widget | Qt::WindowStaysOnTopHint)
, ui(new Ui::Widget)
{
ui->setupUi(this);
}
Qt::WindowStaysOnTopHint It means that the window is on the top , After setting, it can be suspended on the desktop .
For other enumeration identifiers , You can refer to this .Qt::WindowFlags Enumeration type resolution Like canceling the top toolbar 、 no border 、 Pop up or something , Can be realized .
Now there is a situation , We need to flexibly switch the top state of the window , Then you can directly call the relevant setting interface to realize .setWindowFlags();
To be specific :
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); and setWindowFlags(NULL); Can cancel the top status , Just reset it . But after this setting , The window is not displayed by default , So we need to show once . but show When , The window will flash obviously . This has not been well solved .
Another question , The priority between two top windows cannot be set and determined , That is to say, set two top windows in the same way , The drag between them , It can cover each other , It depends on which window the current focus clicks in .
This problem , On some occasions, it is mutually exclusive and contradictory .
At present, one method is , hold a Object set to b The parent of the object , that b The object is always at the top and a object
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();
}

These three top windows are superimposed on each other , It's related to the relationship between father and son .(son_widget_1/2 Set up Qt::Dialog, If it is Qt::Widget, When the parent class is set , Is a child part of the parent class ).
2022.5.25 Add
Thank the friends in the comment area for providing better solutions , It solves the problem that you must show Caused by flickering , The following code has been verified to be feasible :
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;
}
边栏推荐
- QNetworkAccessManager实现ftp功能总结
- [teacher Gao UML software modeling foundation] collection of exercises and answers for level 20 cloud class
- 1013. Divide the array into three parts equal to and
- Acwing - game 55 of the week
- 860. Lemonade change
- 日期加1天
- Codeforces Round #803 (Div. 2)A~C
- C language must memorize code Encyclopedia
- [exercise-8] (UVA 246) 10-20-30== simulation
- Pyside6 signal, slot
猜你喜欢

VMware Tools和open-vm-tools的安装与使用:解决虚拟机不全屏和无法传输文件的问题

Browser print margin, default / borderless, full 1 page A4

树莓派4B安装opencv3.4.0

Penetration test (4) -- detailed explanation of meterpreter command
![[exercise-7] crossover answers](/img/66/3dcba2e70a4cd899fbd78ce4d5bea2.png)
[exercise-7] crossover answers

OneForAll安装使用

Ball Dropping

Maximum product (greedy)

Read and save zarr files

Codeforces Round #799 (Div. 4)A~H
随机推荐
Differential (one-dimensional, two-dimensional, three-dimensional) Blue Bridge Cup three body attack
Luogu P1102 A-B number pair (dichotomy, map, double pointer)
The concept of C language array
Educational Codeforces Round 130 (Rated for Div. 2)A~C
1005. Maximized array sum after K negations
力扣:第81场双周赛
628. Maximum product of three numbers
Codeforces Round #800 (Div. 2)AC
AcWing:第58场周赛
Anaconda下安装Jupyter notebook
计算时间差
Common configuration files of SSM framework
QWidget代码设置样式表探讨
[exercise-3] (UVA 442) matrix chain multiplication
VMware Tools和open-vm-tools的安装与使用:解决虚拟机不全屏和无法传输文件的问题
指定格式时间,月份天数前补零
Date plus 1 day
Truck History
409. Longest palindrome
Analysis of protobuf format of real-time barrage and historical barrage at station B