Constantin
Constantin

Reputation: 17758

QComboBox Get The Varient When "currentIndexChanged(int)" Emitted

I am having difficulty finding documentation on this or an example.

Could someone concretely show me how to access the QVariant of the currently selected index in a QComboBox

 QComboBox * combo = new QComboBox();
 combo->addItem("Bla1", QVariant(1));
 combo->addItem("Bla2", QVariant(2));
 combo->addItem("Bla3", QVariant(3));
 combo->addItem("Bla4", QVariant(4));
 connect(combo, SIGNAL(currentIndexChanged(int)), this, slot(HANDLEITMAN(int))

And of course else where in the source

 void TheCooler::HANDLEITMAN(int index)
 {
      //What do I do with index?
      //sender()?
 }

Upvotes: 0

Views: 1707

Answers (2)

Lohrun
Lohrun

Reputation: 6752

If you don't want to make combo a member of the class TheCooler, you can use the sender() function that returns a pointer to the QObject that sent the triggering signal (in this case, currentIndexChanged(int)).

void TheCooler::HANDLEITMAN(int index)
{
    QComboBox * combo = qobject_cast< QComboBox * >(sender());
    if (combo == 0)
        return; // something wrong happened

    QVariant data = combo->itemData(index);
}

If combo is null, then you probably tried to call the slot by yourself, or you have connected it with a signal emitted by a class that is not a QComboBox.

Upvotes: 2

Matt Phillips
Matt Phillips

Reputation: 9691

First, make combo a member of TheCooler, or otherwise put HANDLEITMAN in a class which has combo as a member. Unless it's available to TheCooler::HANDLEITMAN somehow you can't get the data, and this is the logical way to do it. Then it's just

 void TheCooler::HANDLEITMAN(int index)
 {
      QVariant data = combo->itemData(index);
 }

Upvotes: 3

Related Questions