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.
  • 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 Online
                    J.HilkJ Online
                    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