Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to implement Qt Mask layer form

Shulou Source: shulou.com Published: 2022-06-01 13:22:01 10月01日 Update

This article mainly explains "how to implement the Qt mask layer form". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to implement the Qt mask layer form.

I. Preface

In some projects, the original main form needs to be masked behind the pop-up form to highlight the pop-up window, and it suddenly occurred to me that I had written a global screenshot before, which was consistent in principle and used to change it. You just need to introduce a header file and implementation file, and then set the class name of the main form that needs to be masked and the possible pop-up window form in the main form. it's as simple as that. Multiple form lists can be supported. The principle is very simple, first install the event filter, intercept the QEvent::Show event, get the current form, if in the list of forms that need to be masked, then pop up a translucent form with the same size as the pop-up form, and then activate the current pop-up form.

2. Code idea / / usage / / the first step is to set the parent form MaskWidget::Instance ()-> setMainWidget (this) that needs to be masked; / / the second step is to set which pop-up window forms need to be masked QStringList dialogNames;dialogNames dialogNames! = dialogNames) {this- > dialogNames = dialogNames;}} void MaskWidget::setOpacity (double opacity) {this- > setWindowOpacity (opacity);} void MaskWidget::setBgColor (const QColor & bgColor) {QPalette palette = this- > palette () Palette.setBrush (QPalette::Background, bgColor); this- > setPalette (palette);} void MaskWidget::showEvent (QShowEvent *) {if (mainWidget! = 0) {this- > setGeometry (mainWidget- > geometry ());}} bool MaskWidget::eventFilter (QObject * obj, QEvent * event) {if (event- > type () = QEvent::Show) {if (dialogNames.contains (obj- > objectName () {this- > show () This- > activateWindow (); QWidget * w = (QWidget *) obj; w-> activateWindow ();}} else if (event- > type () = = QEvent::Hide) {if (obj- > objectName ()) {this- > hide () }} else if (event- > type () = = QEvent::WindowActivate) {/ / when the main form is activated, activate the mask layer if (mainWidget! = 0) {if (obj- > objectName () = = mainWidget- > objectName ()) {if (this- > isVisible ()) {this- > activateWindow () }} return QObject::eventFilter (obj, event);} 3. Effect diagram

At this point, I believe that everyone on the "Qt mask layer form how to achieve" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Form activation consistency event code content principle size file method learning practical deeper size usage global interest preface at the same time practicality Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Docker OPPO Reno Shulou Tech Info Xiaomi