⚠️ Forum Maintenance: Feb 6th, 8am - 14pm (UTC+2)

Help with setting basic Signal and Slot

  • Hello everyone.
    I have a QButtonGroup of radioButtons and I want that upon clicking of a certain radioButton(with ID say x), it should execute a function of another class(form) till the time radioButton selection does not change. I am fairly new to QT and have been trying to implement this since a long time but in vain.
    Right now, here is my relevant piece of code :

                   case -3 : //do something
                   case x: //call that function from another class

    But I think this switch statement won't be required If I am able to establish a signal/slot connection. Its too confusing. Any help would be appreciated. Thanks!

  • Lifetime Qt Champion

    @HashDragon said:

    execute a function of another class(form) till the time radioButton selection does not change

    what does this means ?
    If I select RadioButt 4. for example, it should
    keep calling the same function until i select another RB?

  • @mrjj yes precisely

  • Lifetime Qt Champion

    Well the basic of signal and slots is that when something
    happens a signal is sent and and some reacts.
    So only 1 signal is sent when u select that RB, so u need more code.

    You can use a Qtimer.
    and start it when X RB is selected.

    This timer sends a signal timeout() on each seconds/what you set.
    So in this slot you can then call the function you need.

    If a new RB is selected u stop the timer again.

    so something like
    (in mainwin constructor, NOT in your function)

    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(TimerTick()));

    then in mainwin.h
    private slots:
    void TimerTick();

    and in mainwin.cpp

    void MainWindow::TimerTick()

    and when right RB is clicked/checked
    and when other is clicked

    Note: for long lasting operations in callthefunc, a timer is not the way to do it.

  • @mrjj This looks elegant! However, the function I need to call is a part of another form.
    How do I call it in my mainwindow ? And what is your approximation for "long lasting?"

  • Lifetime Qt Champion

    well the other form, do u show this in main? it seems a bit strange to call
    its function if its not shown, but it is possible.
    A more clean design would be using signals but lets see how to call it never the less.

    so lets call form MyForm
    then you would do
    MyForm fm;
    but it wont keep window open or anything.

    so im not sure what u are trying?

    Where do u show this other form?
    There you would have a pointer to it.

    • approximation for "long lasting?"
      well more than a few sec.

    But overall i dont understand why mainwindow would have to repeatedly call a function in another form object.
    Seems not so right :)

  • @mrjj Actually I am developing an OpenCV program. I have a mainwindow with a set of buttons. Say, I press on a button "Threshold", then it will open another form which has the design for Thresholding(i.e all the trackbars to vary the HSV properties,etc). So my threshold function is defined in that particular form. So I need to work on "each frame" which the camera is capturing(therefore the term "infinitely call" to threshold function).
    I have a pointer to that form available but with that I'm not able to access the functions of that form(even though its declared as public) .

  • Lifetime Qt Champion

    Ok i see. Never use used OpenCV so forgive any ignorance on my part.

    • Thresholding
      Why do u need to "drive it" from mainwindow?
      it seems to me that the UI form Object could very well do this it self , once open ?
      Like it open source and process it. Or does mainwindow contain feed and u need to just show
      widgets to adjust Threshold ? is there a frame/preview in this other window?
      button "Threshold" could call slot.
      in that slot you

    Thresholddialog Thresholddia;
    here the main/GUI thread is directed into exec() so it blocks the mainwindow.
    Im not sure that is what you are after?

    Do you call show() or exec() on your Thresholddialog ?

    I have a pointer to that form available but with that I'm not able to access the functions
    is the pointer of the correct class type`? You should be able to call methods.

Log in to reply