QComboBox插件是一个集按钮和下拉菜单于一体的插件。
QComboBox在占用最小屏幕空间的情况下为用户提供一个下拉菜单选项。
QComboBox在显示当前选项的同时还弹出一系列可选择的项目。用户也可以编辑ComboBox。
ComboBox可以包含pixmap和字符串(insertItem和setItemText)。对于可编辑的ComboBox,clearEditText可以被用来清除用于显示的字符串而不改变ComboBox的内容。
当ComboBox的当前值改变时可以发出两个信号(currentIndexChanged和activated)。不管是否在程序中预先设定或存在用户交互,currentIndexChanged总会被激发,而activated只有在存在用户交互的时候才会被激发。highlighted信号当用户点亮comboBox下拉菜单时被激发。这三种信号都存在QString和int两个版本。如果用户选择或点击一个pixmap,只有int信号会被激发。当一个可编辑的combobox的出现改变的时候,editTextChanged信号就会被激发。
当用户往一个可编辑的combobox里输入一个新的字符串的时候,combobox插件可以将其插进去或不插进去,而且插件还可以把它插到很多位置。默认的规则是AtBottom,但也可以用setInsertPolicy改变规则。
可以通过QValidator来限制可编辑combobox的输入(setValidator)。默认情况下,所有出入都可以接受。
ComboBox可以用insertItem来添加项目。项目可以用setIemText来改变。一个项目可以用removeItem移除,所有项目可以用clear一起移除。当前项目的内容可以用currentText返回,数字项目的内容可以用text返回。当前项目可以用setCurrentIndex设置。comboBox项目数可以用count返回;项目数的最大值可以用setMaxCount来设置。可以用setEditable使项目可编辑。对于可编辑的combobox,可以用setCompleter来设置自动完成,用户是否可以添加副本可以用过setDuplicatesEnabled来设置。
QComboBox使用model/view架构来显示下拉列表和存储项目。默认情况下,QStandardItemModel存储项目,QLIstView子类显示下拉列表。用户可以访问model和view,但QComboBox也提供了函数来设置和取得数据(setItemData和itemText)。也可以设置一个新的model和view。对于combobox标签的文字和标志来说,使用了拥有Qt::DisplayRole和Qt::DecorationRole的model的数据。
QComboBox是QT GUI中的下拉列表框。
- class Q_GUI_EXPORT QComboBox : public QWidget
- {
- Q_OBJECT
常用方法和属性:
(1)addItems
void addItems ( const QStringList & texts )在QComboBox的最后添加一项。
(2)count
int count () const返回列表项总数。
(3)currentIndex
int currentIndex () const当前显示的列表项序号。
(4)currentText
QString currentText () const返回当前显示的文本。
(5)insertItem
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 & list )
插入一项或多项至序号index处。
(6)insertSeparator
在序号为index的项前插入分隔线
(7)setItemText
改变序号为index项的文本。
(8)itemText
返回相应的组的文本
示例:
Window.h
- #ifndef __WINDOW_H__
- #define __WINDOW_H__
- #include <QMainWindow>
- #include <QPushButton>
- #include <QLineEdit>
- #include <QLayout>
- #include <QLabel>
- #include <QComboBox>
- #include <QMessageBox>
- #include <QDialog>
- class Window : public QMainWindow
- {
- Q_OBJECT
- public:
- Window(QWidget *parent = NULL):QMainWindow(parent)
- {
- QGridLayout *gridLayout = new QGridLayout;
- gridLayout->setColumnStretch(0, 1);
- gridLayout->setColumnStretch(1, 3);
- gridLayout->setMargin(10);
- QLabel *lbl_caption = new QLabel(QWidget::tr("Sex:"));
- cbo_sex = new QComboBox();
- cbo_sex->addItem(QWidget::tr("male"));
- cbo_sex->addItem(QWidget::tr("female"));
- cbo_sex->insertItem(2, tr("Insert item"));
- cbo_sex->insertSeparator(2);
- gridLayout->addWidget(lbl_caption, 0, 0);
- gridLayout->addWidget(cbo_sex, 0, 1);
- QHBoxLayout *bomLayout = new QHBoxLayout;
- QPushButton *btn = new QPushButton(QWidget::tr("Select"));
- bomLayout->addStretch();
- bomLayout->addWidget(btn);
- bomLayout->addStretch();
- QVBoxLayout *mainLayout = new QVBoxLayout;
- mainLayout->addLayout(gridLayout);
- mainLayout->addLayout(bomLayout);
- QWidget *mainWidget = new QWidget;
- mainWidget->setLayout(mainLayout);
- setCentralWidget(mainWidget);
- connect(cbo_sex, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(on_sel_sex(const QString &)));
- connect(btn, SIGNAL(clicked()), this, SLOT(on_click_sel()));
- }
- private:
- QComboBox *cbo_sex;
- private slots:
- void on_sel_sex(const QString &text)
- {
- QString str;
- str = "You select " + text;
- QMessageBox::information(this, tr("Info"), str);
- }
- void on_click_sel()
- {
- QString str;
- str = "You select " + cbo_sex->currentText();
- QMessageBox::information(this, tr("Info"), str);
- }
- };
- #endif
main.cpp
- #include <QApplication>
- #include <QDialog>
- #include "Window.h"
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- Window *mainWindow = new Window;
- mainWindow->resize(200, 100);
- mainWindow->setWindowTitle(QWidget::tr("Qt Test"));
- mainWindow->show();
- return a.exec();
- }
编译运行,界面如下:
本文详细介绍了QComboBox插件在Qt GUI中的使用方法,包括添加项目、编辑项目、显示下拉菜单等功能,并通过示例展示了如何在窗口中实现性别选择功能。
1万+

被折叠的 条评论
为什么被折叠?



