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. Cannot pass variable to another Dialog

Cannot pass variable to another Dialog

Scheduled Pinned Locked Moved Unsolved General and Desktop
18 Posts 5 Posters 4.7k Views 2 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.
  • T Offline
    T Offline
    t0msk
    wrote on last edited by
    #1

    Hello, I would like to pass variable into another Dialog, this is my code:

    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    
    db.setHostName("hostname");
    db.setDatabaseName("db");
    db.setUserName("root");
    db.setPassword("pass");
    
    MainWindow *window = new MainWindow();
    window->setAttribute(Qt::WA_DeleteOnClose);
    window->setData(db, store);
    window->show();
    

    header file:

    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
        void setData(QSqlDatabase db, Store store[5][5][10]);
    

    But I get an error:

    error: incompatible types in assignment of 'Store (**)[5][10]' to 'Store [5][5][10]'
         store = &store_init;
               ^
    

    Function setData:

    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    Store store[5][5][10];
    
    void MainWindow::setData(QSqlDatabase db_init, Store store_init[5][5][10])
    {
        db = db_init;
        store = &store_init;
    }
    

    Student who loves C/C++

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

      Hi,

      First thing, there's no need to pass db anywhere, you are using the default connection so every call you'd be making without passing the db parameter will use that default connection.

      You are trying to store the address of your tridimensional array rather than the array itself.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      T 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,

        First thing, there's no need to pass db anywhere, you are using the default connection so every call you'd be making without passing the db parameter will use that default connection.

        You are trying to store the address of your tridimensional array rather than the array itself.

        T Offline
        T Offline
        t0msk
        wrote on last edited by
        #3

        @SGaist said in Cannot pass variable to another Dialog:

        Hi,

        First thing, there's no need to pass db anywhere, you are using the default connection so every call you'd be making without passing the db parameter will use that default connection.

        You are trying to store the address of your tridimensional array rather than the array itself.

        So I changed it to this:

        void MainWindow::setData(QSqlDatabase db_init, Store store_init[5][5][10])
        {
            store = store_init;
        }
        

        And I got:

        error: incompatible types in assignment of 'Store (*)[5][10]' to 'Store [5][5][10]'
             store = store_init;
                   ^
        

        By the way I have no connection to mysql in second Dialog so this "First thing, there's no need to pass db anywhere, you are using the default connection" is not probably true

        Student who loves C/C++

        MikhailGM 1 Reply Last reply
        0
        • T t0msk

          @SGaist said in Cannot pass variable to another Dialog:

          Hi,

          First thing, there's no need to pass db anywhere, you are using the default connection so every call you'd be making without passing the db parameter will use that default connection.

          You are trying to store the address of your tridimensional array rather than the array itself.

          So I changed it to this:

          void MainWindow::setData(QSqlDatabase db_init, Store store_init[5][5][10])
          {
              store = store_init;
          }
          

          And I got:

          error: incompatible types in assignment of 'Store (*)[5][10]' to 'Store [5][5][10]'
               store = store_init;
                     ^
          

          By the way I have no connection to mysql in second Dialog so this "First thing, there's no need to pass db anywhere, you are using the default connection" is not probably true

          MikhailGM Offline
          MikhailGM Offline
          MikhailG
          wrote on last edited by MikhailG
          #4

          @t0msk Why do not you try this snippet of code to check if it work properly?

          void MainWindow::setData(QSqlDatabase db_init, Store*** store_init)
          {
              Store***  store = store_init;
          }
          
          T 1 Reply Last reply
          0
          • MikhailGM MikhailG

            @t0msk Why do not you try this snippet of code to check if it work properly?

            void MainWindow::setData(QSqlDatabase db_init, Store*** store_init)
            {
                Store***  store = store_init;
            }
            
            T Offline
            T Offline
            t0msk
            wrote on last edited by
            #5

            @MikhailG said in Cannot pass variable to another Dialog:

            @t0msk Why do not you try this snippet of code to check if it work properly?

            void MainWindow::setData(QSqlDatabase db_init, Store*** store_init)
            {
                Store***  store = store_init;
            }
            

            weird syntax

            Student who loves C/C++

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

              Your code doesn't show whether your opening the connection nor checking that it was successfully opened.

              The default connection behaviour is described in the details of the QSqlDatabase documentation and it's also used that way in all of Qt's database examples.

              Might be weird but that's what you are having, a three dimensional table.

              Store[5] => One dimensional table => Store *
              Store[5][5] => Two dimensional table => Store **
              Store[5][5][10] => Three dimensional table => Store ***
              

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • T Offline
                T Offline
                t0msk
                wrote on last edited by
                #7

                But now I have an error:

                error: no match for 'operator=' (operand types are 'Store' and 'Store****')
                     ***store = &store_init;
                              ^
                

                code:

                void MainWindow::setData(QSqlDatabase db_init, Store ***store_init)
                {
                    //db = db_init;
                    ***store = store_init;
                }
                

                Student who loves C/C++

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  You are dereferencing your pointer three time, that's why it complains.

                  Out of curiosity, what do you have in that three-dimensional table ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  T 1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mpergand
                    wrote on last edited by
                    #9

                    I think you need a pointer, for example:

                    char s[5][2];
                    char (*s2)[2]=s;  // pointer to array of two chars
                    

                    Passing an array to a function is the same as passing a pointer to the first dimension:

                    void myFunct(char s[5][2])
                    // is the same as:
                    void myFunct(char (*s)[2])
                    // more simply, you can omit the size of the first dimension:
                    void myFunct(char s[][2])
                    
                    1 Reply Last reply
                    0
                    • SGaistS SGaist

                      You are dereferencing your pointer three time, that's why it complains.

                      Out of curiosity, what do you have in that three-dimensional table ?

                      T Offline
                      T Offline
                      t0msk
                      wrote on last edited by t0msk
                      #10

                      @SGaist said in Cannot pass variable to another Dialog:

                      You are dereferencing your pointer three time, that's why it complains.

                      Out of curiosity, what do you have in that three-dimensional table ?

                      Three-dimensional array represents a 3D space in store.

                      My code:

                      Store store[5][5][10];
                      void MainWindow::setData(Store store_init[][5][10])
                      {
                          store = store_init;
                      }
                      
                      error: incompatible types in assignment of 'Store (*)[5][10]' to 'Store [5][5][10]'
                           store = store_init;
                                 ^
                      

                      Maybe I will set data using three nested loops :D

                      Student who loves C/C++

                      jsulmJ 1 Reply Last reply
                      0
                      • T t0msk

                        @SGaist said in Cannot pass variable to another Dialog:

                        You are dereferencing your pointer three time, that's why it complains.

                        Out of curiosity, what do you have in that three-dimensional table ?

                        Three-dimensional array represents a 3D space in store.

                        My code:

                        Store store[5][5][10];
                        void MainWindow::setData(Store store_init[][5][10])
                        {
                            store = store_init;
                        }
                        
                        error: incompatible types in assignment of 'Store (*)[5][10]' to 'Store [5][5][10]'
                             store = store_init;
                                   ^
                        

                        Maybe I will set data using three nested loops :D

                        jsulmJ Online
                        jsulmJ Online
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @t0msk You cannot do it like this. C/C++ will not copy the content of the array if you assign it to another array. Arrays are basically pointers in C/C++. And [5][5][10] is not the same as [][5][10] (here the first dimension is not specified)!
                        You should take a look at std::array.

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

                        1 Reply Last reply
                        2
                        • T Offline
                          T Offline
                          t0msk
                          wrote on last edited by
                          #12

                          I dont know how to copy content of whole array to another using 1 line, I solved it by loops:

                          int i, j, k;
                          
                          for(i = 0;i < 5;i++) {
                              for(j = 0;j < 5;j++) {
                                  for(k = 0;k < 10;k++) {
                                      store[i][j][k] = store_init[i][j][k];
                                  }
                              }
                          }
                          

                          Student who loves C/C++

                          jsulmJ 1 Reply Last reply
                          0
                          • T Offline
                            T Offline
                            t0msk
                            wrote on last edited by
                            #13

                            One more question.

                            Calling window:

                            int test = 7;
                            
                            Dialog *window = new Dialog();
                            window->setAttribute(Qt::WA_DeleteOnClose);
                            window->setData(test);
                            window->show();
                            

                            Dialog.h

                            public:
                                explicit Dialog(QWidget *parent = 0);
                                ~Dialog();
                                void setData(int test);
                            

                            Dialog.cpp

                            int second;
                            
                            Dialog::Dialog(QWidget *parent) :
                                QDialog(parent),
                                ui(new Ui::Dialog)
                            {
                                ui->setupUi(this);
                            
                                ui->label->setText(QString::number(second));
                            }
                            
                            void Dialog::setData(int test) {
                            
                                second = test;
                            }
                            
                            Dialog::~Dialog()
                            {
                                delete ui;
                            }
                            

                            Problem is that value in ui->label is 0 and not 7, why? :/

                            Student who loves C/C++

                            1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              Because it's test that is 7 and second is not initialised so it's pure luck that it's 0.

                              Interested in AI ? www.idiap.ch
                              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              0
                              • T Offline
                                T Offline
                                t0msk
                                wrote on last edited by
                                #15

                                And how to initialise "second"?

                                Student who loves C/C++

                                1 Reply Last reply
                                0
                                • SGaistS Offline
                                  SGaistS Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  Assign it a value in the constructor initializer list.

                                  Interested in AI ? www.idiap.ch
                                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  1 Reply Last reply
                                  0
                                  • M Offline
                                    M Offline
                                    mpergand
                                    wrote on last edited by
                                    #17
                                    void Dialog::setData(int test) {
                                      second = test;
                                     ui->label->setText(QString::number(second));
                                    }
                                    

                                    In the constructor, you can set the default value ( 0 ?)

                                    1 Reply Last reply
                                    0
                                    • T t0msk

                                      I dont know how to copy content of whole array to another using 1 line, I solved it by loops:

                                      int i, j, k;
                                      
                                      for(i = 0;i < 5;i++) {
                                          for(j = 0;j < 5;j++) {
                                              for(k = 0;k < 10;k++) {
                                                  store[i][j][k] = store_init[i][j][k];
                                              }
                                          }
                                      }
                                      
                                      jsulmJ Online
                                      jsulmJ Online
                                      jsulm
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #18

                                      @t0msk You could use the C function memcpy() - it is much more efficient compared to your solution and is only one line :-)

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

                                      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