Signals and Slots with array



  • Hi,

    I am working with an extra workerthread in Qt which communicates with the mainwindow via Signals and Slots. This works fine so far. Now I want to send a signal containing 2 arrays from my workerthread to the mainwindow, which doesn't really work. Can anyone help me?

    Here is a little extract from my code:

    workerthread.h:

    ...
    private:
       long val1[5];
       int val2[5];
    
    signals:
       void testsignal(long val1[], int val2[]);
    ...
    

    workerthread.cpp:

    ...
    for (i=0;i<5;i++)
    {
       val1[i] = i;
       val2[i] = 6-i;
    }
    emit testsignal(long val1, int val2);
    ...
    

    mainwindow.h:

    ...
    puclic slots:
       testslot(long val1[], int val2[]);
    ...
    

    mainwindow.cpp:

    ...
    MainWindow::MainWindow(QWidget *parent) :
       QMainWindow(parent),
       ui(new Ui::MainWindow)
    {
       ...
       m_workerThread = new WorkerThread();
    
       connect(m_workerThread, SIGNAL(testsignal(long[],int[]), this, SLOT(testslot(long[],int[])));
       ...
    }
    
    ...
    
    void MainWindow::testslot(long val1[], int val2[])
    {
       QString string = QString("Value 1 = %1 ,  Value 2 = %2").arg(val1[0]).arg(val2[0]);
       ui->textBrowser->append(string);
    }
    

    The output in my GUI is:
    Value 1 = 889220 , Value 2 = 889260


  • Moderators

    @Beatsteak You should either use std::array or QVector instead of array. Else in your slot you do not know how many elements the arrays contain.
    And this is wrong:

    emit testsignal(long val1, int val2);
    

    it should be

    emit testsignal(val1, val2);
    


  • Thanks! The QVector thing works :)


Log in to reply
 

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