help on signals and slots
-
Hello,
I'm new to QT and signals and slots mechanism, correct me where I'm wrong?
I have a
label
in a form, the text need to be changed upon clicking a button , I trying to implement using a signals and slots.
My signal will be emitted as follows:void MainOne::on_pushButton_help_clicked() { emit textToChange("text updated"); }
I then connected it as follows:
connect(this, SIGNAL(textTochange(QString)), this->ui->label_pid, SLOT(setText(QString)));
But this is not working, any help?
-
@russjohn834 Did you verify that on_pushButton_help_clicked() was executed?
Do you see any warnings in the application output when executing your app? -
@jsulm Thank you. Yes , on_pushButton_help_clicked() is executed. No warnings related to this.
-
@russjohn834 said in help on signals and slots:
connect(this, SIGNAL(textTochange(QString)), this->ui->label_pid, SLOT(setText(QString)));
is this copy and pasted ? because that is not the signal you emit
textTochange != textToChange
-
@russjohn834 said in help on signals and slots:
Yes , on_pushButton_help_clicked() is executed. No warnings related to this.
But is the connect() statement executed without warning?
auto cnxState = connect(this, SIGNAL(textTochange(QString)), this->ui->label_pid, SLOT(setText(QString))); if(!cnxState) qDebug() << "Failed to connect!!!";
-
@russjohn834 as you're using the old (Qt4) syntax, you only get a run time warning, not a compile time one
-
@russjohn834 said in help on signals and slots:
But qt did not warn me that it is an undefined signal!! why!?
With this old connect syntax you will not get any warning/error when compiling, only when running your app - that's why I asked you whether you see anything in the application output (in the console). I'm sure there was something.
It is better to use new connect syntax: https://wiki.qt.io/New_Signal_Slot_Syntax
-
Thanks a lot for the feedback guys!
can someone show me how can I switch this to new connect syntax?connect(this, SIGNAL(textToChange(QString)), this->ui->label_pid, SLOT(setText(QString)));
-
@russjohn834
sure, but all information needed should actually be inside the wiki page @jsulm linked youconnect(this, &MainOne::textToChange, ui->label_pid, &QLabel::setText);