• 5313阅读
  • 1回复

[讨论]小弟想问各位大侠一个关于QComboBox的问题? [复制链接]

上一主题 下一主题
离线青春岁月
 

只看楼主 倒序阅读 楼主  发表于: 2011-03-11
我建立了一个ComboBox,我想建立几个item?我想实现这样的效果?
我按一下PUSHBUTTON 加一个item,并显示这个item的名字
然后再按一下PUSHBUTTON,再加一个item,并显示这个item的名字

求高手给力!
go!   go!  go!  go!
离线83888788
只看该作者 1楼 发表于: 2011-03-12
我不是高手
下面的代码楼主可以试试

//example.h


#ifndef EXAMPLE_H
#define EXAMPLE_H

#include<QtGui>


class Example:public QWidget
{
    Q_OBJECT
public:
    Example(QWidget *parent=0);
protected slots:
    void slotAddAnItem();
private:
    QComboBox *comboBox;
    QPushButton *addButton;
    QLabel *showLable;
};

#endif // EXAMPLE_H


//example.cpp


#include"example.h"

Example::Example(QWidget *parent):QWidget(parent)
{
    comboBox=new QComboBox(this);
    addButton=new QPushButton(tr("add"));
    showLable=new QLabel;
    connect(addButton,SIGNAL(clicked()),this,SLOT(slotAddAnItem()));

    QHBoxLayout *mainLayout =new QHBoxLayout(this);
    mainLayout->addWidget(comboBox);
    mainLayout->addWidget(addButton);
    mainLayout->addWidget(showLable);
}


void Example::slotAddAnItem()
{
    this->comboBox->addItem(QString::number(comboBox->count()));
    comboBox->setCurrentIndex(comboBox->count()-1);
    showLable->setText(comboBox->currentText());
}


//main.cpp

#include<QtGui>
#include"example.h"

int main(int argc,char* argv[])
{
    QApplication app(argc,argv);

    Example e;
    e.show();

    return app.exec();
}

最后建议楼主还是找个书看看,论坛就很多啊!
[ 此帖被83888788在2011-03-12 01:54重新编辑 ]
快速回复
限100 字节
 
上一个 下一个