Array not sent from signal and slot



  • I want to send an array from one form(MainWindow) to another form(Dialog) upon a button click! Though the array seems to be sent it returns a wrong value! Following are my codes!

    MainWindow.h

    signals:void sendArray(int a[]);

    MainWindow.cpp

    void MainWindow::on_pushButton_clicked()
    {
    int a[] = {1,2,3};
    Dialog dialog1;
    connect(this,SIGNAL(sendArray(int[])),&dialog1,SLOT(receiveArray(int[])));
    emit sendArray(a);

    dialog1.exec();
    

    }

    Dialog.h
    public slots:
    void receiveArray(int a[]);

    Dialog.cpp

    void Dialog::receiveArray(int a[]){
    int value=a[0];
    QString value1=QString::number(value);
    ui->label->setText(value1);
    }

    When the program is run(no compile errors occur) and the button is clicked though 1 (0th index of the array) should printed on the label in Dialog form a wrong value 8441124 is printed. How can I correct this?



  • I found the solution myself! Use QList<int> instead of normal arrays :)


  • Qt Champions 2016

    @Lasith
    Hi
    old type c array can also work.
    Its just a matter of what is registered already.
    You can register your own types.

    http://doc.qt.io/qt-5/qmetatype.html#Q_DECLARE_METATYPE



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