Qt comboBox的簡單應用


一顯示效果


二編輯界面如下


三 在mainwindow.h中聲明槽函數

 1 #ifndef MAINWINDOW_H  2 #define MAINWINDOW_H
 3  
 4 #include <QMainWindow>
 5 #include <QComboBox>
 6  
 7 namespace Ui {  8 class MainWindow;  9 } 10  
11 class MainWindow : public QMainWindow 12 { 13  Q_OBJECT 14  
15 public: 16     explicit MainWindow(QWidget *parent = 0); 17     ~MainWindow(); 18     //聲明槽函數print_s();
19 public slots: 20     void print_s(); 21  
22 private: 23     Ui::MainWindow *ui; 24 }; 25  
26 #endif // MAINWINDOW_H

四 main.c中的程序不需改動

 1 #include "mainwindow.h"
 2 #include <QApplication>
 3  
 4 int main(int argc, char *argv[])  5 {  6  QApplication a(argc, argv);  7  MainWindow w;  8  w.show();  9  
10     return a.exec(); 11 }

五 在mainwindow.c中編寫槽函數及連接

 1 #include "mainwindow.h"
 2 #include "ui_mainwindow.h"
 3  
 4 MainWindow::MainWindow(QWidget *parent) :  5  QMainWindow(parent),  6     ui(new Ui::MainWindow)  7 {  8     ui->setupUi(this);  9     connect(ui->comboBox,SIGNAL(currentIndexChanged(QString)),this,SLOT(print_s())); 10 } 11  
12 MainWindow::~MainWindow() 13 { 14     delete ui; 15 } 16  
17 void MainWindow::print_s() 18 { 19     if(ui->comboBox->currentIndex()==0) ui->lineEdit->setText("小振國"); 20     else if(ui->comboBox->currentIndex()==1) ui->lineEdit->setText("中振國"); 21     else if(ui->comboBox->currentIndex()==2) ui->lineEdit->setText("大振國"); 22     else if(ui->comboBox->currentIndex()==3) ui->lineEdit->setText("老振國"); 23     else if(ui->comboBox->currentIndex()==4) ui->lineEdit->setText("死振國"); 24     else ui->lineEdit->setText("振國"); 25 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM