qt This function is commonly used in the center top window :

this->setWindowFlags(Qt::FramelessWindowHint);

But there is a problem :

When I start the program display window , Keep clicking on other windows , Causes the currently running program window to lose focus , If the program window is set FramelessWindowHint flag,

The main window is not topped . The code is as follows :

main.cpp

#include "mainwindow.h" #include <QApplication> int main(int argc, char
*argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec();
}#include "mainwindow.h" #include <QApplication> #include <QDesktopWidget>
#include <QThread> MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent) {
this->setFixedSize(QSize(QApplication::desktop()->width(),
QApplication::desktop()->height())); // In general, the close button will be customized
this->setWindowFlags(Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint);
this->setAutoFillBackground(true); // Suppose there are other resources to initialize , If the construction time is short , It's not easy to see the effect .
QThread::sleep(1); } MainWindow::~MainWindow() { }

After the program starts to run, keep clicking on other windows ( as QtCreator), You will find that the window program appears in the taskbar , But the window is not topped .

The specific reasons need to be further studied .

Try a variety of solutions :

1. After clicking a button show Out of a top window . ( invalid )

2. Click the next button show Open a new window (subwindow attribute , The parent window is empty , Managing memory with smart pointer ).( Effective , But new show The window that comes out cannot respond to events , It's covered by the initial window );

...

Later I thought of using it directly windows The function of the top window , But there will still be the problem at the beginning .

And then try all sorts of weird tricks ...

In the end, unintentionally show(); This code is written to the constructor of the top window !

The final code is as follows :

main.cpp

#include "mainwindow.h" #include <QApplication> #include <windows.h> int
main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; //[!]
New code , Need to be in .pro Document plus LIBS += -lUser32 ::SetWindowPos((HWND)w.winId(),
HWND_TOPMOST, 0, 0, w.width(), w.height(), SWP_NOMOVE | SWP_NOSIZE |
SWP_SHOWWINDOW); return a.exec(); }mainwindow.cpp
#include "mainwindow.h" #include <QApplication> #include <QDesktopWidget>
#include <QThread> MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent) {
this->setFixedSize(QSize(QApplication::desktop()->width(),
QApplication::desktop()->height())); // In general, the close button will be customized
this->setWindowFlags(Qt::FramelessWindowHint/*|Qt::WindowStaysOnTopHint*/);//
[!] Comments are required qt Top property of this->setAutoFillBackground(true); //
Suppose there are other resources to initialize , If the construction time is short , It's not easy to see the effect . QThread::sleep(1); //[!] New code show(); }

Technology
©2019-2020 Toolsou All rights reserved,
Digital rolling lottery program Keras Save and load model (JSON+HDF5) Remember once EventBus Project issues caused by memory leaks I've been drinking soft water for three years ? What is the use of soft water and water softener msf Generate Trojan horse attack android mobile phone Time conversion front desk will 2020-07-17T03:07:02.000+0000 Into 2020-07-17 11:07:02 Chuan Shen 1190 Reverses the substring between each pair of parentheses leetcodehive Summary of processing methods for a large number of small files SparkSQL Achieve partition overlay write Image format conversion