No matching signal AND no matching member function for call to connect
-
*** Edit: This topic includes two problems. Both solved. For no matching member function for call to connect issue, see the bottom of the page. ***
In the header file, I have defined a checkbox like following:public: QCheckBox *c1;
In .cpp file, I am dynamically creating the checkbox:
ui->setupUi(this); c1 = new QCheckBox(this);
Since this checkbox is not visible at .ui file, I cannot right click and choose "go to slot". Instead, I have defined a similar function manually.
Now, my header file includes:public: QCheckBox *c1; private slots: void on_c1_stateChanged(int arg1); // manually written.
And I added the following fuction to my .cpp file:
void settingsDialog::on_c1_stateChanged(int arg1) { if(arg1 == 0) qDebug() << "now unchecked"; else if (arg1 == 2) qDebug() << "now checked"; }
So I am basicly trying to debug "checked" when checked, and vice versa. To do this, I have added the following code manually:
QObject::connect( c1 , SIGNAL(stateChanged(int)) , this , SLOT(on_c1_stateChanged(int)));
And, it works! However, everytime when I run the program, I get a warning in the application output tab:
" QMetaObject::connectSlotsByName: No matching signal for on_c1_stateChanged(int). "
Although it works, I just wanted to be sure that it will not cause any errors in the future. Do I have any mistake in my QObject::connect(...) function?
Thank you in advance!
-
@Kuvars said in No matching signal:
" QMetaObject::connectSlotsByName: No matching signal for on_c1_stateChanged(int). "
Although it works, I just wanted to be sure that it will not cause any errors in the future. Do I have any mistake in my QObject::connect(...) function?- Use the 'new' signal and slot syntax to catch connection errors on compile time rather than runtime
- Due to your naming of your slot you trigger the auto-connect feature of Qt which then complains that it can't find the approriate stuff - rename your slot (do not start with
on_
or ignore the message)
-
@Christian-Ehrlicher
Thank you, sir!
For other people who might wonder the solution:I have changed the name of my fuction from "on_c1_stateChanged(int arg1)" to "on_c1StateChanged(int arg1)".
In brief, changing the name indeed solves the issue. -
@Christian-Ehrlicher
Hello again, now I have another question related to it. I can successfully connect in two ways:QObject::connect( c1, SIGNAL(stateChanged(int)) , this , SLOT( function1() )); // old syntax QObject::connect( c1, &QCheckBox::stateChanged, this, &settingsDialog::fuction1); // new syntax
Now, instead of connecting it to function1 in my settingsDialog class, I want to connect it to function2 in another class, called MainWindow. I try to do this as:
QObject::connect( c1, &QCheckBox::stateChanged , this , &MainWindow::function2);
However, it gives the following error: No matching member function for call to 'connect'.
Both function1() and function2() are defined as public slots. Also, in the header files of both classes, there is Q_OBJECT Macro:
class settingsDialog : public QDialog { Q_OBJECT public: explicit settingsDialog(QWidget *parent = nullptr); ~settingsDialog(); public slots: void function1(); // other fuctions and data members
class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); public slots: void function2(); // other functions and data members };
Couldn't solve it. I hope you guys help, thank you in advance.
-
@Kuvars said in No matching signal:
QObject::connect( c1, &QCheckBox::stateChanged , this , &MainWindow::function2);
What is
this
? Is it stillsettingsDialog
and notMainWindow
? And anyway you should never connect toMainWindow
-anything other than from insideMainWindow
(nor includemainwindow.h
anywhere other than inmainwindow.cpp
). -
@JonB
Let me summarize what I am trying to do. I have a mainwindow class that I perform most operations. In its header file, I have defined:SettingsDialog *settingsPage;
In the constructor in .cpp file of maindwindow, I am creating it dynamically:
settingsPage = new SettingsDialog(this);
The reason what I am doing is that, I will add checkboxes to this dialog, and if I check those checkboxes and close the dialog, I want them to remain their state. For instance, when I open the dialog again by clicking a button on mainwindow, I will see that they are still checked on settingsdialog.
In a button on mainwindow, I am opening setttings dialog by:
settingsPage->show();
Now, here is the problematic part:
My aim is, when I changed the state of a checbox on settingsdialog, I want to change the value of a variable in mainwindow. For instance, in mainwindow, now there is an integer: int var = 0; Everytime when I check the checkbox on settingsdialog, I want the "var"in mainwindow to become 1, and everytime when I uncheck it, I want it to become 2, lets say.To do this, I need to add signal and slot to checkbox. If I choose a slot function in settingsdialog, I cannot access mainwindows's "var" from settingsdialog class, because I don't have an object of mainwindow class to access its data member or functions. Instead, I am trying to connect the checkbox in settingsdialog to a function in mainwindow, so that evertime the state of the checkbox in settingdialog changes, it will call a fuction in mainwindow, and so that I can access "var" within this function.
Thats why I am trying to connect like:
QObject::connect( c1, &QCheckBox::stateChanged , this , &MainWindow::function2);
where function2 is the function in mainwindow.
I hope I could explain my purpose. Thank you in advance for your help.
-
@Kuvars said in No matching signal:
Instead, I am trying to connect the checkbox in settingsdialog to a function in mainwindow
QObject::connect( c1, &QCheckBox::stateChanged , this , &MainWindow::function2);
So just as i asked, what is
this
? This is insettingsdialog
, right? Sothis
is notMainWindow
, so your slot connection cannot be made.As stated earlier, you never should connect to, or even access, anything in
MainWindow
from elsewhere.connect()
s must be placed somewhere which can access the slot (as well the signal), do not connect from where the signal is to some "outside world" slot.You need to move the connection into
MainWindow
, where the slot is. You will either have to exportsettingsDialog.c1
so that main window can use it for the signaller, or you will need to create your own signal to emit fromsettingsDialog
which main window can connect from and have the dialog do its own internal connection onc1
to emit that dialog signal. -
@JonB said in No matching signal:
or you will need to create your own signal to emit from settingsDialog which main window can connect from and have the dialog do its own internal connection on c1 to emit that dialog signal.
That is what I am trying to do sir, how can I modify :
QObject::connect( c1, &QCheckBox::stateChanged , this , &MainWindow::function2);
I apologize, new in Qt. Should I also change the 3rd argument (this) to be able to do that? I appreciate if it is possible for you to illustrate it with a short code segment.
-
ChkBox1::ChkBox1(QWidget *parent) : QWidget(parent), ui(new Ui::ChkBox1) { ui->setupUi(this); chbTest = new QCheckBox(tr("Hide this window")); connect(chbTest, SIGNAL(clicked(bool)), SLOT(HideWindow())); vbxTest = new QVBoxLayout; vbxTest->addWidget(chbTest); setLayout(vbxTest); setWindowTitle(tr("Check Box demo")); } ChkBox1::~ChkBox1() { delete ui; } void ChkBox1::HideWindow() { if(chbTest->isChecked()) { QMessageBox msg1; msg1.setText(tr("Hai")); msg1.exec(); } }
Hope this helps
-
@dan1973
YESS! It helped, thank you! and also thanks to @JonB !Here is how I handled the problem in mainwindow . cpp file by analyzing your code segment:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); settingPage = new settingsDialog(this); QObject::connect(settingPage->c1 ,&QCheckBox::stateChanged, this, &MainWindow::function2) }
So that I could access the checkbox on settingsdialog by settingspage object and "->" operator. Also, the connect() fuction and the slot function2() are in the mainwindow as JohnB suggested.
Thank you all!