Qt基础学习(4)-----下拉列表框(组合框)之QComboBox

//mydialog.h
#ifndef MYDIALOG_H
#define MYDIALOG_H
#include <QDialog>
class QComboBox;
class MyDialog : public QDialog
{
  Q_OBJECT
public:
  explicit MyDialog(QWidget *parent = 0);
signals:
public slots:
  void onChanged(int index);
private:
  QComboBox *comboBox;
};
#endif // MYDIALOG_H

//mydialog.cpp
#include "mydialog.h"
#include <QtGui/QComboBox>
#include <QtGui/QMessageBox>
#include <QtGui/QIcon>
MyDialog::MyDialog(QWidget *parent) :
  QDialog(parent)
{
  comboBox = new QComboBox(this);
  comboBox->resize(100, 20);
  QIcon icon1(":/new/ico/images/ftp.ico");
  comboBox->addItem(icon1, "ftp");

  QIcon icon2(":/new/ico/images/www.ico");
  comboBox->addItem("www");
  //设置列表框中选项的图标
  comboBox->setItemIcon(1, icon2);
  //当下拉列表框中的项重新选择时 产生一个currentIndexChanged(int index)信号 弹出一个消息框提示你重新选择了哪一项
  connect(comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onChanged(int)));
  this->resize(100, 100);
  this->setWindowTitle("QComboBoxDemo");
}
void MyDialog::onChanged(int index)
{
  QMessageBox::warning(this, "Message", comboBox->itemText(index), QMessageBox::Ok);
}


//main.cpp

#include <QtGui/QApplication>

#include "mydialog.h"

int main(int argc, char *argv[])

{

  QApplication a(argc, argv);

  MyDialog dialog;

  dialog.show();

  return a.exec();

}


资源文件截图:

Qt基础学习(4)-----下拉列表框(组合框)之QComboBox

程序运行截图:

Qt基础学习(4)-----下拉列表框(组合框)之QComboBox

你可能感兴趣的:(combobox)