Qt入門-下拉列表框QComboBox類

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

  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

  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

  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. }  


編譯運行,界面如下:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章