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. signal and slots between two classes
QtWS25 Last Chance

signal and slots between two classes

Scheduled Pinned Locked Moved General and Desktop
31 Posts 5 Posters 9.7k Views
  • 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.
  • M Offline
    M Offline
    marlenet15
    wrote on last edited by
    #1

    I have two classes: signin and dialog. When I click a pushbutton on signin I want dialog to perform a slot. I have looked at forums and I believe I did the code correctly and I have no errors. However, when I try click the button nothing happens.

    signin.h

    #define SIGNIN_H
    
    #include <QDialog>
    
    namespace Ui {
    class signin;
    }
    
    class signin : public QDialog
    {
        Q_OBJECT
    
    public:
        explicit signin(QWidget *parent = 0);
        ~signin();
    
    signals:
        void clickedNextSignal();
    
    private:
        Ui::signin *ui;
    };
    
    #endif // SIGNIN_H
    

    signin.cpp

    #include "ui_signin.h"
    
    signin::signin(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::signin)
    {
        ui->setupUi(this);
    
        connect(ui->nextButtonSignin, SIGNAL(clicked()),this,SIGNAL(clickedNextSignal()));
    }
    
    signin::~signin()
    {
        delete ui;
    }
    
    

    dialog.h

    #define DIALOG_H
    
    #include <QDialog>
    #include "signin.h"
    #include "ui_signin.h"
    
    namespace Ui {
    class Dialog;
    }
    
    class Dialog : public QDialog
    {
      Q_OBJECT
    
    public:
      explicit Dialog(QWidget *parent = 0);
      ~Dialog();
    
    public slots:
      void openBox();
    
    private:
      Ui::Dialog *ui;
    
    protected:
      signin *_signin;
    
    };
    
    #endif // DIALOG_H
    

    dialog.cpp

    #include "ui_dialog.h"
    #include <QMessageBox>
    
    Dialog::Dialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::Dialog)
    {
        ui->setupUi(this);
    
        _signin = new signin(this);
    
        connect(_signin , SIGNAL(clickedNextSignal()),this,SLOT(openBox()));
    }
    
    void Dialog::openBox()
    {
        QMessageBox::information(this,tr("title"),tr("hi"));
    }
    
    Dialog::~Dialog()
    {
        delete ui;
    }
    
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      One thing I saw
      connect(ui->nextButtonSignin, SIGNAL(clicked()),this,SIGNAL(clickedNextSignal()));
      should have been SLOT in last.

      If possible, use the new syntax as it has more type checking

      connect( ui->nextButtonSignin, &QPushButton::clicked, this, &Dialog::clickedNextSignal);
      (If nextButtonSignin is QPushbutton)

      Hmm, there is something odd going on:
      signin has signal clickedNextSignal but you never call
      emit clickedNextSignal()
      so in Dialog::Dialog
      when you say
      connect(_signin , SIGNAL(clickedNextSignal()),this,SLOT(openBox()));
      Its correct but since signin do not emit (send) signal then nothing will happen.

      So if you add a slot to sign in, lets cal lit DoNext (bad name:)
      and hook it up to the button
      connect(ui->nextButtonSignin, SIGNAL(clicked()),this,SLOT(DoNext()));
      and in
      void DoNext() {
      emit clickedNextSignal();
      }
      Then the connect in Dialog should be more fun,
      So in short.
      NextButton clicked goes to signin slot that emit clickedNextSignal signal , that is hooked up to Dialog openBox().

      Hope it helps.

      M 1 Reply Last reply
      0
      • M Offline
        M Offline
        marlenet15
        wrote on last edited by
        #3
        This post is deleted!
        mrjjM 1 Reply Last reply
        0
        • M marlenet15

          This post is deleted!

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

          @marlenet15
          Ok, as far as I know you cannot connect signal to signal but you can
          connect multiple signals to one slot.

          Did you place break point to see if the DoNext is called and
          also check in application output that none of the connets return fail.
          maybe even
          qDebug() << " result:" << connect(ui->nextButtonSignin, SIGNAL(clicked()),this,SLOT(DoNext())); for both connect to see
          if they are successful.

          JKSHJ 1 Reply Last reply
          0
          • mrjjM mrjj

            Hi
            One thing I saw
            connect(ui->nextButtonSignin, SIGNAL(clicked()),this,SIGNAL(clickedNextSignal()));
            should have been SLOT in last.

            If possible, use the new syntax as it has more type checking

            connect( ui->nextButtonSignin, &QPushButton::clicked, this, &Dialog::clickedNextSignal);
            (If nextButtonSignin is QPushbutton)

            Hmm, there is something odd going on:
            signin has signal clickedNextSignal but you never call
            emit clickedNextSignal()
            so in Dialog::Dialog
            when you say
            connect(_signin , SIGNAL(clickedNextSignal()),this,SLOT(openBox()));
            Its correct but since signin do not emit (send) signal then nothing will happen.

            So if you add a slot to sign in, lets cal lit DoNext (bad name:)
            and hook it up to the button
            connect(ui->nextButtonSignin, SIGNAL(clicked()),this,SLOT(DoNext()));
            and in
            void DoNext() {
            emit clickedNextSignal();
            }
            Then the connect in Dialog should be more fun,
            So in short.
            NextButton clicked goes to signin slot that emit clickedNextSignal signal , that is hooked up to Dialog openBox().

            Hope it helps.

            M Offline
            M Offline
            marlenet15
            wrote on last edited by
            #5

            @mrjj I read from somewhere that you can have connect with two signals. I did try your recommendation but nothing happens.

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

              also how does you main.cpp look like ?

              You show the Dialog instead of mainwindow?

              M 1 Reply Last reply
              0
              • mrjjM mrjj

                also how does you main.cpp look like ?

                You show the Dialog instead of mainwindow?

                M Offline
                M Offline
                marlenet15
                wrote on last edited by
                #7

                @mrjj

                #include "dialog.h"
                #include <QApplication>
                
                int main(int argc, char *argv[])
                {
                    QApplication a(argc, argv);
                    Dialog *w = new Dialog();
                    w->showNormal();
                
                    return a.exec();
                }``` 
                
                and yes I used Dialog instead of mainwindow
                mrjjM 1 Reply Last reply
                0
                • M marlenet15

                  @mrjj

                  #include "dialog.h"
                  #include <QApplication>
                  
                  int main(int argc, char *argv[])
                  {
                      QApplication a(argc, argv);
                      Dialog *w = new Dialog();
                      w->showNormal();
                  
                      return a.exec();
                  }``` 
                  
                  and yes I used Dialog instead of mainwindow
                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @marlenet15 said:
                  Hi I think the dialog in dialog mess up something.
                  Didnt work for me if I new the sign dialog in Start dialog constructor.
                  But it does work if I hooked it up outside.
                  Have a look at this example
                  https://www.dropbox.com/s/bvpps7qd8oovtjf/gonext.zip?dl=0
                  Its the same idea but created slightly different.

                  M 1 Reply Last reply
                  0
                  • mrjjM mrjj

                    @marlenet15 said:
                    Hi I think the dialog in dialog mess up something.
                    Didnt work for me if I new the sign dialog in Start dialog constructor.
                    But it does work if I hooked it up outside.
                    Have a look at this example
                    https://www.dropbox.com/s/bvpps7qd8oovtjf/gonext.zip?dl=0
                    Its the same idea but created slightly different.

                    M Offline
                    M Offline
                    marlenet15
                    wrote on last edited by
                    #9

                    @mrjj I followed your example but it is still not working.

                    mrjjM 1 Reply Last reply
                    0
                    • M marlenet15

                      @mrjj I followed your example but it is still not working.

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

                      @marlenet15
                      ??
                      so the getnext example which runs here do no work when you compile it?

                      M 1 Reply Last reply
                      0
                      • mrjjM mrjj

                        @marlenet15
                        ??
                        so the getnext example which runs here do no work when you compile it?

                        M Offline
                        M Offline
                        marlenet15
                        wrote on last edited by
                        #11

                        @mrjj ok it works. I redid my code using only QWidgets and it works. I think I know the problem and I forgot to mention it. I made QDialog a stacked widget and promoted the file signin.

                        1 Reply Last reply
                        0
                        • mrjjM mrjj

                          @marlenet15
                          Ok, as far as I know you cannot connect signal to signal but you can
                          connect multiple signals to one slot.

                          Did you place break point to see if the DoNext is called and
                          also check in application output that none of the connets return fail.
                          maybe even
                          qDebug() << " result:" << connect(ui->nextButtonSignin, SIGNAL(clicked()),this,SLOT(DoNext())); for both connect to see
                          if they are successful.

                          JKSHJ Offline
                          JKSHJ Offline
                          JKSH
                          Moderators
                          wrote on last edited by JKSH
                          #12

                          @mrjj said:

                          Ok, as far as I know you cannot connect signal to signal

                          You can. See the documentation:

                          You can connect as many signals as you want to a single slot, and a signal can be connected to as many slots as you need. It is even possible to connect a signal directly to another signal. (This will emit the second signal immediately whenever the first is emitted.)

                          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                          mrjjM 1 Reply Last reply
                          1
                          • JKSHJ JKSH

                            @mrjj said:

                            Ok, as far as I know you cannot connect signal to signal

                            You can. See the documentation:

                            You can connect as many signals as you want to a single slot, and a signal can be connected to as many slots as you need. It is even possible to connect a signal directly to another signal. (This will emit the second signal immediately whenever the first is emitted.)

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

                            @JKSH
                            Aha, that is news for me. so that is like a chain signal or almost like an alias.

                            @marlenet15
                            Super. not sure what the real reason was but as long as it works :)

                            M 1 Reply Last reply
                            0
                            • mrjjM mrjj

                              @JKSH
                              Aha, that is news for me. so that is like a chain signal or almost like an alias.

                              @marlenet15
                              Super. not sure what the real reason was but as long as it works :)

                              M Offline
                              M Offline
                              marlenet15
                              wrote on last edited by
                              #14

                              @mrjj Before creating the stackedwidget it worked. After adding it, it doesn't work anymore.

                              mrjjM 1 Reply Last reply
                              0
                              • M marlenet15

                                @mrjj Before creating the stackedwidget it worked. After adding it, it doesn't work anymore.

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

                                @marlenet15
                                Ok. do you promote a qDialog to a stackwidget ?
                                Not sure I fully understand.
                                At least I have never tried that.

                                M 1 Reply Last reply
                                0
                                • mrjjM mrjj

                                  @marlenet15
                                  Ok. do you promote a qDialog to a stackwidget ?
                                  Not sure I fully understand.
                                  At least I have never tried that.

                                  M Offline
                                  M Offline
                                  marlenet15
                                  wrote on last edited by
                                  #16

                                  @mrjj on the .ui file of the class dialog, I created a stackedwidget and promoted the file signin.h

                                  mrjjM 1 Reply Last reply
                                  0
                                  • M marlenet15

                                    @mrjj on the .ui file of the class dialog, I created a stackedwidget and promoted the file signin.h

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

                                    @marlenet15
                                    so the signin dialog was sort of inline to Dialog ?

                                    M 1 Reply Last reply
                                    0
                                    • mrjjM mrjj

                                      @marlenet15
                                      so the signin dialog was sort of inline to Dialog ?

                                      M Offline
                                      M Offline
                                      marlenet15
                                      wrote on last edited by
                                      #18

                                      @mrjj can you exaplain what you mean by that? Sorry

                                      mrjjM 1 Reply Last reply
                                      0
                                      • M marlenet15

                                        @mrjj can you exaplain what you mean by that? Sorry

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

                                        @marlenet15
                                        Hi np.
                                        Im not 100% sure what you did.
                                        If you promote a widget to a dialog.
                                        the dialog will be sort of inside the parent/widget that contains the
                                        promoted widget.
                                        So I wondered if you promoted a stacked widget to the signin dialog ?

                                        M 1 Reply Last reply
                                        0
                                        • mrjjM mrjj

                                          @marlenet15
                                          Hi np.
                                          Im not 100% sure what you did.
                                          If you promote a widget to a dialog.
                                          the dialog will be sort of inside the parent/widget that contains the
                                          promoted widget.
                                          So I wondered if you promoted a stacked widget to the signin dialog ?

                                          M Offline
                                          M Offline
                                          marlenet15
                                          wrote on last edited by
                                          #20

                                          @mrjj I created a stackedwidget in dialog.ui, in there I promoted signin.h into the stackedwidget.

                                          JKSHJ 1 Reply Last reply
                                          0

                                          • Login

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