I want to connect custom slot which takes one int
argument to clicked()
signal emited by different widget. Here's the sample code:
.h
file:
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void setLayoutNow();
void changeLabelText(int nr); //slot I'm talking about
private:
Ui::MainWindow *ui;
QLabel *label;
QPushButton *button1, *button2;
QVBoxLayout *mainLayout;
};
.cpp
file:
void MainWindow::setLayoutNow()
{
label = new QLabel("LABEL");
button1 = new QPushButton("WRITE 1");
button2 = new QPushButton("WRITE 2");
mainLayout = new QVBoxLayout(ui->centralwidget);
mainLayout -> insertWidget(0, label, 3);
mainLayout -> insertWidget(1, button1, 1);
mainLayout -> insertWidget(2, button2, 1);
connect(button1, SIGNAL(clicked()), this, SLOT(changeLabelText(1)));
connect(button2, SIGNAL(clicked()), this, SLOT(changeLabelText(2)));
}
void MainWindow::changeLabelText(int nr)
{
if (nr == 1)
label->setText("1");
if (nr == 2)
label->setText("2");
}
When I start my app I receive a message qt.core.qobject.connect: QObject::connect: No such slot MainWindow::changeLabelText(1)
. How am I supposed to connect clicked
signal with this slot? What causes the problem?
I read this question which points how qt requires matching argument types but my app gives me different error than Incompatible sender/receiver arguments
.
I also want to point out that when I connected functions with no arguments, no problem like that occured.