Qt入门-下拉列表框QComboBox类
来源:互联网 发布:985 211知乎 编辑:程序博客网 时间:2024/06/05 22:57
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项的文本。
示例:
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_OBJECTpublic: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();}
编译运行,界面如下:
- Qt入门-下拉列表框QComboBox类
- Qt入门-下拉列表框QComboBox类
- Qt入门-下拉列表框QComboBox类
- Qt-下拉列表框QComboBox类
- Qt 之-下拉列表框QComboBox类
- 下拉列表框QComboBox类
- Qt QComboBox下拉框高度固定问题
- Qt QComboBox自定义的下拉框
- Qt基础学习(4)-----下拉列表框(组合框)之QComboBox
- Qt基础学习-----下拉列表框(组合框)之QComboBox
- QT QComboBox的下拉列表项设置高度
- [Qt]QCombobox下拉列表文字显示一半的问题
- QComboBox下拉列表自动填充
- Qt5学习笔记(6)——下拉列表框QComboBox类
- QComboBox下拉列表添加复选框及消息处理
- QComboBox下拉列表添加复选框及消息处理
- QComboBox下拉列表添加复选框及消息处理 .
- Qt - 自定义QComboBox下拉框的item高度
- SQL Server中行列转换 Pivot UnPivot
- 傻逼的老板,苦逼的程序员
- Useful宏定义
- Hibernate4实战 之第五部分:Hibernate的事务和并发
- select error:不能用DB-library(如isql)或odbc3.7或更早版本将ntext数据或仅使用Unicode排序规则的Unicode数据发送到客户端
- Qt入门-下拉列表框QComboBox类
- 自己动手写CSDN博客提取器源码分析之一:处理网页保存为txt文件
- 获取 主机的信息
- 正则表达式语法
- Silverlight 4常用StringFormat格式总结
- python数字转成字符串
- i2c对比spi-帧结构
- jquery的函数属性
- Ubuntu 12.04 LTS游戏安装与使用