Same slot name executing on parent.
-
Are parents suppose to execute slots of the same name as their children? This seems like a glitch to me.
I came across this issue and created a test project. Sure enough. If I have a slot (even if both are private), it will execute the slot on both the child and parent if the name is the same. Here is the code:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow),
_dialog(new TestDialog(this))
{
ui->setupUi(this);
_dialog->show();
}MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::on_pushButton_close_clicked()
{
qDebug()<<"Close on MainWindow"<<endl;
}TestDialog::TestDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::TestDialog)
{
ui->setupUi(this);
}TestDialog::~TestDialog()
{
delete ui;
}void TestDialog::on_pushButton_close_clicked()
{
qDebug()<<"Close on TestDialog"<<endl;
}The display is just a pushbutton on both the MainWindow and TestDialog. If I press "Close" on TestDialog, then the debugger will display the following:
Close on TestDialog
Close on MainWindow
This means it is executing the TestDialog slot then the MainWindow slot. Even though there is no connection. It just has the same name.
-
signal, child, qwidget?
What's your question?
-
Are parents suppose to execute slots of the same name as their children? This seems like a glitch to me.
I came across this issue and created a test project. Sure enough. If I have a slot (even if both are private), it will execute the slot on both the child and parent if the name is the same. Here is the code:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow),
_dialog(new TestDialog(this))
{
ui->setupUi(this);
_dialog->show();
}MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::on_pushButton_close_clicked()
{
qDebug()<<"Close on MainWindow"<<endl;
}TestDialog::TestDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::TestDialog)
{
ui->setupUi(this);
}TestDialog::~TestDialog()
{
delete ui;
}void TestDialog::on_pushButton_close_clicked()
{
qDebug()<<"Close on TestDialog"<<endl;
}The display is just a pushbutton on both the MainWindow and TestDialog. If I press "Close" on TestDialog, then the debugger will display the following:
Close on TestDialog
Close on MainWindow
This means it is executing the TestDialog slot then the MainWindow slot. Even though there is no connection. It just has the same name.
-
@Nekreg45
You appear to be using the auto-connect-by-name feature of slots via the UI Designer? You might look at the code generated, and try changing over to manually naming and connecting slots?I love people who heavily modify their posts afterwards... where's the downvote button??
-
I love people who heavily modify their posts afterwards... where's the downvote button??
@Christian-Ehrlicher Sorry, I accidentally submitted the post before I wrote it. I was scrambling to write it up. It was not my intention.
-
@Nekreg45
You appear to be using the auto-connect-by-name feature of slots via the UI Designer? You might look at the code generated, and try changing over to manually naming and connecting slots?@JonB I am using the auto-connect-by-name feature. I was able to fix this for my program by changing the name.
My question was whether this behavior was intended by Qt or is this a glitch. It seems very odd to me. I'm not sure why anyone would want this to happen.
-
@JonB I am using the auto-connect-by-name feature. I was able to fix this for my program by changing the name.
My question was whether this behavior was intended by Qt or is this a glitch. It seems very odd to me. I'm not sure why anyone would want this to happen.
@Nekreg45
Since you ask that, and your original questions statesAre parents suppose to execute slots of the same name as their children? This seems like a glitch to me.
I would start by reading QMetaObject::connectSlotsByName(QObject *object)
Searches recursively for all child objects of the given object, and connects matching signals from them to slots of object that follow the following form:
void on_<object name>_<signal name>(<signal parameters>);Note the recursively.
This only adds to the case that you should not be using the connect-by-name feature in the first place, because of such an issue and others.
-
@Nekreg45
Since you ask that, and your original questions statesAre parents suppose to execute slots of the same name as their children? This seems like a glitch to me.
I would start by reading QMetaObject::connectSlotsByName(QObject *object)
Searches recursively for all child objects of the given object, and connects matching signals from them to slots of object that follow the following form:
void on_<object name>_<signal name>(<signal parameters>);Note the recursively.
This only adds to the case that you should not be using the connect-by-name feature in the first place, because of such an issue and others.
@JonB Interesting. Thanks for the info. It still feels odd why it would search through other objects, but maybe there is grander purpose designed by Qt than what fits in my little example.
I've been using the connect-by-name feature for years and this is the first time I stumbled on this problem. I guess I should change up my habit in the future and create my own connects.
Thanks again!
-
@JonB Interesting. Thanks for the info. It still feels odd why it would search through other objects, but maybe there is grander purpose designed by Qt than what fits in my little example.
I've been using the connect-by-name feature for years and this is the first time I stumbled on this problem. I guess I should change up my habit in the future and create my own connects.
Thanks again!
@Nekreg45
See Nailing 13 signal and slot mistakes with clazy 1.3. That's KDAB and from 2018. It suggests:- connect-by-name
Warns when auto-connection slots are used. They’re also known as “connect by name”, an old and unpopular feature which shouldn’t be used anymore.