One , sketch

In use some time ago setWindowFlags Method encountered a hole , The specific situation is to pass a window interface checkBox To control whether the current state of the window is set to the top , and Qt Provided Qt::WindowStaysOnTopHint sign , The ability to keep windows in front of other windows at all times , That is to put the window on top .

theoretically , We're on the tick checkBox After that Qt::WindowStaysOnTopHint Sign setting , The window will be on the top , The result is that the window is hidden . So why the second call setWindowFlags When setting the window flag bit, the window will be hidden ( Actually called hide() method ), Let's take a look at the specific causes .

Qt::WindowStaysOnTopHint

Informs the window system that the window should stay on top of all other
windows. Note that on some window managers on X11 you also have to pass
Qt::X11BypassWindowManagerHint for this flag to work correctly.

Two , Analyze the reasons for window hiding

First, let's look at the code , In the constructor, we bind checkBox, Then set the window properties .
this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinimizeButtonHint);
connect(ui.checkBox,SIGNAL(stateChanged(int)), this, SLOT(onStateChanged(int)));
checkBox State change slot function
void onStateChanged(int state) { if (state == Qt::Unchecked) { // Do not set top if not checked ; this
->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinimizeButtonHint); } else
if (state == Qt::Checked) { // Top when checked ; this->setWindowFlags(Qt
::FramelessWindowHint | Qt::WindowMinimizeButtonHint | Qt::WindowStaysOnTopHint
); } }
Want to find out why , You have to look directly Qt Source code , So we followed up directly qwidget.cpp Medium setWindowFlags method .

After debugging , It's found that the setParent method , Let's take a closer look , stay setParent Method is called hide method
, When will it be called , The first is to determine whether the window is created , Then it's whether the window is hidden , This is called if the window is created and not hidden hide method . Here's the
Invocation in constructor setWindowFlags Time ,wasCreated by false, It will not be called hide, And the window was not created , So when the window is not created, no matter how many times it is called
setWindowFlags method , Will be based on the parameters passed in on the last call .

The following is a check in the window checkBox, And then called again setWindowFlags
method , The window is created and not hidden , So it's called here hide Method to hide the window , Finally, the reason was found .

Then it is not impossible to solve the problem of window top ,windows Provided SetWindowPos Method to solve the problem of window top . If we want to keep the window on top all the time, we can call it when the window is initialized setWindowFlags Method pass in Qt::WindowStaysOnTopHint Mark to top the window .

If both windows are in the top state , If they are stacked together, they will cover each other .

Solution 1 :
void onStateChanged(int state) { if (state == Qt::Unchecked) { ::SetWindowPos
((HWND)this->winId(), HWND_NOTOPMOST, this->pos().x(), this->pos().y(),
width(), height(), SWP_SHOWWINDOW); }else if (state == Qt::Checked) {
::SetWindowPos((HWND)this->winId(), HWND_TOPMOST, this->pos().x(), this->pos().
y(), width(), height(), SWP_SHOWWINDOW); } }
Solution 2 :
void onStateChanged(int state) { if (state == Qt::Unchecked) { // Do not set top if not checked ; this
->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinimizeButtonHint); //
If the current window is not visible , It will be displayed ; if (!this->isVisible()) { setVisible(true); } } else if (state
== Qt::Checked) { // Top when checked ; this->setWindowFlags(Qt::FramelessWindowHint | Qt
::WindowMinimizeButtonHint | Qt::WindowStaysOnTopHint); // If the current window is not visible , It will be displayed ; if (
!this->isVisible()) { setVisible(true); } } }
Updated on 2017_7_12.

Technology
©2019-2020 Toolsou All rights reserved,
Image format conversion Count the number of letters (java Language implementation ) Using orthogonal table method to design test cases One is called “ Asking for the train ” A small village Finally got the train VaR - Value at risk - Monte Carlo method - Python How to build data security system ?python: Convert a file to a binary file (binary)vue use vue-clipboard2 Realize the function of copy link 13. solve git Merge conflict uniapp Summary of page value transfer