Qt入门-下拉列表框QComboBox类

  QComboBox是QT GUI中的下拉列表框。

[cpp]  view plain copy
  1. class Q_GUI_EXPORT QComboBox : public QWidget  
  2. {  
  3.     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项的文本。


示例:

Window.h

[cpp]  view plain copy
  1. #ifndef __WINDOW_H__  
  2. #define __WINDOW_H__  
  3.   
  4. #include <QMainWindow>  
  5. #include <QPushButton>  
  6. #include <QLineEdit>  
  7. #include <QLayout>  
  8. #include <QLabel>  
  9. #include <QComboBox>  
  10. #include <QMessageBox>  
  11. #include <QDialog>  
  12.   
  13.   
  14. class Window : public QMainWindow  
  15. {  
  16.     Q_OBJECT  
  17.   
  18. public:  
  19.     Window(QWidget *parent = NULL):QMainWindow(parent)  
  20.     {  
  21.         QGridLayout *gridLayout = new QGridLayout;  
  22.         gridLayout->setColumnStretch(0, 1);  
  23.         gridLayout->setColumnStretch(1, 3);  
  24.   
  25.         gridLayout->setMargin(10);  
  26.   
  27.         QLabel *lbl_caption = new QLabel(QWidget::tr("Sex:"));  
  28.         cbo_sex = new QComboBox();  
  29.   
  30.         cbo_sex->addItem(QWidget::tr("male"));  
  31.         cbo_sex->addItem(QWidget::tr("female"));  
  32.         cbo_sex->insertItem(2, tr("Insert item"));  
  33.         cbo_sex->insertSeparator(2);  
  34.   
  35.         gridLayout->addWidget(lbl_caption, 0, 0);  
  36.         gridLayout->addWidget(cbo_sex, 0, 1);  
  37.   
  38.         QHBoxLayout *bomLayout = new QHBoxLayout;  
  39.         QPushButton *btn = new QPushButton(QWidget::tr("Select"));  
  40.         bomLayout->addStretch();  
  41.         bomLayout->addWidget(btn);  
  42.         bomLayout->addStretch();  
  43.   
  44.         QVBoxLayout *mainLayout = new QVBoxLayout;  
  45.         mainLayout->addLayout(gridLayout);  
  46.         mainLayout->addLayout(bomLayout);  
  47.           
  48.         QWidget *mainWidget = new QWidget;  
  49.         mainWidget->setLayout(mainLayout);  
  50.   
  51.         setCentralWidget(mainWidget);  
  52.   
  53.         connect(cbo_sex, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(on_sel_sex(const QString &)));  
  54.         connect(btn, SIGNAL(clicked()), this, SLOT(on_click_sel()));  
  55.     }  
  56.   
  57. private:  
  58.     QComboBox *cbo_sex;  
  59.   
  60. private slots:  
  61.     void on_sel_sex(const QString &text)  
  62.     {  
  63.         QString str;  
  64.         str = "You select " + text;  
  65.         QMessageBox::information(this, tr("Info"), str);  
  66.     }  
  67.   
  68.     void on_click_sel()  
  69.     {  
  70.         QString str;  
  71.         str = "You select " + cbo_sex->currentText();  
  72.         QMessageBox::information(this, tr("Info"), str);  
  73.     }  
  74.   
  75. };  
  76.   
  77.   
  78. #endif  


main.cpp

[cpp]  view plain copy
  1. #include <QApplication>  
  2. #include <QDialog>  
  3. #include "Window.h"  
  4.   
  5.   
  6.   
  7. int main(int argc, char *argv[])  
  8. {  
  9.     QApplication a(argc, argv);  
  10.     Window *mainWindow = new Window;  
  11.   
  12.   
  13.   
  14.     mainWindow->resize(200, 100);  
  15.     mainWindow->setWindowTitle(QWidget::tr("Qt Test"));  
  16.     mainWindow->show();  
  17.   
  18.     return a.exec();  
  19. }  


编译运行,界面如下:


FROM: http://blog.csdn.net/xgbing/article/details/7767530

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值