Quick development of QCombox and business style customization and qcombox style Customization

Source: Internet
Author: User

Quick development of QCombox and business style customization and qcombox style Customization

This is my personal summary in the project practice. I am writing in a rush. Some of the things are not necessarily accurate. Some of them are inferred by myself. I hope you will give me more advice and comment.

If you do not need to customize QCombox, writing the UI is very simple.

Create an instance: QComboBox * m_pMicrophoneCombox = new QComboBox;

I use the QSS Writing Style. Here I use a naming function: m_pMicrophoneCombox-> setObjectName ("DeviceCombox ");

If you use setstylesheet, you can simply write it in the code.

The following describes several attribute functions:

M_pMicrophoneCombox-> setMaxVisibleItems (2); // The maximum visible area is two.

M_pMicrophoneCombox-> setEditable (false); // You can edit QComboBox.

M_pMicrophoneCombox-> setFocusPolicy (Qt: NoFocus); // set QCombox to have no focus.

M_pMicrophoneCombox-> setLineEdit (edit); // edit is a lineedit, that is, the LineEdit of QCombox can be customized, and the top of the source code of QT is also a LineEdit.

M_pMicrophoneCombox-> setCurrentIndex (0); // you can specify the currently selected index.

M_pMicrophoneCombox-> setCurrentText (TR_TALK_DEVICE_COMBOX); // set the current display text of QCombox, which is encapsulated by a macro and can be modified later.

M_pMicrophoneCombox-> setItemData (I, list [I]); // QCombox can also input a linked list and define indexes by yourself. Of course, you want to upload a piece of data, and define the index.

QListWidget * pListWidget = new QListWidget (); // an instance of a new widget.

M_pMicrophoneCombox-> setModel (pListWidget-> model (); // Insert the model into the widget.
M_pMicrophoneCombox-> setView (pListWidget); // load the widget into Combox.

M_pMicrophoneCombox-> count (); // used to calculate the size of the combox drop-down form.

M_pMicrophoneCombox-> addItems (list); // insert a list.

M_pMicrophoneCombox-> addItem ("111"); // insert a single data record.

The above functions basically meet the business requirements. If the interactive style is complicated and requires custom style, We need to write the style of each item on our own.

The source code of QCombox is analyzed. In fact, its implementation is a LineEdit and a ListWidget. If you know this, you can re-write a ListWidget.

Item * pItem = new Item (); // item is a custom layout. The custom style of each item can be written in this layout.
QListWidgetItem * pListWidgetItem = new QListWidgetItem (list [I], pListWidget); // I am inserting the data of the linked list into QCombox.
PItem-> setItemData (list [I]); // insert data into each item
PListWidget-> setItemWidget (pListWidgetItem, pItem); // In my understanding about this function, I will overwrite one layer at the bottom layer.

Then write the hover and pressed mouse events in the QSS style, and there will be mouse interaction.

This method is feasible, but the data is not separated from the UI and is stirred up in a group.

The best solution is:

Since QT's combox supports listwidget, The listwidget is customized as a whole.

Write the layout in itemwidget, create a delegate, manage the UI in the proxy, and operate the data in the model. The model here may encapsulate a layer by itself to meet the interaction requirements of combox.

PListWidget-> m_pModel-> Append (list [I]); // inserts each data into the model, and the remaining data operations are managed by the model.

Note: At this time, the native interfaces of qt do not take effect, for example, setcurrenttext and setcurrentindex. You can view the source code and find that they are controlled by indexes. In fact, we use our own model

The data to be managed does not generate an index to satisfy the data managed by combox. Therefore, these external interfaces may also be encapsulated by themselves. It can be said that they basically gave up combox, assemble LineEdit and ListWidget by yourself.

In fact, the amount of code is large, and the company's business can be encapsulated into controls for convenient development.

Finally, if you have a better solution, please comment.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.