Slot Connect Question



  • I am trying to reduce the number of methods by including a value that matches the slot type; however, I get:
    error: cannot call member function ‘void MyCanvas::setMode(int)’ without object
    connect(ui->setviewport, &QPushButton::clicked, ui->canvas, &MyCanvas::setMode(1));
    The slot is:
    void MyCanvas::setMode(int m)
    {
    mode = m;
    this->update();
    }
    Can I pass a literal in the connect statement?


  • Qt Champions 2017

    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.



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


  • Qt Champions 2017

    @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();});
    


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


  • Qt Champions 2017

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



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


  • Qt Champions 2017

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



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


  • Qt Champions 2017

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



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


  • Qt Champions 2017

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



  • @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.
    ^


Log in to reply
 

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