当前位置:网站首页>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;
}
边栏推荐
- Shell Scripting
- B - Code Party (girls' competition)
- pytorch提取骨架(可微)
- MariaDB的安装与配置
- [exercise-1] (UVA 673) parentheses balance/ balanced brackets (stack)
- Is the sanic asynchronous framework really so strong? Find truth in practice
- Auto. Getting started with JS
- What is the difficulty of programming?
- C language is the watershed between low-level and high-level
- OneForAll安装使用
猜你喜欢
Pyside6 signal, slot
Anaconda下安装Jupyter notebook
b站 實時彈幕和曆史彈幕 Protobuf 格式解析
1013. Divide the array into three parts equal to and
Radar equipment (greedy)
Luogu P1102 A-B number pair (dichotomy, map, double pointer)
D - function (HDU - 6546) girls' competition
Suffix expression (greed + thinking)
Read and save zarr files
第 300 场周赛 - 力扣(LeetCode)
随机推荐
Flask框架配置loguru日志庫
Input can only input numbers, limited input
Li Kou - 298th weekly match
Maximum product (greedy)
Penetration test 2 --- XSS, CSRF, file upload, file inclusion, deserialization vulnerability
(POJ - 3579) median (two points)
969. Pancake sorting
Common configuration files of SSM framework
1005. Maximized array sum after K negations
b站 实时弹幕和历史弹幕 Protobuf 格式解析
Codeforces Round #799 (Div. 4)A~H
Interesting drink
(POJ - 3685) matrix (two sets and two parts)
useEffect,函數組件掛載和卸載時觸發
Codeforces Round #803 (Div. 2)A~C
Codeforces Round #801 (Div. 2)A~C
AcWing:第56场周赛
860. Lemonade change
Bidirectional linked list - all operations
Codeforces Round #803 (Div. 2)A~C