Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Help with setting basic Signal and Slot

Help with setting basic Signal and Slot

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 2 Posters 1.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • HashDragonH Offline
    HashDragonH Offline
    HashDragon
    wrote on last edited by HashDragon
    #1

    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 :

    switch(ui->buttonGroup->checkedId())
                {
                   case -3 : //do something
                   break;
                   case x: //call that function from another class
                   break;
               }
    

    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!

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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?

      HashDragonH 1 Reply Last reply
      0
      • mrjjM mrjj

        @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?

        HashDragonH Offline
        HashDragonH Offline
        HashDragon
        wrote on last edited by
        #3

        @mrjj yes precisely

        mrjjM 1 Reply Last reply
        0
        • HashDragonH HashDragon

          @mrjj yes precisely

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          @HashDragon
          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()
          {
          callthefunc
          }

          and when right RB is clicked/checked
          timer->start(1000);
          and when other is clicked
          timer->stop();

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

          HashDragonH 1 Reply Last reply
          0
          • mrjjM mrjj

            @HashDragon
            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()
            {
            callthefunc
            }

            and when right RB is clicked/checked
            timer->start(1000);
            and when other is clicked
            timer->stop();

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

            HashDragonH Offline
            HashDragonH Offline
            HashDragon
            wrote on last edited by HashDragon
            #5

            @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?"
            Thanks!

            mrjjM 1 Reply Last reply
            0
            • HashDragonH HashDragon

              @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?"
              Thanks!

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @HashDragon
              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;
              fm.callsomefunction();
              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 :)

              HashDragonH 1 Reply Last reply
              0
              • mrjjM mrjj

                @HashDragon
                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;
                fm.callsomefunction();
                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 :)

                HashDragonH Offline
                HashDragonH Offline
                HashDragon
                wrote on last edited by
                #7

                @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) .

                mrjjM 1 Reply Last reply
                0
                • HashDragonH HashDragon

                  @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) .

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #8

                  @HashDragon
                  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;
                  Thresholddia.exec();
                  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.

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved