PDA

View Full Version : how do I use comboBox



QT++
2nd July 2013, 11:30
I want to use a comboBox but am having some problems with the signals. I want to have comboBox with the options

cat
dog
mouse

then when I click a pushButton (which I have already tested and coded) it calls the appropriate function. So the pushButton has the option of calling 3 different functions depending if the user has selected cat, dog or mouse from the comboBox. I know the code below isn't right but hopefully explains the idea.



if (comboBox == cat){
cat();
}

if (comboBox == dog){
dog();
}

wysota
2nd July 2013, 11:33
And what is the question?

QT++
2nd July 2013, 11:42
What is the code to achieve what I have explained above.

rawfool
2nd July 2013, 11:42
And I dind't understand the use of QPushButton in your requirement.
But you can invoke this signal - void currentIndexChanged(const QString & text)

I think this helps


connect(comboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(onComboIndexChanged(QString)));

// Write your slot something like this
void CYourClass::onComboIndexChanged(QString str)
{
if(str == "cat")
{
cat();
}
else if(str == "dog")
{
dog();
}
}

wysota
2nd July 2013, 13:16
What is the code to achieve what I have explained above.

Connect to the button's clicked() signal and check the current index of the combobox.

anda_skoa
2nd July 2013, 14:13
if (comboBox->currentText() == "cat")

or, if you know that "cat" is the first option, as wysota said



if (comboBox->currentIndex() == 0)


cheers,
_