PDA

View Full Version : QComboBox



mandlakoteswar2011
1st April 2015, 05:33
How can I get the selected VALUE out of a QCombobox & Count the Values form QComboBox?

I have to get values from QComboBox...and count how many columns are inserted in QComboBox

Kindly Do Needful....

jefftee
1st April 2015, 05:49
Have you looked at the documentation for QComboBox? Specifically QComboBox::count and QComboBox::currentText might be useful to you.

mandlakoteswar2011
1st April 2015, 07:56
Thanks for u r advice...


i tried currentText...it is getting only first item value....when i press second item didn't print second value...


Here is the Code....



#include <QApplication>
#include <QLabel>
#include <QPushButton>
#include <QWidget>
#include <QComboBox>
#include <QDialog>
#include <QVBoxLayout>
#include <QMessageBox>
#include <stdio.h>
#include <QDebug>

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

QApplication a(argc, argv);
QWidget w;
char str[34]= "12333";
char str1[10]= "267788";
char str2[12]= "39987";


QVBoxLayout *layout = new QVBoxLayout(&w);
QLabel *label = new QLabel("Plz Select the items in ComboBox");
QComboBox *combo = new QComboBox();
layout->addWidget(label);
layout->addWidget(combo);
combo->addItem(str);
combo->addItem(str1);
combo->addItem(str2);
QString val = combo->currentText();
qDebug()<<val;

QObject::connect(combo, SIGNAL(currentIndexChanged(QString)), label, SLOT(setText(QString)));

w.show();

return a.exec();

}

....PFA

jefftee
1st April 2015, 08:05
Unless I am mistaken, QComboBox only supports a single selection. Perhaps others may know of a way to accomplish this, but I see nothing in QComboBox documentation that leads me to believe it supports multiple selections.

mandlakoteswar2011
1st April 2015, 08:11
But TopLayout if u press multiple selections it is updating...:confused:

jefftee
1st April 2015, 08:12
But TopLayout if u press multiple selections updated...:confused:
Sorry, I have no idea what you just said... :)

mandlakoteswar2011
1st April 2015, 08:15
iam adding QVBoxLayout what ever text in QcomBox...it is perfectly updated...but when iam printing text in QComboBox...only single Item only printed...

jefftee
1st April 2015, 08:23
The code you are using above and what you originally asked was how to get the text for the currently selected item. If you want to print all entries in the QComboBox, you'll have to loop through all of the entries like:



for (int i = 0; i < combo->count(); i++)
{
QString text = combo->itemText(i);
qDebug() << text;
}

Is that what you are looking for?

mandlakoteswar2011
1st April 2015, 08:45
Thanks guru really....its working ....I modified according the my Requiremet...