实现简单的QComboBox的功能
1.新建一个无ui页面的工程
基类为QMainWindow 步骤省略
2.代码实现
mainwindow.h
#include
<QMainWindow
>
#include
<QComboBox
>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget
*parent
= 0);
~MainWindow();
public slots
:
void onChanged(int index
);
private:
QComboBox
* comBox
;
};
mainwindow.cpp
#include
"mainwindow.h"
#include
<QMessageBox
>
MainWindow
::MainWindow(QWidget
*parent
)
: QMainWindow(parent
)
{
comBox
= new QComboBox(this);
comBox
->setGeometry(50,70,100,30);
comBox
->addItem("第一页");
comBox
->addItem("第二页");
connect(comBox
,SIGNAL(currentIndexChanged(int
)),
this,SLOT(onChanged(int
)));
this->resize(200,200);
this->setWindowTitle("ComboBox_demo");
}
MainWindow
::~MainWindow()
{
}
void MainWindow
::onChanged(int index
)
{
QMessageBox
::warning(this,"消息",comBox
->itemText(index
),
QMessageBox
::Ok
);
}
3.效果展示
转载请注明原文地址: https://win8.8miu.com/read-1232.html