Important: Please read the Qt Code of Conduct -

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;
    Can I pass a literal in the connect statement?

  • Lifetime Qt Champion

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

    However, there is 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.

  • Lifetime Qt Champion

    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.

  • Lifetime Qt Champion

    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

    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.

  • Lifetime Qt Champion

    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.

  • Lifetime Qt Champion

    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.

  • Lifetime Qt Champion

    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