trouble connecting private slot to a signal
-
Hello,
I am having trouble connecting my slot to a signal that I want, I might be making some beginner mistake (since I am a beginner) but I can't figure it out even after searching on google for quite a while. Thank you for any helpheader file:
private: QPushButton *btn_search; private slots: void some_function(); };in my window.cpp file I am trying to connect the slot to a signal from the btn_search:
window::window(QWidget *parent) : QWidget{parent} { btn_search = new QPushButton("Search", this); connect(btn_search, SIGNAL(clicked()), this, SLOT(some_function())); } void window::some_function() { /*do something*/ }I am not getting any errors, but the terminal says this:
QObject::connect: Cannot connect (nullptr)::clicked() to window::combo_by_attribute()and the button obviously doesn't do anything. I have a lot of other stuff in the program, that work perfectly fine when I run the project. Why I can't connect these two, am I missing something? Thank you
-
Your example does not match the output - btn_search is a nullptr when you call connect()
-
Hello,
I am having trouble connecting my slot to a signal that I want, I might be making some beginner mistake (since I am a beginner) but I can't figure it out even after searching on google for quite a while. Thank you for any helpheader file:
private: QPushButton *btn_search; private slots: void some_function(); };in my window.cpp file I am trying to connect the slot to a signal from the btn_search:
window::window(QWidget *parent) : QWidget{parent} { btn_search = new QPushButton("Search", this); connect(btn_search, SIGNAL(clicked()), this, SLOT(some_function())); } void window::some_function() { /*do something*/ }I am not getting any errors, but the terminal says this:
QObject::connect: Cannot connect (nullptr)::clicked() to window::combo_by_attribute()and the button obviously doesn't do anything. I have a lot of other stuff in the program, that work perfectly fine when I run the project. Why I can't connect these two, am I missing something? Thank you
@mono said in trouble connecting private slot to a signal:
btn_search = new QPushButton("Search", this); connect(btn_search, SIGNAL(clicked()), this, SLOT(some_function()));The error message looks like your actual code (not the one you show) has these two lines flipped, like:
connect(btn_search, SIGNAL(clicked()), this, SLOT(some_function())); btn_search = new QPushButton("Search", this);Also, better switch to the current standard way to establish a connection:
connect(btn_search, &QPushButton::clicked, this, &window::some_function); -
Hello,
I am having trouble connecting my slot to a signal that I want, I might be making some beginner mistake (since I am a beginner) but I can't figure it out even after searching on google for quite a while. Thank you for any helpheader file:
private: QPushButton *btn_search; private slots: void some_function(); };in my window.cpp file I am trying to connect the slot to a signal from the btn_search:
window::window(QWidget *parent) : QWidget{parent} { btn_search = new QPushButton("Search", this); connect(btn_search, SIGNAL(clicked()), this, SLOT(some_function())); } void window::some_function() { /*do something*/ }I am not getting any errors, but the terminal says this:
QObject::connect: Cannot connect (nullptr)::clicked() to window::combo_by_attribute()and the button obviously doesn't do anything. I have a lot of other stuff in the program, that work perfectly fine when I run the project. Why I can't connect these two, am I missing something? Thank you
@mono I suggest to change your code like this:
window::window(QWidget *parent) : QWidget{parent} { btn_search = new QPushButton("Search", this); connect(btn_search, &QPushButton::clicked, this, &window::some_function); } void window::some_function(bool checked) { /*do something*/ } -
@mono said in trouble connecting private slot to a signal:
btn_search = new QPushButton("Search", this); connect(btn_search, SIGNAL(clicked()), this, SLOT(some_function()));The error message looks like your actual code (not the one you show) has these two lines flipped, like:
connect(btn_search, SIGNAL(clicked()), this, SLOT(some_function())); btn_search = new QPushButton("Search", this);Also, better switch to the current standard way to establish a connection:
connect(btn_search, &QPushButton::clicked, this, &window::some_function);@Pl45m4 Thank you, it was the problem of flipping those two, so indeed a rookie mistake.
I am using some older books to learn Qt, so they use old notation. My projects worked fine every time, is it a problem that I am using that syntax to connect signals and slots? Thanks -
M mono has marked this topic as solved on
-
M mono has marked this topic as unsolved on
-
M mono has marked this topic as solved on
-
@Pl45m4 Thank you, it was the problem of flipping those two, so indeed a rookie mistake.
I am using some older books to learn Qt, so they use old notation. My projects worked fine every time, is it a problem that I am using that syntax to connect signals and slots? Thanks@mono not a problem per se but you are losing on compiler generated error vs runtime debug output.
One of the advantages is that if you modify a signal or a slot and forget to update the counterpart accordingly, you will know something is off at compile time rather than maybe missing it in logs of your application.