我不是高手
下面的代码楼主可以试试
//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重新编辑 ]