I am new to Qt and trying to write signal and slot. Below is the code I was trying to run. with SLOT and SIGNAL keywords on "connect" function, it works fine. But I do not want to use SIGNAL and SLOT keywords on connect function, instead want to use a different approach.
class MyWindow:public QMainWindow
{
Q_OBJECT
public:
MyWindow();
};
MyWindow::MyWindow()
{
QWidget *widget=new QWidget;
QHBoxLayout *layout =new QHBoxLayout;
QSpinBox *mySlide = new QSpinBox;
mySlide->setRange(0,10);
QLCDNumber *lcdNumber= new QLCDNumber;
layout->addWidget(mySlide);
layout->addWidget(lcdNumber);
connect(mySlide,SIGNAL(valueChanged(int)),lcdNumber,SLOT(display(int)));
// connect(mySlide, &QSpinBox::valueChanged,lcdNumber, &QLCDNumber::display);
widget->setLayout(layout);
setCentralWidget(widget);
}
In above code, uncommented connect function works fine but commented connect function does not work. I am getting error "no matching function to call MyWindow::connect". Not sure what mistake I made here. I was following this article from qt.
The problem is that both the signal --
QSpinBox::valueChanged
-- and the slot --QLCDNumber::display
-- have multiple overloads for different argument types. You have a few options.1) Use a
static_cast
to disambiguate between the various signal and slot overloads...or...
2) Just use a
lambda
...Although using the
lambda still
requires astatic_cast
on the signal.