Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to achieve Mutual exclusion with Qt Custom Widget

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this issue, the editor will bring you about Qt custom Widget how to achieve the effect of mutual exclusion. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

Frontier

What is meant by custom Widget to implement mutexes?

When using Qt to do an interface aesthetic function, you may encounter this problem: multiple controls are mutually exclusive, similar to QRadiButton controls, but not simple QRadioButton controls, mutually exclusive may be a window, may also be a few buttons, and so on.

Here I just give a simple example of mutual exclusion, although simple, but contains a variety of holes, diggers in need can take notes, especially for novices in Qt, it is still necessary.

You can see from the effect that three custom widget have been created. When you click on one of them, the other two background colors and text color changes are selected.

Next, the functions shown in the effect picture are explained one by one, including knowledge points and trampling records.

Function realization

You have encountered the following knowledge points and problems in the process of implementing a custom mutex widget. See if there are any features you have encountered or just need.

Knowledge point

The four-state function of 1:Widget simulation button, including: normal, press, focus, disable

Background color settings and text content style settings for 2:Widget custom classes

3: how to make multiple widget mutually exclusive

problem

1: why doesn't it take effect after customizing the Widget background color setting?

In view of the above knowledge points and problems to talk about this simple function!

Explain knowledge point 1

Using the four-state function of the Widget simulation button, you need to use the message of Widget itself: mouse down, mouse in, mouse out.

Virtual void mousePressEvent (QMouseEvent * event); / / mouse down response message virtual void enterEvent (QEvent * event); / / mouse entry response message virtual void leaveEvent (QEvent * event); / / mouse leave response message

Does anyone ask why there is no mouseMoveEvent message?

Answer: using the mouseMoveEvent message mouse directly in Qt cannot be triggered. You must set setMouseTracking (true) to make the mouse tracking event valid in the current window.

Whether this function needs to be set up according to the specific circumstances of use. In the current small demo, only do the image conversion, there is no need to consume resources in mouseMove all the time.

(digression: mouse mosemove events under the MFC framework are directly available and do not require special settings)

Once the mouse enters the widget, it can be marked as the mouse has been active in the widget unless a leaveEvent message is triggered.

Mouse down response message

Void QCustomWidget::mousePressEvent (QMouseEvent * event) {this- > SetWidgetStyle (Style_Down); QWidget::mousePressEvent (event);}

Currently used enumeration type: mouse down response.

Mouse enters widget response message

Void QCustomWidget::enterEvent (QEvent * event) {this- > SetWidgetStyle (Style_Focus); QWidget::enterEvent (event);}

The enumeration type currently used: mouse focus state, using incoming messages instead of mousemove messages.

If you log, you will find that the trigger function will only go once when the mouse is entered, and will not be triggered when the mouse continues to move within the widget, which greatly reduces message processing.

Mouse off widget response message

Void QCustomWidget::leaveEvent (QEvent * event) {this- > SetWidgetStyle (Style_Normal); QWidget::leaveEvent (event);}

Currently used enumeration type: mouse away state.

I just showed the simplest leave setting, and there is one thing to consider: if the current widget is pressed and the mouse is left, how should it be displayed?

Do you still want to show the normal style?

The answer must be NO!

Although the mouse has been removed, the selected state has changed from normal to pressed. In the program, we need to use a Bool variable to record whether the current widget has been selected. If so, we need to change it to the selected state when the mouse leaves.

The modifications are as follows:

If (m_bClickedState = = true) {this- > SetWidgetStyle (Style_Down);} else this- > SetWidgetStyle (Style_Normal); explain knowledge point 2

In the program, the state of the simulation is represented by the type of enumeration.

Type description initial state of Style_Normal mouse without any action Style_Down mouse is pressed in wiget when Style_Focus mouse moves in widget Style_Disable current widget is in progress

The same content is shown in each widget: number, text

Because only the display function is done, all QLabel controls are used.

QLabel * mumber labNumber; / / numbered class pointer

QLabel * masked LabContente; / / content class pointer

Corresponding actual processing

