Signals and Slots with array
Solved
General and Desktop
-
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 -
@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);