当前位置:网站首页>Implement window blocking on QWidget

Implement window blocking on QWidget

2022-07-06 08:55:00 InfoQ



A summary of the premises

When we want to implement blocking windows , The first reaction was QDialog The modal window of , But because of QDialog Failure of normal use, etc ( I have encountered this problem ), Then we need to find another way . And then it's shown in QWidget The blocking effect is achieved under the non modal window of .

Code display :

Widget = new QWidget;

Widget->setWindowFlags(Widget->windowFlags() |Qt::Dialog);

Widget->setWindowModality(Qt::ApplicationModal); // Block all forms except the current form

setWindowFlags  It can be used to set the properties of the window , His attributes can be divided into many kinds ( If you are interested, you can check , I'll just introduce what I use and some simple ).

Qt::Dialog As one can imagine , It is equivalent to Qwidget Forced conversion to QDialog Of , But his compulsion is not so obvious , But it uses QDialog Default value of constructor , Therefore, we can make QWidget Realize the effect of combined window . The same can be ,Qt::Widget and Qt::Window equally . We can think about their usage by drawing inferences from one instance , But remember to check it after you use your brain , Whether the identification is completely correct .

Widget->windowFlags() Is to obtain Widget The current properties of .

New learning content

In the process of checking the data, I found ,Qt::ToolTip This window can be set as a prompt window , But there is no title bar and window border .

summary

If it can be used normally QDialog As a modal window , The most recommended is the system default , This kind of window blocking implemented by forced conversion is recommended only when it is unavoidable . After all, it must have its stability and benefits , Otherwise, developers will not do this . And this method is prone to confusion , Easy to form bad habits , Just like I'm creating new interfaces now, they are used to QWidget, We must correct this bad habit in the future , I also hope you don't learn from me .

This concludes today's update , I hope the boss will criticize and give advice !
原网站

版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060852461192.html