Qt 之-下拉列表框QComboBox类
2014-05-13 16:43
225 查看
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中的下拉列表框。
[cpp] view
plaincopy
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
void insertSeparator ( int index )
在序号为index的项前插入分隔线
(7)setItemText
void setItemText ( int index, const QString & text )
改变序号为index项的文本。
(8)itemText
返回相应的组的文本
示例:
Window.h
[cpp] view
plaincopy
#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
[cpp] view
plaincopy
#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在占用最小屏幕空间的情况下为用户提供一个下拉菜单选项。
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中的下拉列表框。
[cpp] view
plaincopy
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
void insertSeparator ( int index )
在序号为index的项前插入分隔线
(7)setItemText
void setItemText ( int index, const QString & text )
改变序号为index项的文本。
(8)itemText
返回相应的组的文本
示例:
Window.h
[cpp] view
plaincopy
#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
[cpp] view
plaincopy
#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();
}
编译运行,界面如下:
相关文章推荐
- 初次偿试开源项目开发---初偿Qt项目开发
- QT主窗口的停靠窗口实现
- 2D简单绘图---QT
- Ubuntu QT显示中文
- Qt4_VS10 程序打包发布
- 【Qt编程】基于Qt的词典开发系列<六>--界面美化设计
- centos 安装Qt-----出现 ERROR: Package gstreamer-app-0.10 not found 问题的解决
- Qt之事件系统
- Qt编写OpenMP程序--HelloWorld
- C++(qt)游戏实战项目:坦克大战(五)
- Qt中建立DockWidget的代码
- qt 安装错误 undefined reference to "FcFreeTypeQueryFace
- QT配置文件入门
- msys2、mingw-w64、GCC 4.9.2 、gdb-7.8.1、Qt 4.8.6、qtcreator 3.2.2 绿色版开发环境 -- 支持32位和64位
- QT 实现彩色图亮度均衡,RGB和HSI空间互相转换
- QT中的调试技术
- QT读取文本中文
- OpenCV【2】---读取png图片显示到QT label上的问题
- VS_QT中配置qDebug输出
- QT5 中的信号与槽新的写法