QComboBox text and a drop-down center

Let QComboBox text is center, the thought is very simple, the results engage in an hour or so, record it.

This feature is actually not too difficult, if you like to write some steps, such as rewriting class and other more "trouble" can be easy to do, but obviously a seemingly simple function of non-engage in such a complex, a little less able to accept, so engage in rivalry for a while, I think about the record or to prevent the re-encounter this problem.

QComboBox achieve this text is center, it means custom QComboBox, but simpler, but the idea is the same.

QComboBox total is divided into four parts, one by one we achieved in accordance with the effect to be achieved:

1 shows part of the currently selected item: You canQComboBox :: setLineEdit void ( QLineEdit * Edit) function, set a self-defined line editor to replace the existing display controls, so we only need to customize this QLineEdit it.

2. The drop-down list: When we choose paragraph, will expand the drop-down list, you can :: setView (by QComboBox void QAbstractItemView * ItemView) function, replace the existing use of a View drop-down list, which we need to customize or beautify this view. (Note that if the function setView convenient to use a Widget class, then the first set the Widget-related Model, see sample code below)

The view of the landscaping, in addition to Qt QSS characteristics to provide their own outside, we can also set Delegate way to complete their rendering of the item, high degree of freedom.

3. The drop-down box upper right corner: this section may be provided by QSS, i.e. sub-set QComboBox drop-down control, the child control is default QComboBox right side is rectangular, there are internal drop-down sub-control a down-arrow, devoted to mapping the drop-down button, the default is located in the upper right corner of the drop-down.

4. The drop-down button: setting a sub control QComboBox down-arrow by QSS.

 

After thinking there, very simple, even if some are not familiar with QSS, you can also find some examples of reference, Zhaomaohuahu can.

The following is the text centered Code:

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    QStringList items;
    items << "offline" << "student" << "teacher" << "manager";
    QListWidget *listWidget = new QListWidget(this);
    for(int i = 0;i < items.count();++i)
    {
        QListWidgetItem *item = new QListWidgetItem(items.at(i));
        item->setTextAlignment(Qt::AlignCenter);
        listWidget->addItem(item);
    }

    ui->comboBox->setModel(listWidget->model());
    ui->comboBox->setView(listWidget);

    QLineEdit *lineEdit = new QLineEdit;
    lineEdit->setReadOnly(true);
    lineEdit->setAlignment(Qt::AlignCenter);
    ui->comboBox->setLineEdit(lineEdit);

    ui->comboBox->setStyleSheet("QComboBox {"
                                "color:black;"
                                "background-color:white;"
                                "outline:0px;"
                                "border:0px;"
                                "}"
                                "QComboBox::down-arrow{"
                                "right:3px;"
                                "image:url('subscriptLogin.png');"
                                "}"
                                "QComboBox:drop-down {"
                                "outline:0px;"
                                "border:0px;"
                                " }"
                                );
}
 

 

 
H&A
Released seven original articles · won praise 4 · Views 836

Guess you like

Origin blog.csdn.net/qq_34305316/article/details/96488636
Recommended