Basic control of QT software development -- 2.4.2 comboBox drop-down box

Keywords: Qt

2.4.2 comboBox drop-down box

QComboBo provides a drop-down option in the form of a list for users to display, saving screen space. The data in the drop-down combo box can be modified and edited, and can contain pictures and string text. The font selection box, font size selection box and color selection box in Word document software are implemented by ComboBo.

Create a QComboBo example

QComboBox *box=new QComboBox(this);
box->addItem("Option 1");
box->addItem("Option 2");

  Figure 2-4-3 QComboBo effect diagram

The following describes the common member functions of QComboBo

1. Add entry information

void QComboBox::addItem(const QString &text, const QVariant &userData = QVariant()) 
void QComboBox::addItem(const QIcon &icon, const QString &text, const QVariant &userData = QVariant())
void QComboBox::addItems(const QStringList &texts)

addItem is an overloaded function. You can add string text alone or add icons.

2. Return the number of all entries

int count() const

3. Set the icon size displayed in the option box

QSize	iconSize() const
void	setIconSize(const QSize & size)

4. Return the icon or string of the specified entry

QString QComboBox::itemText(int index) const
QIcon QComboBox::itemIcon(int index) const

5. Set the icon and text for the specified entry

 void QComboBox::setItemIcon(int index, const QIcon & icon)
 void QComboBox::setItemText(int index, const QString & text)

6. Set whether the option can be edited

bool isEditable() const
void setEditable(bool editable)

By default, this property is false and cannot be edited.

7. Set the text displayed in the current edit box

QString currentText() const  //Returns the string text of the current edit box
void setCurrentText(const QString &text)  //Sets the currently displayed string text

The premise of using the currentText property is that the current QComboBox must support the text property (setCurrentText(true)).

8. Set the maximum number of entries supported by QComboBox. The default is 2147483647

int maxCount() const
void setMaxCount(int max)

9. Set the display text and icon of the specified option

void setItemText(int index, const QString &text);
void setItemIcon(int index, const QIcon &icon);
void setItemData(int index, const QVariant &value, int role = Qt::UserRole);

int   Index represents the index value of the entry option. The index starts at 0.

10. Insert new entry options

void insertItem(int index, const QString &text, const QVariant &userData = QVariant());
void insertItem(int index, const QIcon &icon, const QString &text,const QVariant &userData = QVariant());
void insertItems(int index, const QStringList &texts);

int   Index represents the index value of the entry option.

QComboBox common slot function

void clear();

Clear all entry options

void clearEditText();

Clear text display

void setEditText(const QString &text);

Sets the text displayed in the option box

void setCurrentIndex(int index);

Sets the node index for the current option

void setCurrentText(const QString &text);

Sets the text for the current option

QComboBox common signals

1.editTextChanged signal: sent when the text of the edit box changes (QComboBox needs to be editable). The formal parameter saves the changed new text.

void editTextChanged(const QString &);

2.activated signal: sent when the user selects an option in the drop-down list box. Parameter saves the node index value or text of the selected option.

void activated(int index);
void activated(const QString &text)

Note: the activated signal will be sent even if the selected option has not changed (this selection is the same as the previous selection).

3.currentTextChanged signal: issued when the text of the edit box changes. The formal parameter saves the changed new text.

void currentTextChanged(const QString &text)

Note: this signal corresponds to the currentText property

4.currentIndexChanged signal: issued when an option is clicked. Parameter saves the node index value or text of the selected option.

void currentIndexChanged(int index)
void currentIndexChanged(const QString &text)

Note: if the option is not changed (this selection is the same as the previous selection), the currentIndexChanged signal will not be sent.

5.highlighted signal: when the cursor selects an option, it is sent out without clicking the option. Parameter saves the node index value or text of the selected option.

void highlighted(int index)
void highlighted(const QString &text)

The following is a simple application example of QComboBox (supporting program No. CH2-9).

1.widget.ui design interface

  Figure 2-4-4 UI design interface

2.widget.cpp code

#include "widget.h"
#include "ui_widget.h"
#include <QComboBox>
#include <QDebug>
#include <QMessageBox>
#include <QIcon>
Widget::Widget(QWidget *parent) :
    ui(new Ui::Widget)

    QStringList list;
    //Create machine status list
    ui->comboBox_state->addItem(QIcon(":/start.ico"),"Start running");
    ui->comboBox_state->addItem(QIcon(":/stop.ico"),"stop it");
    //Create a list of accounts to be selected
    ui->comboBox_number->addItems(list); //Add displayed entries
    ui->comboBox_number->setEditable(true); //Set options editable properties
    //Set password display mode

    delete ui;

//Start connection
void Widget::on_pushButton_connect_clicked()
   //In practical application, it can be encrypted through QCryptographicHash class
    QString password_info;
    password_info+=tr("account number:");
    QMessageBox::information(this, tr("login information"), password_info,QMessageBox::Ok);

3. Operation effect

Figure 2-4-5 example of qcombobox operation effect

Posted by HockeyDevil07 on Thu, 14 Oct 2021 12:27:49 -0700