QComboBox::currentIndexChanged() send two parameters to SLOT function.



  • Hi,

    I have a QComboBox testStateCombo. I have connected SIGNAL currentIndexChanged to a SLOT testStateChanged().
    @connect(testStateCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(testStateChanged(int))) ;@

    Now the above code gives me the index of the value it changed to. I needed to send another value to my testStateChanged function. I checked QSignalMapper but it is just overriding the index value to another value passed.

    @QSignalMapper* signalMapper = new QSignalMapper(this) ;
    connect(testStateCombo, SIGNAL(currentIndexChanged(int)), signalMapper, SLOT(map()));
    signalMapper->setMapping(testStateCombo, stateId);
    connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(testStateChanged(int))) ;@

    Now using the above code I am able to override the index id to stateId. Is there a way I can send both the values to my testStateChanged function.

    I am fairly new to QT, so please ask for more details if I have missed out anything and is relevant.

    Thanks
    Deepak

    PS. Its a QT 4.8 project



  • Is it even possible ?? Does it make sense ?? Why nobody is replying... :(


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Please practice some patience, not everybody on this forum lives in the same timezone as you.

    Back to your problem: you can't call a two arguments signal from a one argument signal. What you can do however is to use an intermediate slot from which you will emit the new signal.



  • Hi SGalst,

    Thanks for your reply.

    I saw few posts got answered that was posted after mine, so got little jealous.. :-|
    Actually as I said I am new to QT so was wondering if this is even a valid question to ask or not.

    Will try and check if I can fit an intermediate slot in my logic. Thanks for the hint.

    Cheers,
    Deepak



  • I was thinking about std::bind if you know that function wrapper, but I don't think you can use that with Qt signals and slots and Qt has nothing like that (yet)?

    An intermediate slot would do the trick obviously, if you don't want to define an extra slot you could just use an inline lambda function, but that depends on what your second argument is and what makes more sense for your problem.

    example code:
    @
    connect(testStateCombo, &QComboBox::currentIndexChanged, [this](int index){
    testStateChanged(index, stateId); // your might need to capture "stateId" I don't know where it comes from
    });
    @

    I might do some tests later with std::bind and std::function as slot functions, I don't know if anybody has done that with Qt, so why not :D



  • Hi Xander84,

    Thanks a lot for the tip, will try and implement it.

    Deepak



  • I was just playing around with that a little and noticed QComboBox::currentIndexChanged is an overloaded function, why Qt? :D
    @
    void currentIndexChanged(int index);
    void currentIndexChanged(const QString &);
    @
    anyway you need to cast the function pointer because there is also an QString overload as you can see, so the compiler need to know you want the "int" version, do this:
    @
    connect(ui->comboBox, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), [this](int index) {
    testStateChanged(index, stateId);
    });
    @
    maybe not as pretty, but that is because of the overloaded signal... :(

    I also tested it with std::bind if anyone is interested in that solution it works fine like this:
    @
    int stateId = 123;
    auto slotFunc = std::bind(&YourClass::testStateChanged, this, std::placeholders::_1, stateId);
    connect(ui->comboBox, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), slotFunc);
    @
    just an experiment, that is more useful if you have constant values I guess, in case your "stateId" changes the first approach with the lambda might be more useful, but in some cases the second approach is nicer because you don't need a custom function, only a function binding.



  • Hi Xander84,

    Thanks a lot for the tip, But I am using QT 4.8. and not sure if this is valid for this version. Updating my question to include the QT version.

    Deepak



  • Ok sorry I didn't thought so many people are still using Qt 4, also you said that your are new to Qt so I assumed you are using at least Qt 5.
    Anyway it might be useful for some other people, also I like to experiment with modern c++ and Qt and this was only an 5 min experient :D



  • Thanks for your time mate. Hopefully it will be useful to others.


  • Lifetime Qt Champion

    There is still a lot of people using Qt 4 for various good reasons :-)

    Then currently, you can only use the intermediate slot technique.

    Don't be jealous ;-)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.