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 realize Custom Item effect by QListWidget

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

Share

Shulou(Shulou.com)05/31 Report--

In this article Xiaobian for you to introduce in detail "QListWidget how to achieve custom Item effect", the content is detailed, the steps are clear, the details are handled properly, I hope this "QListWidget how to achieve custom Item effect" article can help you solve your doubts, the following follow the editor's ideas slowly in-depth, together to learn new knowledge.

Realize the effect

I don't think this is a very rare problem. I recently recruited a new graduate and found that I was perplexed when I realized the above effect. Did I get confused when I first came into contact with the slightly more difficult interface of Qt?

Therefore, I intend to share with you the ideas for implementation, as well as the problems that will arise, as far as I, an employee who has been developing C++ for five years, is concerned, what problems the novice will encounter.

Current development environment: win10 VS2017 + Qt5.14.2 x64

The difficulties that beginners will encounter in the process of implementation are as follows

1: how do I add a piece of data with buttons, text, and other controls to QListWidget?

2: how to respond when each one is selected? Why doesn't the item response that comes with QListWidget take effect?

3: how do I select the delete button and tell QListWidget to make a specific response?

Based on the above three questions, let's talk about the problem while implementing Item content with custom controls.

When inserting a piece of data using QListWidget, the default method is as follows:

Ui.listWidget- > insertItem (0, "Text Content"); / / method 1ui.listWidget-> addItem ("Text Content"); / / method 2

It is not possible to use the above code, because only strings of type QString can be added to the parameters, so how do you add customizations?

A subclass called QListWidgetItem is provided in the QListWidget class to implement a custom item.

At this point, we need to redefine a class and bind the class to ListWidgetItem, so that we can display our custom format on the item of each line.

The item shown in the example shows a selection box, file name, and delete button.

Here, it is realized by using two buttons, QCheckBox and QPushButton.

Some people will ask: should file names not be represented by QLabel controls?

The answer is: currently, you can use the QLabel control to display the file name. Here, QCheck is mainly used to display the icon of the file, which displays different icons according to different file name suffixes.

Custom Widget

This class inherits from QWidget. Suppose it is called: CustomItem

Some novices will directly create a pure C++ class, which must be problematic. When we use the current custom class externally, you will find out why the newly created classes are separated out.

If you use pure C++ classes directly, there is another crucial question, how do you pass it to external callers when the current class needs to communicate with each other? Callback? Isn't it a little overqualified?

Class CustomItem: public QWidget {Q_OBJECTpublic: CustomItem (QWidget * parent); ~ CustomItem (); private: QCheckBox* masked checkname; / / filename QCheckBox* masked checkSelect; / / Select QPushButton* mSecretbtnDelete; / / Delete}

Three control variables are created in the custom class CustomItem, which represent the selection box, the file name, and the delete button.

It is the article that begins to display the three controls of the rendering.

Next, you need to define an external calling interface and insert a piece of valid data, assuming that the interface name is: AddINewtemData

Class CustomItem: public QWidget {Q_OBJECTpublic: CustomItem (QWidget * parent); ~ CustomItem (); public: / / Open API void AddINewtemData (int nRow, QString qsFileName); / / add a new data private: int mSecretnRows; QCheckBox* masked checkname; / / attachment name QCheckBox* masked checkSelect; / / Select QPushButton* mSecretbtnDelete; / / delete}

AddINewtemData parameter

Parameter 1: represents the line number of the current custom widget that belongs to QListWidget, which is used for subsequent messaging.

Parameter 2: the name of the file to be displayed

Depending on the file name passed in by the user, different icons are displayed according to the file suffix.

Void CustomItem::AddINewtemData (int nRow, QString qPath) {m_nRow = nRow; / / record the line number of the QListWidget corresponding to the current custom widget / / get the file name according to the path name and set QFileInfo info (qPath); QString qsFileName = info.fileName (); masking checkname-> setText (qsFileName); / / get the file suffix QString qsCheckStyle = "" If (info.suffix () = = "mp4") / / Video file {/ / Custom QCheckBox style} else if (info.suffix () = = "png") / / Picture file {/ / Custom QCheckBox style} else if (info.suffix () = = "xlsx") / / tabular file {/ / Custom QCheckBox style} else if (info.suffix () = = "pdf") {/ / Custom QCheckBox style} else / / document file {/ / Custom QCheckBox style} masking checkname-> setStyleSheet (qsCheckStyle) }

After you have constructed the custom widget class, you need to bind the class to QListWidgetItem and display it to QListWidget.

Call the method externally as follows:

Int nCount = ui.listWidget- > count (); CustomItem* widget = new CustomItem (this); widget- > AddINewtemData (nCount, qsFileName); widget- > show (); QListWidgetItem* item = new QListWidgetItem;item- > setSizeHint (QSize (48,48)); ui.listWidget- > addItem (item); ui.listWidget- > setItemWidget (item, widget)

Custom CustomItem response

The above functions can implement the custom widget in QListWidget, how to click on each item in QListWidget to make a different response?

At this point, after we bind the custom class to each row of QListWigetItem, we cannot respond to the QListWidget's own selection message! We need to keep this in mind.

So, how do you trigger it?

For every QWidget class, as long as it is inherited from QWidget, there will be four responses from the mouse:

Virtual void mousePressEvent (QMouseEvent * event); virtual void mouseReleaseEvent (QMouseEvent * event); virtual void mouseDoubleClickEvent (QMouseEvent * event); virtual void mouseMoveEvent (QMouseEvent * event)

When the mouse has done the click effect in the custom class, you can certainly get the click response in the mouse down event.

Therefore, in the custom class CustomItem, you need to rewrite the system message of QWidget: mousePressEvent

Void CustomItem::mousePressEvent (QMouseEvent * event) {QWidget::mousePressEvent (event);}

When the breakpoint is set in the response function, it is sure to trigger, if it is not certain that the current widget is disabled or masked.

There is also a hidden question: when some students click on a custom window, they will find such a strange phenomenon: why do you click on some areas to respond to mousePressEvent messages while others do not?

The following is the top priority!

This is the content mentioned in the previous paragraph. When the current widget is not triggered, it must be disabled or blocked.

There are three active controls in our custom Widget, two QCheck and one QPushButton. When we click on any control, the click response should be to the child control, not to the custom Widget (CustomItem).

In order for the mouse to click on any control and all the responses are sent to the parent class Widget, that is, CustomItem, we should do special handling for the controls that support mouse response operations.

SetAttribute (Qt::WA_TransparentForMouseEvents)

Each control needs to set the above operations. The current control is only used for display and does not do any message processing. It is the message processing done by the current window.

Customize the Widget control response and notify the outside world for processing

Next, let's talk about the third key issue, how to inform the outside world to deal with it.

After we have used the custom Widget, we can no longer use the internal selected message of QListWidget. In order to let the external window get the message of internal Widget, we need to inform the outside world and simulate the QListWidget message by signaling.

Signals: void Msg_SendDeleteItemData

The external window manipulates the message directly, using the same method as the normal method, so I won't talk too much about the message mechanism in Qt here.

Read this, the "QListWidget how to achieve custom Item effect" article has been introduced, want to master the knowledge of this article also need to practice and use in order to understand, if you want to know more about the article, 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