zoukankan      html  css  js  c++  java
  • 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
     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 }

    编译运行,界面如下:

    注:

    另外一种简单的添加item的方法

    双击QComboBox控件结果如下

    在此可以添加任意想要的item,添加完点击OK即可。

  • 相关阅读:
    [转]Understanding Integration Services Package Configurations
    [转]SSIS: Execute Package via Stored Procedure
    [转]Loading and Running a Local Package Programmatically
    [转]Mapping Stored Procedure Parameters in SSIS OLE DB Source Editor
    [转]Data Flow How-to Topics (SSIS)
    [转]Advanced Oracle SQL Developer Features
    [转]SSIS包的调用方式
    [转]oracle 11g 忘记 默认用户密码
    [转]Insert, Update, and Delete Destination table with SSIS
    [转]JS判断访问设备、客户端操作系统类型
  • 原文地址:https://www.cnblogs.com/zhouwenJS/p/3769837.html
Copyright © 2011-2022 走看看