[SOLVED] Calling Slots with value/parameter?



  • so hi again,

    I need to call a SLOT with a parameter/value.
    how to do this, if i set up like this, the slot is not called

    @for (int i=0; i< value_x; i++){

       connect (push_button_here[i], SIGNAL(clicked()), this, SLOT(do_this(i)));   
    

    } @

    how can this be fixed?
    just tried solving this like forever.

    thank you already



  • Hi!

    "QSignalMapper":http://doc.qt.digia.com/qt/qsignalmapper.html should do the trick.

    Regards,
    Jake



  • thanks but you could provide little bit of code,

    i dont really understand how to get this run

    where should i write my own "do_this(i)" function?

    that example doesnt really help me


  • Moderators

    Something like this should work, assuming you have a slot do_this(int) in your code.

    @
    QSignalMapper *signalMapper = new QSignalMapper(this);

    for (int i = 0; i < value_x; ++i) {
    connect(push_button_here[i], SIGNAL(clicked()), signalMapper, SLOT(map()));
    signalMapper->setMapping(push_button_here[i], i);
    }

    connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(do_this(int)));
    @
    Brain to keyboard, YMMV



  • I can't find a project in which I played with QSignalMapper, but based on docs, it should be something like this:

    @
    QSignalMapper *mapper = new QSignalMapper(this);

    for(int i=0; i<0; i<value_x; i++)
    {
    connect(push_button_here[i], SIGNAL(clicked()), mapper, SLOT(map()));
    mapper->setMapping(push_button_here[i], i);
    }

    connect(mapper, SIGNAL(mapped(int)), this, SLOT(do_this(int)));@

    Note: Not tested!



  • [quote author="mlong" date="1354648588"]Something like this should work, assuming you have a slot do_this(int) in your code.

    @
    QSignalMapper *signalMapper = new QSignalMapper(this);

    for (int i = 0; i < value_x; ++i) {
    connect(push_button_here[i], SIGNAL(clicked()), signalMapper, SLOT(map()));
    signalMapper->setMapping(push_button_here[i], i);
    }

    connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(do_this(int)));
    @
    Brain to keyboard, YMMV[/quote]

    WOW Thats amazing!

    great working!

    thanks a lot



  • Don't forget to mark as solved.

    And mlong, you were faster :) . I had to read all the docs first.

    Regards,
    Jake


  • Moderators

    Glad it's working :)

    And Jake007, I like how our code is very much the same, though. Almost as if we both were modifying the example code.



  • Well... I did modify the example code :) .

    Oh and one more solution I forgot to mention.

    You could use sender() method ( in do_this(), without parameters), which returns QObject *. You can then cast it to QPushButton * or whatever widget you have. But I wouldn't recommend using it, as there might be problems if you call method directly or is accidentally called with signal from object with different type.
    But might come in handy in a few lazy cases ;) .


Log in to reply
 

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