cat testdlg.h
#ifndef _TEST_DLG_H
#define _TEST_DLG_H
#include <QDialog>
class QComboBox;
class TestDlg : public QDialog
{
Q_OBJECT
public:
TestDlg(QWidget *parent=0);
private slots:
void slotComboChanged(int i);
private:
QComboBox * m_comboBox;
};
#endif
cat testdlg.cpp
#include <QtGui>
#include "testdlg.h"
TestDlg::TestDlg(QWidget *parent)
:QDialog(parent)
{
this->setWindowTitle(tr("监控系统"));
m_comboBox = new QComboBox(this);
m_comboBox->addItem("line 1", 1);
m_comboBox->addItem("line 2", 3);
m_comboBox->addItem("line 3", 5);
connect(m_comboBox,SIGNAL(currentIndexChanged(int)),this,SLOT(slotComboChanged(int)));
QHBoxLayout* layout = new QHBoxLayout();
layout->addWidget(m_comboBox);
this->setLayout(layout);
}
void TestDlg::slotComboChanged(int i)
{
QString content=m_comboBox->itemText(i);
QVariant data=m_comboBox->itemData(i);
// qDebug() << content << data;
qDebug() << content << data.toInt();
}
cat main.cpp
#include <QtGui>
#include "testdlg.h"
int main(int argc, char *argv[])
{
QTextCodec *codec=QTextCodec::codecForName("utf-8");
QTextCodec::setCodecForLocale(codec);
QTextCodec::setCodecForTr(codec);
QApplication a(argc,argv);
QFont font= a.font();
font.setPointSize(16);
a.setFont(font);
TestDlg dlg;
dlg.show();
return a.exec();
}
执行结果: (选择comboBox, 打印输出)./test2
"line 2" 3
"line 3" 5
"line 1" 1