当前位置:网站首页>Example of QT combox

Example of QT combox

2022-06-22 08:03:00 Licht powder

QComboBox yes Drop down list box component class , It provides a drop-down list for users to choose , It can also be used directly as a QLineEdit As input .QComboBox In addition to displaying the visible drop-down list , Each item (item, List or item ) You can also associate a QVariant Variable of type , Used to store some invisible data . As shown in the figure is a combox:

Usage method :

1—— Add dropdown options

    // Mode one 
    ui->comboBox->addItem("a");
    ui->comboBox->addItem("b");
    ui->comboBox->addItem("c");
    ui->comboBox->addItem("d");
    // Mode two 
    QStringList strList;
    strList<<"a"<<"b"<<"c"<<"d";
    ui->comboBox->addItems(strList);

The effect is as follows :

1-1—— Add drop-down options with icons

     // Mode one 
     ui->comboBox->setIconSize(QSize(32,32));
     ui->comboBox->addItem(QIcon("C:/Users/richard/Desktop/demo1/demo1/123.jpg"),"123");

The effect is as follows :

Mode two :void setItemIcon(int index, const QIcon &icon)

    ui->comboBox->addItem("a");
    ui->comboBox->addItem("b");
    ui->comboBox->addItem("c");
    ui->comboBox->addItem("d");

    ui->comboBox->setSizeAdjustPolicy(QComboBox::AdjustToContents);
    QIcon icon("C:/Users/richard/Desktop/demo1/demo1/123.jpg");
    ui->comboBox->setIconSize(QSize(32,32));
    ui->comboBox->setItemIcon(1,icon);

  

Add : Add an item with user data QComboBox::addltem() Of two parameters of a function Prototype definition as follows :

void addItem (const QString &text, const QVariant &userData = QVariant())
void addItem (const QIcon &icon, const QString &text, const QVariant &userData = QVariant())

No matter which one addItem() function , There is an optional... In the back QVariant Parameters of type userData, You can use this variable to store user-defined data

2——item Carry custom data

  • QVariant currentData(int role = Qt::UserRole): Returns the associated data of the current item , The default role of data is role = Qt::UserRole. notes : About setRole An example of storing data can be seen here
  • QString itemText(int index): Returns the text of the item with the specified index number .
  • QVariant itemData(int index, int role = Qt%:UserRole): Returns the associated data of the item with the specified index number .
struct Student
{
    QString name;
    int age;
};
// Declare custom types , bring Qt Can find the definition of this type 
Q_DECLARE_METATYPE(Student);

When using :

QVariant useVar;
Student s1 = {" Xiao Ming ",20};
useVar.setValue(s1);
ui->comboBox->addItem(" Xiao Ming ",useVar);


// Slot function 
QVariant useVar = ui->comboBox->itemData(index);
// Judge whether there is specified data before 
if(useVar == QVariant::Invalid)
{
    return;
}
 
qDebug() << useVar.value<Student>().name << ":" << useVar.value<Student>().age;

3—— Common function settings

//1  The default setting is the subscript number 
ui->comboBox->setCurrentIndex(2);

//2  get combox Total number of indexes for the control 
int iSum = ui->comboBox->count();

//3  Get the current index number 
int index = ui->comboBox->currentIndex();

//4  Get the current text content 
ui->comboBox->currentText()

// Clear list 
ui->comboBox->clear()

// Open editable properties 
ui->comboBox->setEditable(true);

// Set the currently displayed content , Remember to use it with editable properties 
ui->comboBox->setCurrentText(QString);

notes : After opening editable properties , It can also be done by setValidator Interface defines its input format , adopt setCompleter Interface setting input automatic completion .

What needs to be explained here is , By default ,QComboBox The editing mode of It does not delete or change existing item, Instead, the user Press enter , Add the content you just edited at the top or bottom . If you want to change the default properties , Can pass setInsertPolicy Modify the interface .             
 

QComboBox::NoInsert The string will not be inserted into the combo box
QComboBox::InsertAtTop     The string will be inserted as the first item in the combo box
QComboBox::InsertAtCurrent The current item will be replaced by a string
QComboBox::InsertAtBottom   The string will be inserted after the last item in the combo box
QComboBox::InsertAfterCurrent   Insert a string after the current item in the combo box
QComboBox::InsertBeforeCurrent Inserts a string before the current item in the combo box
QComboBox::InsertAlphabetically Insert strings alphabetically into the combo box

4—— The signal

// When a list item is clicked , Will be issued activated The signal , Pass on index or text Parameter to the slot function corresponding to the signal .
void activated(int index)
void activated(const QString &text)

// When the selection changes , Will be issued currentIndexChanged The signal , Pass on index or text Parameter to the slot function corresponding to the signal .
void currentIndexChanged(int index)
void currentIndexChanged(const QString &text)
void currentTextChanged(const QString &text)

// When the list text changes ( Need to open editable properties ), It triggers editTextChanged The signal , Pass the changed text to the slot function corresponding to the signal .
void editTextChanged(const QString &text)


// When a list item is highlighted ( For example, place the mouse over the list item ), Will be issued highlighted The signal , Pass on index or text Parameter to the slot function corresponding to the signal .
void highlighted(int index)
void highlighted(const QString &text)

give an example : Signal to slot binding

connect(ui->comboBox,SIGNAL(activated(int)),this,SLOT(slotActivated(int)));

原网站

版权声明
本文为[Licht powder]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220800101370.html