当前位置:网站首页>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)));边栏推荐
- Example of using listagg in Oracle 11g (splicing the values of a field into a line, separated by commas)
- Docker install mysql, hello world
- It operation and maintenance knowledge map
- Master data management system
- LNMP environment setup
- (9) Sequential queue and stack queue
- MySQL view
- MySQL backup - mysqldump
- Expérience électrique en mode - - expérience 2 circuit d'amplification de source commune JFET
- Mt4/mql4 getting started to mastering EA tutorial lesson 5 - common functions of MQL language (V) - common functions of account information
猜你喜欢
随机推荐
IP address planning
Problems caused by canvas palette width and height and canvas width and height
lr 2022超详细安装教程「最新」
ES6 set data type de duplication of array, intersection, union and difference
Difference between ID instancetype nsobject *
Charles uses
The applet uses the step bar vant steps in vant
How Navicat queries the password information of the connected database
(8) Sequence stack and chain stack
JSON使用示例
[Oracle database] wet nurse tutorial day15 DDL, DML, index, view, sequence and deadlock are enough
代码覆盖率测试对编程小白的意义及其使用方法
Note pad replaces all contents after a character in all lines
Chmod Chmod command
MySQL index
数据可视化优秀案例
【Oracle 数据库】奶妈式教程 day14 转换函数
Invalid applet margin right, text overflow?
【 Oracle database】 Nursery Mother Tutorial day13 date Function
Microsoft Remote Desktop 10.7.6 official





![[普通物理]波的能量与干涉](/img/fe/066aa9e8ed776b8f069b59b7123367.png)



