Qt comboBox控件的三种基本用法


一般用到控件comboBox的基本用法是获取当前的值:

1.currentIndex(); 获取当前comBox的索引,是int类型的值。
2.currentText(); 获取当前comBox的文本,是QString类型。

二、可以通过以下两种方式来通过切换comobox的值来执行一些指令:

1、通过首先在界面上拖入控件comobox,然后就是直接通过将comobox中的输入“Chinese”和“English”

 1 void MainWindow::on_comboBox_currentIndexChanged(const QString &arg1)  2 {  3 // if(ui->comboBox->currentText()=="Chinese")  4 // {  5 // qDebug()<<"优秀";  6 // }  7  
 8 // else if (ui->comboBox->currentText()== "English")  9 // { 10 // qDebug()<<"good"; 11 // } 12     //或者是这样
13     if(ui->comboBox->currentIndex() == 0) 14  { 15         qDebug()<<"优秀"; 16  } 17     else if(ui->comboBox->currentIndex()==1) 18  { 19         qDebug()<<"good"; 20  } 21     
22 }

在构造函数中进行绑定:

connect(ui->comboBox,SIGNAL(currentTextChanged(QString)),this,SLOT(SetValue(QString)));

执行槽函数:

 1 void MainWindow::SetValue(QString)  2 {  3     if(ui->comboBox->currentText()== "Chinese")  4  {  5         qDebug()<<"111";  6  
 7  }  8     else if(ui->comboBox->currentText()=="English")  9  { 10         qDebug()<<"222"; 11  } 12  
13 }

三、就是当comobox里面的值发生了改变之后,自动识别并打印出来里面的值,如下代码:

在.h文件中:

 void on_comboBox_currentIndexChanged(const QString &arg1);

在.cpp文件中如下:

1 void MainWindow::on_comboBox_currentIndexChanged(const QString &arg1) 2 { 3     QString str =ui->comboBox->currentText(); 4     qDebug()<<"str:"<<str; 5  
6 }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM