In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces Qt how to use QDialog to achieve interface mask related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this Qt how to use QDialog to achieve interface mask article will have a harvest, let's take a look.
Let's take a look at the effect:
According to the demand function, we need to provide the interface to set the main window, similarly, not all windows need to be masked, then we also need to know which windows need to be masked, therefore, we also need to provide a standard of judgment, in a project, each UI file's objectName is unique, so we can use these objectName to determine which dialog needs masking.
This class automatically invokes the display when the masked dialog needs to be displayed, rather than manually, so you need to detect the global event loop.
Above, let's take a look at the header file definition of the component:
# ifndef MASK_WIDGET_H#define MASK_WIDGET_H#include namespace Ui {class MaskWidget;} class MaskWidget: public QDialog {Q_OBJECT Q_PROPERTY (QStringList names READ names WRITE setNames DESIGNABLE true) public: static MaskWidget * instance (); void setMainWidget (QWidget* pWidget); QStringList names () const; void setNames (const QStringList& names); protected: bool eventFilter (QObject * obj, QEvent * event); private: explicit MaskWidget (QWidget* parent = Q_NULLPTR); ~ MaskWidget () Private: Ui::MaskWidget* ui; QStringList m_listName {QStringList ()}; QWidget* m_pMainWidget {Q_NULLPTR}; static MaskWidget* masked pSelf;}; # endif / / MASK_WIDGET_H
As can be seen from the above class definition, this component is relatively simple, as simple as only two interfaces and an event filter function, so let's take a look at the implementation in detail.
First of all, there is a stereotyped singleton implementation. If there is only one copy of this component in the whole project, there may be unexpected situations (multi-layer coverage or conflict):
MaskWidget * MaskWidget::m_pSelf = return MaskWidget::instance * MaskWidget::instance () {if (new MaskWidget; = = Q_NULLPTR) {m_pSelf = new MaskWidget;} maskWidget;}
In its construction, we need to set some window-related properties and hide the window first, otherwise the program will see a gray mask on it as soon as it opens. In fact, the most important thing is to register event filtering in its constructor.
MaskWidget::MaskWidget (QWidget * parent): QDialog (parent), ui (new Ui::MaskWidget) {ui- > setupUi (this); hide (); setWindowFlags (Qt::FramelessWindowHint | Qt::Tool | Qt::WindowDoesNotAcceptFocus); qApp- > installEventFilter (this);}
After the main program starts, we still have two things to do, that is, the two interfaces we mentioned earlier need to be called for implementation, one is to set the main window that needs to be masked, and the other is to set the name of the window that pops up that needs to be masked. Let's take a look at setting the main window.
Void MaskWidget::setMainWidget (QWidget * pWidget) {this- > setFixedSize (QSize (pWidget- > width (), pWidget- > height ()); this- > setParent (pWidget); this- > move (pWidget- > x (), pWidget- > y ());}
As can be seen from the above, after setting the main window, we also set the parent class of the component as the main window, which ensures that when the component is displayed, it must be displayed with the set main window as the parent node. and can cover the entire main window.
The setting of the display window is also a relatively simple way to operate the properties, as follows:
Void MaskWidget::setNames (const QStringList& names) {if (m_listName = = names) {return;} m_listName = names;} QStringList MaskWidget::names () const {return names;}
In the whole process, the most important thing is the implementation of the event filtering function, which basically contains the basic functions of the component. Let's take a look at the implementation of this function.
Bool MaskWidget::eventFilter (QObject * obj, QEvent * event) {if (event- > type () = = QEvent::Hide) {if (m_listName.contains (obj- > objectName () {hide ();} return QObject::eventFilter (obj, event) } if (event- > type () = = QEvent::Show) {if (! m_listName.contains (obj- > objectName () {return QObject::eventFilter (obj, event);} show (); auto pWidget = dynamic_cast (obj) / / convert object to normal QWidget if (Q_NULLPTR = = pWidget) {return QObject::eventFilter (obj, event);} pWidget- > activateWindow (); pWidget- > setFocus (Qt::ActiveWindowFocusReason); stackUnder (pWidget) / / place the window settings under the pop-up window if (Q_NULLPTR = = m_pMainWidget) {return QObject::eventFilter (obj, event);} mroompMainWidget-> stackUnder (this) / / put the main window setting below the interface of the component, you can have a relatively clear hierarchical relationship / / the following is to move the position of the pop-up window to the middle of the main program. The purpose here is to reduce the amount of code. After all, you can write code lazy or must steal QRect screenGeometry = mroompMainWidget-> geometry () Int x = screenGeometry.x () + (screenGeometry.width ()-pWidget- > width ()) / 2; int y = screenGeometry.y () + (screenGeometry.height ()-pWidget- > height ()) / 2; pWidget- > move (x, y);} return QObject::eventFilter (obj, event);}
Above, all the functions of the component have been introduced.
In the process of using, the include file can be used directly. It is important to note that the base class of the pop-up dialog window must be QDialog, and use the QDialog::exec () function to implement the mode when calling. If you do not implement the mode, there will be some accidents, of course, these accidents do not affect the use, but the interaction will be less friendly. Assuming that your main program cannot be moved, it will be very unfriendly.
TestDialog dlg;if (QDialog::Accept = = dlg.exec ()) {} on "how Qt uses QDialog to implement interface masking" ends here. Thank you for reading! I believe that everyone has a certain understanding of "Qt how to use QDialog to achieve interface masking" knowledge, if you want to learn more knowledge, welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.