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? -
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.
-
@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();});
-
@ofmrew
Yes, lambdas are good to know :)
I think you need to capture this pointer, to allow for it to call setModeconnect(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.htmlIt 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.
-
@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.
-
-
@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 meconnect(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.
^