Cannot pass variable to another Dialog



  • 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;
    }
    

  • Lifetime Qt Champion

    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.



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



  • @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;
    }
    


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


  • Lifetime Qt Champion

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


  • 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;
    }
    

  • Lifetime Qt Champion

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

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



  • 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])
    


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


  • Moderators

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



  • 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];
            }
        }
    }


  • 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? :/


  • Lifetime Qt Champion

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



  • And how to initialise "second"?


  • Lifetime Qt Champion

    Assign it a value in the constructor initializer list.



  • void Dialog::setData(int test) {
      second = test;
     ui->label->setText(QString::number(second));
    }
    

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


  • Moderators

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.