How can i pass data between Qwidgets forms?



  • This is my code:
    Dialog2.h

    public:
        explicit Dialog2(QWidget *parent = 0);
        Dialog2(QWidget *parent = 0, const QString curso = "", const int numero = 0);
        int n = 0;
        int *LunINI;
        ~Dialog2();
    

    Dialog3.h

    public:
    
        explicit Dialog3(QWidget *parent = 0);
        Dialog3(QWidget *parent = 0,  int *LunINI = 0);
        ~Dialog3();
    
    private:
        int LunINI[24];
        Ui::Dialog3 *ui;
    };
    

    Dialog2.cpp

    void Dialog2::on_pushButton_clicked()
    {
        ...
        Dialog3 *v = new Dialog3(this, LunINI);
        close();
        v->show();
    }
    

    Dialog3.cpp

    Dialog3::Dialog3(QWidget *parent, int *LunINI) :
        QDialog(parent),
        ui(new Ui::Dialog3), LunINI(*LunINI) // ERROR C2075
    { // OR HERE
        ui->setupUi(this);
    }
    Dialog3::~Dialog3()
    {
        delete ui;
    }
    

    When i try to run it Qt says that "error C2075: array initialization needs curly braces". I wanted to pass an array from Dialog2 to Dialog3, however, i can't.



  • @Eduardo12l
    you need to copy the element of LunINI from Dialog2 one by one, since there is no syntax to set array directly.

    by the way, why not use a vector instead?



  • @Flotisable If I use QVector it will work? What about QList?



  • @Eduardo12l
    QVector and QList both will work



  • @Flotisable Ok, I'm quite new at C++ . I have a problem yet in Dialog2.cpp
    This is my QList<int> L .This time im not using an array (LunINI), im using L.

    Dialog2.h

    public:
        explicit Dialog2(QWidget *parent = 0);
        Dialog2(QWidget *parent = 0, const QString curso = "", const int numero = 0);
        int n = 0;
        int LunINI[24];
        QList <int> L;
    

    Dialog3.h

    class Dialog3 : public QDialog
    {
        Q_OBJECT
    
    public:
        explicit Dialog3(QWidget *parent = 0);
        Dialog3(QWidget *parent = 0, QList<int> *L = 0);
        ~Dialog3();
    private:
        int LunINI[24];
        QList <int> L;
        Ui::Dialog3 *ui;
    };
    
    

    Dialog2.cpp

    void Dialog2::on_pushButton_clicked(){
        Dialog3 *v = new Dialog3(this, L); //ERROR C2664 "cannot convert parameter 2 from 'QList<int>'  to  'QList<int>* '"
        close();
        v->show();
    }
    

    Dialog3.cpp

    
    Dialog3::Dialog3(QWidget *parent, QList<int> *L) :
        QDialog(parent),
        ui(new Ui::Dialog3), L(*L)
    {
        ui->setupUi(this);
    }
    

  • Moderators

    @Eduardo12l You should use QVector if you're going to use it like an array as it is much more efficient for this than QList.

    Dialog3 *v = new Dialog3(this, L); //ERROR C2664 "cannot convert parameter 2 from 'QList<int>'  to  'QList<int>* '"
    

    that's because Dialog3() constructor takes a pointer:

    Dialog3 *v = new Dialog3(this, &L);
    


  • @Eduardo12l
    to add @jsulm , you can use const reference instead of pointer in your Dialog3's constructor

    this way, you don't need to use & and *operator, I think it will make it more readable in your code



  • @jsulm Thx this time Qt doesn't show an error message



  • @Flotisable I dont understand, Do you mean that should add "const" instead of &?

    I mean

    Dialog3 *v = new Dialog3 (this, **const** L )
    


  • @Eduardo12l
    no, I mean

    Dialog3::Dialog3( QWidget *parent, const QList<int> &L ) :
        QDialog( parent ), ui( new Ui::Dialog3 ), L( L )
    

    and

    Dialog3 *v = new Dialog3( this, L );
    


  • @Flotisable It doesnt work but anyway thx ^^


  • Moderators

    @Eduardo12l What does not work? What is the error?



  • @jsulm What Flotisable says doesnt work. The code is below:

    Dialog2.h

    public:
        explicit Dialog2(QWidget *parent = 0);
        Dialog2(QWidget *parent = 0, const QString curso = "", const int numero = 0);
        int n = 0;
        int LunINI[24];
        QList<int> L;
        ~Dialog2();
    

    Dialog3.h:

    public:
    
        explicit Dialog3(QWidget *parent = 0);
        Dialog3(QWidget *parent = 0, const QList<int> &L=0); // ERROR C2440: Cannot convert from 'INT' to 'const QList<int>&'
        ~Dialog3();
    
    private:
        int LunINI[24];
        QList <int> L;
        Ui::Dialog3 *ui;
    

    Dialog2.cpp

    void Dialog2::on_pushButton_clicked(){
        Dialog3 *v = new Dialog3(this, L); 
        close();
        v->show();
    }
    

    Dialog3.cpp

    Dialog3::Dialog3(QWidget *parent, const QList<int> &L) :
        QDialog(parent),
        ui(new Ui::Dialog3), L(L)
    {
        ui->setupUi(this);
    }
    

  • Lifetime Qt Champion

    Hi,

    You can't assign 0 to QList container. That's the error. You have to assign it an empty QList value if you want a default value for it.

    On a side note, the common pattern is to have the parent parameter as the last in the constructor.



  • @SGaist Thx but How can i assign an empty QList value?



  • @Eduardo12l
    since you have overloaded constructor, you don't need to give L a default value.
    if you want, you can use

      Dialog3( QWidget *parent = 0, const QList<int> &L = QList<int>() )
    

    QList<int>() will construct a QList<int> instance with empty value.

    for what @SGaist says, you can modify the constructor this way

    Dialog3( const QList<int> &L = QList<int>(), QWidget *parent = 0 )
    

    as I have said, you don't need to set a default value for L, so

    Dialog3( const QList<int> &L, QWidget *parent = 0 )
    

    is enough


Log in to reply
 

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