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. Need help passing a variable to another Form (Signals/Slots)
Forum Updated to NodeBB v4.3 + New Features

Need help passing a variable to another Form (Signals/Slots)

Scheduled Pinned Locked Moved Solved General and Desktop
25 Posts 5 Posters 4.8k 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.
  • mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by mrjj
    #9

    Hi
    you also need
    class Option: QSqlDatabase
    {
    Q_OBJECT
    ...

    Q_OBJECT is needed for signals & slots.

    note, after adding it, select clean all, rebuild all to make sure its seen.

    ? 1 Reply Last reply
    1
    • mrjjM mrjj

      Hi
      you also need
      class Option: QSqlDatabase
      {
      Q_OBJECT
      ...

      Q_OBJECT is needed for signals & slots.

      note, after adding it, select clean all, rebuild all to make sure its seen.

      ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #10

      @mrjj said in Need help passing a variable to another Form (Signals/Slots):

      Hi
      you also need
      class Option: QSqlDatabase
      {
      Q_OBJECT
      ...

      Q_OBJECT is needed for signals & slots.

      note, after adding it, select clean all, rebuild all to make sure its seen.

      Hello,
      just did that.
      After this I instantiated another object of the type Option in the dialog.h:

      #include <QDialog>
      #include "functions.h"
      
      namespace Ui {
      class Dialog;
      }
      
      class Dialog : public QDialog
      {
          Q_OBJECT
      
      public:
          explicit Dialog(QWidget *parent = nullptr);
          Option opt_dia; //note #1
          ~Dialog();
      
      private:
          Ui::Dialog *ui;
          void load_meta();
          int mode;
      
      private slots:
          void on_pushButton_ok_clicked();
      };
      

      #1 - This causes an error telling me I have an semantic error on QObject::connect(). Making it static seems to solve this, but then I get the following compiling errors:

      no matching function for call to 'QObject::connectImpl(const Object*&, void**, const Object*&, void**, QtPrivate::QSlotObject<void (Option::*)(QSqlDatabase, int), QtPrivate::List<QSqlDatabase, int>, void>*, Qt::ConnectionType&, const int*&, const QMetaObject*)'
      
      invalid static_cast from type 'QObject*' to type 'QtPrivate::FunctionPointer<void (Option::*)(QSqlDatabase, int)>::Object* {aka Option*}'
      

      fyi, the Object class now looks like this:

      #include <QtSql>
      #include <QSqlDatabase>
      #include <QTableView>
      
      class Option: QSqlDatabase
      {
          Q_OBJECT
      private:
          QSqlDatabase db;
          int mode;
      
      public:
          QSqlDatabase get_db(){return this->db;}
          Option()
          {
              this->db = QSqlDatabase::addDatabase("QMYSQL");
              this->mode = 0;
          }
          virtual ~Option();
          int get_mode(){return this->mode;}
      
      signals:
          void send_option(QSqlDatabase db, int mode){ emit set_up(db, mode); }
      
      public slots:
          void set_up(QSqlDatabase db, int mode){ this->db = db; this->mode = mode; }
      };
      
      jsulmJ 1 Reply Last reply
      0
      • ? A Former User

        @mrjj said in Need help passing a variable to another Form (Signals/Slots):

        Hi
        you also need
        class Option: QSqlDatabase
        {
        Q_OBJECT
        ...

        Q_OBJECT is needed for signals & slots.

        note, after adding it, select clean all, rebuild all to make sure its seen.

        Hello,
        just did that.
        After this I instantiated another object of the type Option in the dialog.h:

        #include <QDialog>
        #include "functions.h"
        
        namespace Ui {
        class Dialog;
        }
        
        class Dialog : public QDialog
        {
            Q_OBJECT
        
        public:
            explicit Dialog(QWidget *parent = nullptr);
            Option opt_dia; //note #1
            ~Dialog();
        
        private:
            Ui::Dialog *ui;
            void load_meta();
            int mode;
        
        private slots:
            void on_pushButton_ok_clicked();
        };
        

        #1 - This causes an error telling me I have an semantic error on QObject::connect(). Making it static seems to solve this, but then I get the following compiling errors:

        no matching function for call to 'QObject::connectImpl(const Object*&, void**, const Object*&, void**, QtPrivate::QSlotObject<void (Option::*)(QSqlDatabase, int), QtPrivate::List<QSqlDatabase, int>, void>*, Qt::ConnectionType&, const int*&, const QMetaObject*)'
        
        invalid static_cast from type 'QObject*' to type 'QtPrivate::FunctionPointer<void (Option::*)(QSqlDatabase, int)>::Object* {aka Option*}'
        

        fyi, the Object class now looks like this:

        #include <QtSql>
        #include <QSqlDatabase>
        #include <QTableView>
        
        class Option: QSqlDatabase
        {
            Q_OBJECT
        private:
            QSqlDatabase db;
            int mode;
        
        public:
            QSqlDatabase get_db(){return this->db;}
            Option()
            {
                this->db = QSqlDatabase::addDatabase("QMYSQL");
                this->mode = 0;
            }
            virtual ~Option();
            int get_mode(){return this->mode;}
        
        signals:
            void send_option(QSqlDatabase db, int mode){ emit set_up(db, mode); }
        
        public slots:
            void set_up(QSqlDatabase db, int mode){ this->db = db; this->mode = mode; }
        };
        
        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #11

        @SnuggleKat said in Need help passing a variable to another Form (Signals/Slots):

        This causes an error telling me I have an semantic error on QObject::connect(). Making it static seems to solve this, but then I get the following compiling errors:

        Please show your exact connect call

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        ? 1 Reply Last reply
        0
        • jsulmJ jsulm

          @SnuggleKat said in Need help passing a variable to another Form (Signals/Slots):

          This causes an error telling me I have an semantic error on QObject::connect(). Making it static seems to solve this, but then I get the following compiling errors:

          Please show your exact connect call

          ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #12

          @jsulm I have found a little mistake. I accidentally assigned the emit call to the signal instead of the slot. After correcting this I tried connecting 2 instances of my class Option inside the MainWindow class which worked (I think). I still can't see an instance of Option outside the MainWindow class.

          functions.h

          #include <QtSql>
          #include <QSqlDatabase>
          #include <QTableView>
          
          class Option: public QObject
          {
              Q_OBJECT
          private:
              QSqlDatabase db;
              int mode;
          
          public:
              QSqlDatabase get_db(){return this->db;}
              int get_mode(){return this->mode;}
          
          signals:
              void send_option(QSqlDatabase db, int mode);
          
          public slots:
              void set_up(QSqlDatabase db, int mode){ this->db = db; this->mode = mode; emit send_option(db, mode);}
          };
          

          mainwindow.h

          //...
          class MainWindow : public QMainWindow
          {
              Q_OBJECT
          
          public:
              explicit MainWindow(QWidget *parent = nullptr);
              ~MainWindow();
          
          private slots:
              void on_pushButton_connect_clicked();
              void on_pushButton_create_pdf_clicked();
              void on_pushButton_save_settings_clicked();
              void on_pushButton_open_clicked();
          
          private:
              Ui::MainWindow *ui;
              int create_pdf();
              Dialog dialog;
              QSqlDatabase db;
              Option opt_main, testopt; //testopt was the test-instance i used
          };
          

          mainwindow.cpp

          //...
          QObject::connect(&opt_main, &Option::send_option, &opt_dia, &Option::set_up); //opt_dia not declared in this scope
          //...
          void MainWindow::on_pushButton_open_clicked()
          {
              opt_main.send_option(db, 123);
              dialog.setModal(true);
              dialog.exec();
          }
          

          dialog.h

          //...
          #include "functions.h"
          
          namespace Ui {
          class Dialog;
          }
          
          class Dialog : public QDialog
          {
              Q_OBJECT
          
          public:
              explicit Dialog(QWidget *parent = nullptr);
              //QObject* x;
              Option opt_dia;
              ~Dialog();
          
          private:
              Ui::Dialog *ui;
              void load_meta();
              int mode;
          
          private slots:
              void on_pushButton_ok_clicked();
          };
          

          dialog.cpp

          //...
          void Dialog::on_pushButton_ok_clicked()
          {
              qDebug() << "mode: " + QString::number(opt_dia.get_mode()) + " - " + opt_dia.get_db().userName(); //testing the result
          }
          

          opt_dia cannot be found since it's not declared in this scope. If I use &Dialog::opt_dia it is found but the connection will fail to compile with the following error: Fehler: no matching member function for call to 'connect'

          jsulmJ 1 Reply Last reply
          0
          • ? A Former User

            @jsulm I have found a little mistake. I accidentally assigned the emit call to the signal instead of the slot. After correcting this I tried connecting 2 instances of my class Option inside the MainWindow class which worked (I think). I still can't see an instance of Option outside the MainWindow class.

            functions.h

            #include <QtSql>
            #include <QSqlDatabase>
            #include <QTableView>
            
            class Option: public QObject
            {
                Q_OBJECT
            private:
                QSqlDatabase db;
                int mode;
            
            public:
                QSqlDatabase get_db(){return this->db;}
                int get_mode(){return this->mode;}
            
            signals:
                void send_option(QSqlDatabase db, int mode);
            
            public slots:
                void set_up(QSqlDatabase db, int mode){ this->db = db; this->mode = mode; emit send_option(db, mode);}
            };
            

            mainwindow.h

            //...
            class MainWindow : public QMainWindow
            {
                Q_OBJECT
            
            public:
                explicit MainWindow(QWidget *parent = nullptr);
                ~MainWindow();
            
            private slots:
                void on_pushButton_connect_clicked();
                void on_pushButton_create_pdf_clicked();
                void on_pushButton_save_settings_clicked();
                void on_pushButton_open_clicked();
            
            private:
                Ui::MainWindow *ui;
                int create_pdf();
                Dialog dialog;
                QSqlDatabase db;
                Option opt_main, testopt; //testopt was the test-instance i used
            };
            

            mainwindow.cpp

            //...
            QObject::connect(&opt_main, &Option::send_option, &opt_dia, &Option::set_up); //opt_dia not declared in this scope
            //...
            void MainWindow::on_pushButton_open_clicked()
            {
                opt_main.send_option(db, 123);
                dialog.setModal(true);
                dialog.exec();
            }
            

            dialog.h

            //...
            #include "functions.h"
            
            namespace Ui {
            class Dialog;
            }
            
            class Dialog : public QDialog
            {
                Q_OBJECT
            
            public:
                explicit Dialog(QWidget *parent = nullptr);
                //QObject* x;
                Option opt_dia;
                ~Dialog();
            
            private:
                Ui::Dialog *ui;
                void load_meta();
                int mode;
            
            private slots:
                void on_pushButton_ok_clicked();
            };
            

            dialog.cpp

            //...
            void Dialog::on_pushButton_ok_clicked()
            {
                qDebug() << "mode: " + QString::number(opt_dia.get_mode()) + " - " + opt_dia.get_db().userName(); //testing the result
            }
            

            opt_dia cannot be found since it's not declared in this scope. If I use &Dialog::opt_dia it is found but the connection will fail to compile with the following error: Fehler: no matching member function for call to 'connect'

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #13

            @SnuggleKat Why don't you simply define the slot inside Dialog class?
            Exposing a member (opt_dia) as public to outside world is bad design.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            ? 1 Reply Last reply
            1
            • jsulmJ jsulm

              @SnuggleKat Why don't you simply define the slot inside Dialog class?
              Exposing a member (opt_dia) as public to outside world is bad design.

              ? Offline
              ? Offline
              A Former User
              wrote on last edited by A Former User
              #14

              @jsulm said in Need help passing a variable to another Form (Signals/Slots):

              @SnuggleKat Why don't you simply define the slot inside Dialog class?
              Exposing a member (opt_dia) as public to outside world is bad design.

              I'm aware it's bad design. That's why I asked what'd be the best way to instantiate it.
              Is it the right way to access the signal through opt_dia if I define the slot inside the Dialog class?
              And what should the connect() look like now?

              jsulmJ 1 Reply Last reply
              0
              • ? A Former User

                @jsulm said in Need help passing a variable to another Form (Signals/Slots):

                @SnuggleKat Why don't you simply define the slot inside Dialog class?
                Exposing a member (opt_dia) as public to outside world is bad design.

                I'm aware it's bad design. That's why I asked what'd be the best way to instantiate it.
                Is it the right way to access the signal through opt_dia if I define the slot inside the Dialog class?
                And what should the connect() look like now?

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by jsulm
                #15

                @SnuggleKat said in Need help passing a variable to another Form (Signals/Slots):

                When I define the slot inside the Dialog class is it the right way to access the signal through opt_dia?

                I don't understand this - what signal do you want to access from opt_dia?
                Connect would be:

                connect(&opt_main, &Option::send_option, &dialog, &Dialog::set_up);
                

                Actually I don't see a need for signal/slot here as dialog is a member of MainWindow. set_up can be a normal method which you call like any other without emiting a signal:

                dialog.set_up(...);
                

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                ? 1 Reply Last reply
                0
                • jsulmJ jsulm

                  @SnuggleKat said in Need help passing a variable to another Form (Signals/Slots):

                  When I define the slot inside the Dialog class is it the right way to access the signal through opt_dia?

                  I don't understand this - what signal do you want to access from opt_dia?
                  Connect would be:

                  connect(&opt_main, &Option::send_option, &dialog, &Dialog::set_up);
                  

                  Actually I don't see a need for signal/slot here as dialog is a member of MainWindow. set_up can be a normal method which you call like any other without emiting a signal:

                  dialog.set_up(...);
                  
                  ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #16

                  @jsulm Sorry, I meant to ask if ithis would be the right way of doing it:

                  public slots:
                     void set_up(QSqlDatabase db, int mode){ this->db = db; this->mode = mode; emit opt_dia.send_option(db, mode);}
                  

                  Change the connect() method as suggestion but the values don't get passed over.
                  Same for adding dialog.set_up();
                  Should I make QSqlDatabase db and int mode static?

                  jsulmJ 1 Reply Last reply
                  0
                  • ? A Former User

                    @jsulm Sorry, I meant to ask if ithis would be the right way of doing it:

                    public slots:
                       void set_up(QSqlDatabase db, int mode){ this->db = db; this->mode = mode; emit opt_dia.send_option(db, mode);}
                    

                    Change the connect() method as suggestion but the values don't get passed over.
                    Same for adding dialog.set_up();
                    Should I make QSqlDatabase db and int mode static?

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by jsulm
                    #17

                    @SnuggleKat "Change the connect() method as suggestion but the values don't get passed over." - please show the code.
                    "Should I make QSqlDatabase db and int mode static?" - please don't! Static variables are bad design in most cases.

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    ? 1 Reply Last reply
                    0
                    • jsulmJ jsulm

                      @SnuggleKat "Change the connect() method as suggestion but the values don't get passed over." - please show the code.
                      "Should I make QSqlDatabase db and int mode static?" - please don't! Static variables are bad design in most cases.

                      ? Offline
                      ? Offline
                      A Former User
                      wrote on last edited by
                      #18

                      @jsulm said in Need help passing a variable to another Form (Signals/Slots):

                      @SnuggleKat "Change the connect() method as suggestion but the values don't get passed over." - please show the code.
                      "Should I make QSqlDatabase db and int mode static?" - please don't! Static variables are bad design in most cases.

                      That's what I thought.

                      void MainWindow::on_pushButton_open_clicked()
                      {
                          opt_main.send_option(db, 123);
                          QObject::connect(&opt_main, &Option::send_option, &dialog, &Dialog::set_up);
                          dialog.set_up(db, 123); //also tried this which didn't help either
                          dialog.setModal(true);
                          dialog.exec();
                      }
                      
                      jsulmJ 1 Reply Last reply
                      0
                      • ? A Former User

                        @jsulm said in Need help passing a variable to another Form (Signals/Slots):

                        @SnuggleKat "Change the connect() method as suggestion but the values don't get passed over." - please show the code.
                        "Should I make QSqlDatabase db and int mode static?" - please don't! Static variables are bad design in most cases.

                        That's what I thought.

                        void MainWindow::on_pushButton_open_clicked()
                        {
                            opt_main.send_option(db, 123);
                            QObject::connect(&opt_main, &Option::send_option, &dialog, &Dialog::set_up);
                            dialog.set_up(db, 123); //also tried this which didn't help either
                            dialog.setModal(true);
                            dialog.exec();
                        }
                        
                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #19

                        @SnuggleKat I really don't get why you use signals slots here?

                        void MainWindow::on_pushButton_open_clicked()
                        {
                            dialog.set_up(db, 123); //also tried this which didn't help either
                            dialog.setModal(true);
                            dialog.exec();
                        }
                        

                        "//also tried this which didn't help either" - please show what dialog.set_up(db, 123) is doing.

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        ? 1 Reply Last reply
                        0
                        • jsulmJ jsulm

                          @SnuggleKat I really don't get why you use signals slots here?

                          void MainWindow::on_pushButton_open_clicked()
                          {
                              dialog.set_up(db, 123); //also tried this which didn't help either
                              dialog.setModal(true);
                              dialog.exec();
                          }
                          

                          "//also tried this which didn't help either" - please show what dialog.set_up(db, 123) is doing.

                          ? Offline
                          ? Offline
                          A Former User
                          wrote on last edited by
                          #20

                          @jsulm
                          qDebug() shows me that it effectively does nothing. dialog.set_up() does not touch the object declared as opt_dia.

                          "mode: 12141696 - "
                          

                          12141696 seems to be a random value.
                          This is the qDebug() line:

                          qDebug() << "mode: " + QString::number(opt_dia.get_mode()) + " - " + opt_dia.get_db().userName();
                          
                          jsulmJ 1 Reply Last reply
                          0
                          • ? A Former User

                            @jsulm
                            qDebug() shows me that it effectively does nothing. dialog.set_up() does not touch the object declared as opt_dia.

                            "mode: 12141696 - "
                            

                            12141696 seems to be a random value.
                            This is the qDebug() line:

                            qDebug() << "mode: " + QString::number(opt_dia.get_mode()) + " - " + opt_dia.get_db().userName();
                            
                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #21

                            @SnuggleKat Can you show the content of dialog.set_up(...)?

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            ? 1 Reply Last reply
                            0
                            • jsulmJ jsulm

                              @SnuggleKat Can you show the content of dialog.set_up(...)?

                              ? Offline
                              ? Offline
                              A Former User
                              wrote on last edited by
                              #22

                              @jsulm Do you mean this?

                              public slots:
                                 void set_up(QSqlDatabase db, int mode){ this->db = db; this->mode = mode; emit opt_dia.send_option(db, mode);}
                              
                              jsulmJ 1 Reply Last reply
                              0
                              • ? A Former User

                                @jsulm Do you mean this?

                                public slots:
                                   void set_up(QSqlDatabase db, int mode){ this->db = db; this->mode = mode; emit opt_dia.send_option(db, mode);}
                                
                                jsulmJ Offline
                                jsulmJ Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on last edited by
                                #23

                                @SnuggleKat set_up(

                                public slots:
                                   void set_up(QSqlDatabase db, int mode){ this->db = db; this->mode = mode; opt_dia.set_up(db, mode); }
                                

                                https://forum.qt.io/topic/113070/qt-code-of-conduct

                                ? 1 Reply Last reply
                                0
                                • J.HilkJ Offline
                                  J.HilkJ Offline
                                  J.Hilk
                                  Moderators
                                  wrote on last edited by J.Hilk
                                  #24

                                  I admit, I'm a little bit out of my field here, but can you simply reassign a QSqlDatabase in that way?

                                  I usually pass a QSqlDatabase-pointer between classes.


                                  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
                                  • jsulmJ jsulm

                                    @SnuggleKat set_up(

                                    public slots:
                                       void set_up(QSqlDatabase db, int mode){ this->db = db; this->mode = mode; opt_dia.set_up(db, mode); }
                                    
                                    ? Offline
                                    ? Offline
                                    A Former User
                                    wrote on last edited by
                                    #25

                                    @jsulm
                                    Okay, it now works! Thank you.
                                    I also hat to re-write a set_up() function inside the Dialog class.
                                    I will clean up the way it's done by now and remember it for the next time

                                    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