Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem connecting Signals and Slots in a modal dialog
Forum Updated to NodeBB v4.3 + New Features

Problem connecting Signals and Slots in a modal dialog

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 3 Posters 1.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    eschmo
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      First thing is to check that connect returns true to see if its accepted

      qDebug() << "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_Syntax

      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 ?

      E 1 Reply Last reply
      1
      • mrjjM mrjj

        Hi
        First thing is to check that connect returns true to see if its accepted

        qDebug() << "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_Syntax

        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 ?

        E Offline
        E Offline
        eschmo
        wrote on last edited by eschmo
        #3

        @mrjj

        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?

        mrjjM 1 Reply Last reply
        0
        • E eschmo

          @mrjj

          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?

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @eschmo

          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 *SettingsForm

          and we then new one of that type.

          E 1 Reply Last reply
          1
          • mrjjM mrjj

            @eschmo

            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 *SettingsForm

            and we then new one of that type.

            E Offline
            E Offline
            eschmo
            wrote on last edited by
            #5

            @mrjj

            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.

            mrjjM 1 Reply Last reply
            0
            • E eschmo

              @mrjj

              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.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @eschmo
              Hi
              So now the right dialog show up but pressing your ui->Btn_Save_Settings
              does nothing ?

              E 1 Reply Last reply
              0
              • mrjjM mrjj

                @eschmo
                Hi
                So now the right dialog show up but pressing your ui->Btn_Save_Settings
                does nothing ?

                E Offline
                E Offline
                eschmo
                wrote on last edited by
                #7

                @mrjj

                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
                
                mrjjM J.HilkJ 2 Replies Last reply
                0
                • E eschmo

                  @mrjj

                  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
                  
                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @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.

                  E 1 Reply Last reply
                  0
                  • mrjjM mrjj

                    @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.

                    E Offline
                    E Offline
                    eschmo
                    wrote on last edited by
                    #9

                    @mrjj

                    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.

                    mrjjM 1 Reply Last reply
                    0
                    • E eschmo

                      @mrjj

                      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.

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @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 ?

                      E 1 Reply Last reply
                      0
                      • mrjjM mrjj

                        @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 ?

                        E Offline
                        E Offline
                        eschmo
                        wrote on last edited by
                        #11

                        @mrjj

                        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...

                        mrjjM 1 Reply Last reply
                        0
                        • E eschmo

                          @mrjj

                          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...

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @eschmo

                          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.

                          1 Reply Last reply
                          0
                          • E eschmo

                            @mrjj

                            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
                            
                            J.HilkJ Online
                            J.HilkJ Online
                            J.Hilk
                            Moderators
                            wrote on last edited by mrjj
                            #13

                            @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?


                            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                            Q: What's that?
                            A: It's blue light.
                            Q: What does it do?
                            A: It turns blue.

                            E 1 Reply Last reply
                            1
                            • J.HilkJ J.Hilk

                              @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?

                              E Offline
                              E Offline
                              eschmo
                              wrote on last edited by
                              #14

                              @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 of

                              frm_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!

                              J.HilkJ 1 Reply Last reply
                              2
                              • E eschmo

                                @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 of

                                frm_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!

                                J.HilkJ Online
                                J.HilkJ Online
                                J.Hilk
                                Moderators
                                wrote on last edited by J.Hilk
                                #15

                                @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


                                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                Q: What's that?
                                A: It's blue light.
                                Q: What does it do?
                                A: It turns blue.

                                E 1 Reply Last reply
                                3
                                • mrjjM Offline
                                  mrjjM Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on last edited by mrjj
                                  #16

                                  Hi
                                  Also a note

                                  You 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 :)
                                  alt text

                                  1 Reply Last reply
                                  3
                                  • J.HilkJ J.Hilk

                                    @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

                                    E Offline
                                    E Offline
                                    eschmo
                                    wrote on last edited by
                                    #17

                                    @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.HilkJ 1 Reply Last reply
                                    2
                                    • E eschmo

                                      @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.HilkJ Online
                                      J.HilkJ Online
                                      J.Hilk
                                      Moderators
                                      wrote on last edited by
                                      #18

                                      @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
                                      d87a69c8-6117-4c3b-9fe0-b70c5734e36a-image.png


                                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                      Q: What's that?
                                      A: It's blue light.
                                      Q: What does it do?
                                      A: It turns blue.

                                      1 Reply Last reply
                                      1

                                      • Login

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • Users
                                      • Groups
                                      • Search
                                      • Get Qt Extensions
                                      • Unsolved