Void QCustomWidget::SetWidgetStyle (ENUM_WidgetStyle enumStyle) {/ / TODO: set widget style QString qsStyle = "", gStyleNumberNormal = "", gStyleContentNormal = ""; switch (enumStyle) {case Style_Normal: / / normal display {/ / setting: background qsStyle = "QWidget {background-color:#FFD700}" / / setting: numbering style gStyleNumberNormal = "QLabel {color:#666666; font-family:Microsoft YaHei UI; font-size:14px;} QLabel {background-color: transparent}"; / / setting: content style gStyleContentNormal = "QLabel {color:#666666; font-family:Microsoft YaHei UI; font-size:14px;} QLabel {background-color: transparent}" } break; case Style_Down: / / Press {/ / set: background qsStyle = "QWidget {background-color:#FFB6C1}"; / / set: numbering style gStyleNumberNormal = "QLabel {color:#0000FF; font-family:Microsoft YaHei UI; font-size:14px;} QLabel {background-color: transparent}" / / setting: content style gStyleContentNormal = "QLabel {color:#00FFFF; font-family:Microsoft YaHei UI; font-size:14px;} QLabel {background-color: transparent}";} break; case Style_Focus: / / focus {/ / setting: background qsStyle = "QWidget {background-color:#FFF0F5}" / / setting: numbering style gStyleNumberNormal = "QLabel {color:#98FB98; font-family:Microsoft YaHei UI; font-size:14px;} QLabel {background-color: transparent}"; / / setting: content style gStyleContentNormal = "QLabel {color:#98FB98; font-family:Microsoft YaHei UI; font-size:14px;} QLabel {background-color: transparent}" } break; case Style_Disable: / / disable {/ / setting: background qsStyle = "QWidget {background-color:#DCDCDC}"; / / setting: numbering style gStyleNumberNormal = "QLabel {color:#696969; font-family:Microsoft YaHei UI; font-size:14px;} QLabel {background-color: transparent}" / / setting: content style gStyleContentNormal = "QLabel {color:#696969; font-family:Microsoft YaHei UI; font-size:14px;} QLabel {background-color: transparent}";} break; default: break;} this- > setStyleSheet (qsStyle); masked labNumber-> setStyleSheet (gStyleNumberNormal) SetStyleSheet Contente-> labeling (gStyleContentNormal);}

The background style varies according to different types. You can bring the code in, run to see the effect, is it consistent with the effect I show?

Ha ha! If you try, you will find that it looks like this:

Why can only display text, my background? Where did you go? Haven't I already set it up?

Many Qt novices encounter this problem here, so they open various search modes and try various methods, sometimes just change it, and ignore the problem.

When we create a custom widget, the general method uses the new instance method. In the process of new, this is passed as the parent pointer of the newly created window in order to take care of the parent-child relationship.

This happens once we pass in the this pointer and no processing is done in the custom Widget.

The subclass inherits the style of the parent window.

When you encounter this situation, there are two ways to deal with it: override the paintEvent function of the current window, and set it not to follow the style of the parent window.

For convenience, the second method is used when the background image drawn by the window is not complex:

This- > setAttribute (Qt::WA_StyledBackground)

After setting the above code in the current custom widget class constructor, the previous problem of setting the background style but not being seen is easily solved.

Explain knowledge point 3

How to achieve mutual exclusion between multiple widget?

Diggers who have used the QRadioButton control know that it only needs a simple setting function to set mutual exclusion.

For our custom widget, this function does not exist, and the mutex effect can only be manually set with the code and change the corresponding display effect according to the selected and unchecked states.

Suppose that the custom Widget of "content 1" is currently selected, and a message needs to be triggered in the mouse down response in the Widget to inform the outside world that the current custom Widget has done a press operation and needs to do special processing.

Void QCustomWidget::mousePressEvent (QMouseEvent * event) {this- > SetWidgetStyle (Style_Down); emit Msg_SendClicked (); QWidget::mousePressEvent (event);}

Do special handling in response to the corresponding slot function in the parent class that calls the custom Widget.

At this point, it is easy to implement the custom Widget mutex effect.

The implementation of mutex is very simple, and the most important thing to master is how to set the background of widget.

In many cases, this problem is most likely to occur when there are too many levels of nesting between the child window and the parent window, because the best way to create a new widget object is not to follow the style of the parent window every time.

The above is how the Qt custom Widget shared by the editor achieves the effect of mutual exclusion. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report