当前位置:网站首页>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;
}
边栏推荐
- 1323. Maximum number of 6 and 9
- What is the difficulty of programming?
- Alice and Bob (2021 Niuke summer multi school training camp 1)
- 渗透测试 2 --- XSS、CSRF、文件上传、文件包含、反序列化漏洞
- Specify the format time, and fill in zero before the month and days
- TCP's three handshakes and four waves
- Codeforces - 1526C1&&C2 - Potions
- New to redis
- Candy delivery (Mathematics)
- window11 conda安装pytorch过程中遇到的一些问题
猜你喜欢

antd upload beforeUpload中禁止触发onchange

1013. Divide the array into three parts equal to and

顺丰科技智慧物流校园技术挑战赛(无t4)

1323. Maximum number of 6 and 9

D - function (HDU - 6546) girls' competition

Codeforces Round #797 (Div. 3)无F
frida hook so层、protobuf 数据解析
![[exercise-7] crossover answers](/img/66/3dcba2e70a4cd899fbd78ce4d5bea2.png)
[exercise-7] crossover answers

Openwrt build Hello ipk

7-1 understand everything (20 points)
随机推荐
QT实现圆角窗口
Shell Scripting
D - function (HDU - 6546) girls' competition
(POJ - 3579) median (two points)
Data storage in memory & loading into memory to make the program run
“鬼鬼祟祟的”新小行星将在本周安全掠过地球:如何观看
Suffix expression (greed + thinking)
Effet d'utilisation, déclenché lorsque les composants de la fonction sont montés et déchargés
Radar equipment (greedy)
(POJ - 3685) matrix (two sets and two parts)
Codeforces Round #799 (Div. 4)A~H
875. Leetcode, a banana lover
TCP's three handshakes and four waves
(POJ - 2739) sum of constructive prime numbers (ruler or two points)
Interesting drink
[exercise-7] (UVA 10976) fractions again?! (fraction split)
Codeforces Round #799 (Div. 4)A~H
[exercise-1] (UVA 673) parentheses balance/ balanced brackets (stack)
顺丰科技智慧物流校园技术挑战赛(无t4)
Opencv learning log 24 -- Hough transform 2 (maximum interval and minimum length can be limited)