Problem connecting Signals and Slots in a modal dialog
-
Hello @ all.
Actually i'm facing an issue (maybe my fault?!?) with connecting a Tab Widget and a button on a modal dialog.
Basically i've prepared a Settings Dialog which is started from the Main Dialog by clicking a Button.
SettingsForm =new QDialog; SettingsForm->setModal(true); SettingsForm->setWindowFlags(Qt::FramelessWindowHint| Qt::WindowSystemMenuHint); frm_Settings.setupUi(SettingsForm); SettingsForm->show();
The Settings Dialog is basically setup with a Tab Widget and 2 Buttons (Save,Cancel)
Now i want to connect the tabWidget->currentChanged(int) to start a self defined private Slot onTabChanged and the ButtonSave->clicked to the modal dialogs close():
Unfortunately it doesnt work at all but i also dont get any error on the console.
The connect of the cancel button is done by the designers Signal&Slots Module to modal dialogs close() function and it works pretty well.Maybe anybody has an idea what i do wrong?
#ifndef H17T_SETTINGS_H #define H17T_SETTINGS_H #include <QDialog> #include <QMessageBox> namespace Ui{ class H17T_Settings; } class H17T_Settings : public QDialog { Q_OBJECT public: explicit H17T_Settings(QWidget *parent = nullptr); ~H17T_Settings(); private: Ui::H17T_Settings *ui; private slots: void onTabChanged(); }; #endif // H17T_SETTINGS_H
#include "h17t_settings.h" #include "ui_h17t_settings.h" H17T_Settings::H17T_Settings(QWidget *parent) : QDialog(parent, Qt::CustomizeWindowHint), ui(new Ui::H17T_Settings) { ui->setupUi(this); connect(ui->tabWidget,SIGNAL(currentChanged(int)),this,SLOT(onTabChanged())); connect(ui->Btn_Save_Settings,SIGNAL(clicked()),this,SLOT(close())); } void H17T_Settings::onTabChanged() { QString curWidget = ui->tabWidget->currentWidget()->objectName(); ui->debugLabel->setText("Haut hin"); } H17T_Settings::~H17T_Settings() { delete ui; }
thanks a lot in advance!
-
Hi
First thing is to check that connect returns true to see if its acceptedqDebug() << "works:" << connect(ui->tabWidget,SIGNAL(currentChanged(int)),this,SLOT(onTabChanged()));
Or use the new syntax that catches errors at compile time
https://wiki.qt.io/New_Signal_Slot_SyntaxOne thing I did wonder.
You show
SettingsForm =new QDialog;That is creating a new normal Qt Dialog.
However, you dialog isH17T_Settings so
I assume you new this in some code you have not shown ? -
Hi
First thing is to check that connect returns true to see if its acceptedqDebug() << "works:" << connect(ui->tabWidget,SIGNAL(currentChanged(int)),this,SLOT(onTabChanged()));
Or use the new syntax that catches errors at compile time
https://wiki.qt.io/New_Signal_Slot_SyntaxOne thing I did wonder.
You show
SettingsForm =new QDialog;That is creating a new normal Qt Dialog.
However, you dialog isH17T_Settings so
I assume you new this in some code you have not shown ?Thanks for your input.
i did try debug messages from the H17T_Settings class's constructor but i dont get any output.
So i suggest, the constructor will not be called in anyway...unfortunately i dont get it.
Maybe there is something wrong with my call. Of the DIalog as you may suggested?@mrjj said in Problem connecting Signals and Slots in a modal dialog:
One thing I did wonder.
You show
SettingsForm =new QDialog;That is creating a new normal Qt Dialog.
However, you dialog isH17T_Settings so
I assume you new this in some code you have not shown ?what exactely do you mean? frm_Settings is type H17T_Settings if you mean that?
-
Thanks for your input.
i did try debug messages from the H17T_Settings class's constructor but i dont get any output.
So i suggest, the constructor will not be called in anyway...unfortunately i dont get it.
Maybe there is something wrong with my call. Of the DIalog as you may suggested?@mrjj said in Problem connecting Signals and Slots in a modal dialog:
One thing I did wonder.
You show
SettingsForm =new QDialog;That is creating a new normal Qt Dialog.
However, you dialog isH17T_Settings so
I assume you new this in some code you have not shown ?what exactely do you mean? frm_Settings is type H17T_Settings if you mean that?
Hi
If the debug dont show up then I also doubt that the constructor is called.What I talk about is the creation code you shown.
SettingsForm =new QDialog;
I would expect it to be
SettingsForm =new H17T_Settings;
So we do use your new class and not just the default Qt QDialog.
so SettingsForm
is defined as
H17T_Settings *SettingsFormand we then new one of that type.
-
Hi
If the debug dont show up then I also doubt that the constructor is called.What I talk about is the creation code you shown.
SettingsForm =new QDialog;
I would expect it to be
SettingsForm =new H17T_Settings;
So we do use your new class and not just the default Qt QDialog.
so SettingsForm
is defined as
H17T_Settings *SettingsFormand we then new one of that type.
You are absolutely right.
I've corrected it and now i get the debug messages! Thanks!
Tab Connect: true Button Connect: true
But wether my slot neither the dialog close will be called.
Button Cancel calls close() through the ui_h17t_Settings.h without any problem. -
You are absolutely right.
I've corrected it and now i get the debug messages! Thanks!
Tab Connect: true Button Connect: true
But wether my slot neither the dialog close will be called.
Button Cancel calls close() through the ui_h17t_Settings.h without any problem. -
@eschmo
Hi
So now the right dialog show up but pressing your ui->Btn_Save_Settings
does nothing ?Yes exactely, bu it was the right Dialog all the time but obviously just the appearance not the code.
I've also switched to the new connection syntax but debug shows true for the old way as well.qDebug() << "Tab Connect:" <<connect(ui->tabWidget,&QTabWidget::currentChanged,this,&H17T_Settings::onTabChanged); qDebug() << "Button Connect:" <<connect(ui->Btn_Save_Settings,&QPushButton::clicked,this,&H17T_Settings::close);
Tab Connect: true Button Connect: true
-
Yes exactely, bu it was the right Dialog all the time but obviously just the appearance not the code.
I've also switched to the new connection syntax but debug shows true for the old way as well.qDebug() << "Tab Connect:" <<connect(ui->tabWidget,&QTabWidget::currentChanged,this,&H17T_Settings::onTabChanged); qDebug() << "Button Connect:" <<connect(ui->Btn_Save_Settings,&QPushButton::clicked,this,&H17T_Settings::close);
Tab Connect: true Button Connect: true
@eschmo
Hi
Super with the new syntax as then, we are very sure it's not the connect.Try replace the close() with accept();
and see if it then closes.btw does Settings::onTabChange gets called ?
If not I think its something else.
-
@eschmo
Hi
Super with the new syntax as then, we are very sure it's not the connect.Try replace the close() with accept();
and see if it then closes.btw does Settings::onTabChange gets called ?
If not I think its something else.
-
no, nothing is called at all.
I've redone the connection for the "Save Button" with the designer and it worked straight afterwards.
I did it again in the cpp and it doesnt work. thats quite irritating.@eschmo
That is very odd.You only create a H17T_Settings once place right ?
and if it works assigning a slot in Designer for the same dialog then it must work too
with manual connect as its much the same.Just to be sure. You go to Designer and use the "go to slot" function to create a slot
for it and there you call close()but if you don't do this and just have the manual connects it wont work ?
-
@eschmo
That is very odd.You only create a H17T_Settings once place right ?
and if it works assigning a slot in Designer for the same dialog then it must work too
with manual connect as its much the same.Just to be sure. You go to Designer and use the "go to slot" function to create a slot
for it and there you call close()but if you don't do this and just have the manual connects it wont work ?
yes exactely, i'Ve even put the connection for tabwidget currentChange() in the ui file by hand and it works with H17T_Settings close().
But it will not work in anyway with my connections over the cpp, even if they succeed to connect.
Thats quite strange...
-
yes exactely, i'Ve even put the connection for tabwidget currentChange() in the ui file by hand and it works with H17T_Settings close().
But it will not work in anyway with my connections over the cpp, even if they succeed to connect.
Thats quite strange...
Yes its very strange as it should work when connect returns true and besides with the
new syntax, the compiler would tell if something fishy.All seems fine with H17T_Settings soI cant I guess why onTabChanged() is not called.
-
Yes exactely, bu it was the right Dialog all the time but obviously just the appearance not the code.
I've also switched to the new connection syntax but debug shows true for the old way as well.qDebug() << "Tab Connect:" <<connect(ui->tabWidget,&QTabWidget::currentChanged,this,&H17T_Settings::onTabChanged); qDebug() << "Button Connect:" <<connect(ui->Btn_Save_Settings,&QPushButton::clicked,this,&H17T_Settings::close);
Tab Connect: true Button Connect: true
@eschmo said in Problem connecting Signals and Slots in a modal dialog:
Yes exactely, bu it was the right Dialog all the time but obviously just the appearance not the code.
I've also switched to the new connection syntax but debug shows true for the old way as well.No it was not, It could not have been the correct dialog, when you instantiated a different dialog.
I think you have simply multiple dialogs instances ore something along that line.
can you post/link a minimal compellable example?
-
@eschmo said in Problem connecting Signals and Slots in a modal dialog:
Yes exactely, bu it was the right Dialog all the time but obviously just the appearance not the code.
I've also switched to the new connection syntax but debug shows true for the old way as well.No it was not, It could not have been the correct dialog, when you instantiated a different dialog.
I think you have simply multiple dialogs instances ore something along that line.
can you post/link a minimal compellable example?
@mrjj
yes i'm a little bit confused and lost about it.
anyway, thanks a lot for your help.@J-Hilk
it has appeared exactly as the dialog i've prepared in the designer.
Thats for sure because offrm_Settings.setupUi(SettingsForm);
and fits why not my overloaded constructor is in place but the standart one of QDialog.
I couldnt find any other instance in my code, but i dont see how this could affect the called dialog.
I've put the whole project to github if this could help.
https://github.com/eschmo/H17-Timing/tree/main/H17-Timing
Thanks a lot for your time!
-
@mrjj
yes i'm a little bit confused and lost about it.
anyway, thanks a lot for your help.@J-Hilk
it has appeared exactly as the dialog i've prepared in the designer.
Thats for sure because offrm_Settings.setupUi(SettingsForm);
and fits why not my overloaded constructor is in place but the standart one of QDialog.
I couldnt find any other instance in my code, but i dont see how this could affect the called dialog.
I've put the whole project to github if this could help.
https://github.com/eschmo/H17-Timing/tree/main/H17-Timing
Thanks a lot for your time!
@eschmo
inside your h17t_main.h
your SettingsForm member pointer is this:QDialog *SettingsForm
and it should be:
H17T_Settings *SettingsForm;
That should fix your issue, but from looking at the rest of your code:
- You're going to leak memory with H17T_Settings instances.
- You're set up to do threading stuff that apparently have access to GUI stuff
Both are a big nono... :D
-
Hi
Also a noteYou do
void H17T_Main::onSettings() { SettingsForm = new H17T_Settings; // this also calls setupUI ... frm_Settings.setupUi(SettingsForm); <<< what is this for ? now you create all the widgets once more so the other ones that was connected in the constructor is no longer the ones shown on screen. This should not be needed to call manually. SettingsForm->show(); }
That seems to be the reason as it works without that line :)
-
@eschmo
inside your h17t_main.h
your SettingsForm member pointer is this:QDialog *SettingsForm
and it should be:
H17T_Settings *SettingsForm;
That should fix your issue, but from looking at the rest of your code:
- You're going to leak memory with H17T_Settings instances.
- You're set up to do threading stuff that apparently have access to GUI stuff
Both are a big nono... :D
@J-Hilk said in Problem connecting Signals and Slots in a modal dialog:
QDialog
Sorry, not sure why this was in the files i've uploaded but this was allready changed yesterday.
Finally thanks a lot, because the solution was your second tip.
I've removed it completely and everything is fine now!
Thanks a lot to both of you!!
-
@J-Hilk said in Problem connecting Signals and Slots in a modal dialog:
QDialog
Sorry, not sure why this was in the files i've uploaded but this was allready changed yesterday.
Finally thanks a lot, because the solution was your second tip.
I've removed it completely and everything is fine now!
Thanks a lot to both of you!!
@eschmo said in Problem connecting Signals and Slots in a modal dialog:
I've removed it completely and everything is fine now!
I'm afraid that credit goes to @mrjj ;)
Sorry, not sure why this was in the files i've uploaded but this was allready changed yesterday.
I'm talking header, not cpp