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. Slot Connect Question
Qt 6.11 is out! See what's new in the release blog

Slot Connect Question

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 2 Posters 3.3k Views 2 Watching
  • 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.
  • mrjjM mrjj

    Hi
    In the connect , you ever only pass the types expected.
    So no "1" as you shown.

    However, there is default arguments
    http://doc.qt.io/qt-5/signalsandslots.html#signals-and-slots-with-default-arguments
    So you can have it on setMode.

    O Offline
    O Offline
    ofmrew
    wrote on last edited by
    #3

    @mrjj What I want is:
    connect(ui->setviewport, &QPushButton::clicked, ui->canvas, &MyCanvas::setMode(1));
    connect(ui->setwindow, &QPushButton::clicked, ui->canvas, &MyCanvas::setMode(2));
    connect(ui->setboth, &QPushButton::clicked, ui->canvas, &MyCanvas::setMode(3));
    connect(ui->clear, &QPushButton::clicked, ui->canvas, &MyCanvas::setMode(0));

    The values are 0, 1, 2 and 3. So, I will find another way. Thanks.

    mrjjM 1 Reply Last reply
    0
    • O ofmrew

      @mrjj What I want is:
      connect(ui->setviewport, &QPushButton::clicked, ui->canvas, &MyCanvas::setMode(1));
      connect(ui->setwindow, &QPushButton::clicked, ui->canvas, &MyCanvas::setMode(2));
      connect(ui->setboth, &QPushButton::clicked, ui->canvas, &MyCanvas::setMode(3));
      connect(ui->clear, &QPushButton::clicked, ui->canvas, &MyCanvas::setMode(0));

      The values are 0, 1, 2 and 3. So, I will find another way. Thanks.

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

      @ofmrew
      Hi in this use case i would just use lambdas as it both reduces
      number of needed slots and since your slots code is simply ,
      its ok to maintain.

      connect(ui->pushButton, &QPushButton::clicked,    [](){setMode(1);update();});
      connect(ui->pushButton_2, &QPushButton::clicked,  []() {setMode(2);update();});
      connect(ui->pushButton_3, &QPushButton::clicked,  []() {setMode(3);update();});
      
      O 1 Reply Last reply
      1
      • mrjjM mrjj

        @ofmrew
        Hi in this use case i would just use lambdas as it both reduces
        number of needed slots and since your slots code is simply ,
        its ok to maintain.

        connect(ui->pushButton, &QPushButton::clicked,    [](){setMode(1);update();});
        connect(ui->pushButton_2, &QPushButton::clicked,  []() {setMode(2);update();});
        connect(ui->pushButton_3, &QPushButton::clicked,  []() {setMode(3);update();});
        
        O Offline
        O Offline
        ofmrew
        wrote on last edited by
        #5

        @mrjj Thanks. I will try it, guess it is time to learn lambdas.

        mrjjM 1 Reply Last reply
        0
        • O ofmrew

          @mrjj Thanks. I will try it, guess it is time to learn lambdas.

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

          @ofmrew
          Yes, lambdas are good to know :)
          I think you need to capture this pointer, to allow for it to call setMode

          connect(ui->pushButton, &QPushButton::clicked,    [this](){setMode(1);update();});
          
          

          Please google them. its c++ construct.

          Also be aware of this class
          http://doc.qt.io/qt-5/qsignalmapper.html

          It can also be used in such case.

          O 1 Reply Last reply
          0
          • mrjjM mrjj

            @ofmrew
            Yes, lambdas are good to know :)
            I think you need to capture this pointer, to allow for it to call setMode

            connect(ui->pushButton, &QPushButton::clicked,    [this](){setMode(1);update();});
            
            

            Please google them. its c++ construct.

            Also be aware of this class
            http://doc.qt.io/qt-5/qsignalmapper.html

            It can also be used in such case.

            O Offline
            O Offline
            ofmrew
            wrote on last edited by
            #7

            @mrjj Thanks very much, lambdas are easier to use than I first thought. I used:
            connect(ui->setviewport, &QPushButton::clicked, ={ui->canvas->setMode(1);});
            connect(ui->setwindow, &QPushButton::clicked, ={ui->canvas->setMode(2);});
            connect(ui->setboth, &QPushButton::clicked, ={ui->canvas->setMode(3);});
            connect(ui->clear, &QPushButton::clicked, ={ui->canvas->setMode(0);});

            and removed three functions! Note that setMode was in MyCanvas and not MainWindow. I will read the Signal Mapper reference. Thanks again.

            mrjjM 1 Reply Last reply
            0
            • O ofmrew

              @mrjj Thanks very much, lambdas are easier to use than I first thought. I used:
              connect(ui->setviewport, &QPushButton::clicked, ={ui->canvas->setMode(1);});
              connect(ui->setwindow, &QPushButton::clicked, ={ui->canvas->setMode(2);});
              connect(ui->setboth, &QPushButton::clicked, ={ui->canvas->setMode(3);});
              connect(ui->clear, &QPushButton::clicked, ={ui->canvas->setMode(0);});

              and removed three functions! Note that setMode was in MyCanvas and not MainWindow. I will read the Signal Mapper reference. Thanks again.

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

              @ofmrew
              Hi super
              yes they are just nameless function with an odd syntax.
              Note when you do not use </> button to insert code tags, it eats stuff :) (here on forum)

              O 1 Reply Last reply
              0
              • mrjjM mrjj

                @ofmrew
                Hi super
                yes they are just nameless function with an odd syntax.
                Note when you do not use </> button to insert code tags, it eats stuff :) (here on forum)

                O Offline
                O Offline
                ofmrew
                wrote on last edited by
                #9

                @mrjj Sorry about not using </>, but I just learned what the icons mean and I forgot to use it. My bad. For those that might like to use the solution, here it is the correct way:

                    connect(ui->setviewport, &QPushButton::clicked, [=](){ui->canvas->setMode(1);});
                    connect(ui->setwindow, &QPushButton::clicked, [=](){ui->canvas->setMode(2);});
                    connect(ui->setboth, &QPushButton::clicked, [=](){ui->canvas->setMode(3);});
                    connect(ui->clear, &QPushButton::clicked, [=](){ui->canvas->setMode(0);});
                

                I see the difference.

                mrjjM 1 Reply Last reply
                0
                • O ofmrew

                  @mrjj Sorry about not using </>, but I just learned what the icons mean and I forgot to use it. My bad. For those that might like to use the solution, here it is the correct way:

                      connect(ui->setviewport, &QPushButton::clicked, [=](){ui->canvas->setMode(1);});
                      connect(ui->setwindow, &QPushButton::clicked, [=](){ui->canvas->setMode(2);});
                      connect(ui->setboth, &QPushButton::clicked, [=](){ui->canvas->setMode(3);});
                      connect(ui->clear, &QPushButton::clicked, [=](){ui->canvas->setMode(0);});
                  

                  I see the difference.

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

                  @ofmrew
                  Hi
                  Well it will come in time. It just makes sure people do not help you
                  on missing stuff just due to no code tags.

                  Just a note [=] captures all variables in scope. You might only need
                  to capture [ui->canvas]

                  O 1 Reply Last reply
                  0
                  • mrjjM mrjj

                    @ofmrew
                    Hi
                    Well it will come in time. It just makes sure people do not help you
                    on missing stuff just due to no code tags.

                    Just a note [=] captures all variables in scope. You might only need
                    to capture [ui->canvas]

                    O Offline
                    O Offline
                    ofmrew
                    wrote on last edited by
                    #11

                    @mrjj Thanks, I was about to try that, but one step at a time. It did not work, objected to ui.

                    mrjjM 1 Reply Last reply
                    0
                    • O ofmrew

                      @mrjj Thanks, I was about to try that, but one step at a time. It did not work, objected to ui.

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

                      @ofmrew
                      Hi
                      Instead of "did not work" a copy paste of the actual error it gave often produces better answers :)
                      Anyway, this compiles and works for me

                      connect(ui->pushButton, &QPushButton::clicked,    [this](){this->ui->textEdit->setText("kk");});
                      

                      Assume canvas is just a widget on the form just like textEdit is.

                      O 1 Reply Last reply
                      2
                      • mrjjM mrjj

                        @ofmrew
                        Hi
                        Instead of "did not work" a copy paste of the actual error it gave often produces better answers :)
                        Anyway, this compiles and works for me

                        connect(ui->pushButton, &QPushButton::clicked,    [this](){this->ui->textEdit->setText("kk");});
                        

                        Assume canvas is just a widget on the form just like textEdit is.

                        O Offline
                        O Offline
                        ofmrew
                        wrote on last edited by
                        #13

                        @mrjj What I tried was [ui->canvas]
                        error: capture of non-variable ‘MainWindow::ui’
                        connect(ui->setviewport, &QPushButton::clicked, ui->canvas{ui->canvas->setMode(1);});

                        changed to :

                            connect(ui->setviewport, &QPushButton::clicked, [this](){this->ui->canvas->setMode(1);});
                        

                        And that worked. Thanks.
                        ^

                        1 Reply Last reply
                        2

                        • Login

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