How to update a Q3DScatter on MainWindow from a new thread?
-
Dear all,
I wrote a programm with two Q3DScatter on the MainWindow. In the next step it is necessary to update the the Q3DScatter in an endless process till a certain button is pushed. Therefore I want to use a new thread, because otherweise it's not possible to push the button while the main thread is still running.
But how is it possible to manipulate the QScatter by using a new thread?
Below you can see the MainWindow declaration.MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); graph = new Q3DScatter(); QWidget *container = QWidget::createWindowContainer(graph); graph2 = new Q3DScatter(); QWidget *container2 = QWidget::createWindowContainer(graph2); ui->gridLayout->addWidget(container); ui->gridLayout_2->addWidget(container2); }
And there is another class for updating the scatter:
scattermodifier *modifier = new scattermodifier(graph); scattermodifier2 *modifier_2 = new scattermodifier2(graph2);
Could anybody give me a hint how to solve the problem?
Thanks a lot!
Best regards,
Timo
-
@Timo_F Do not change UI from other threads! Never! This is not supported and will cause problems.
For what you want to do just use QTimer, no need for an endless loop.
If you still want to use endless loop in a thread then do not change the UI in this thread, but emit a signal instead. Connect a slot in your MainWindow to this signal and change the UI in this slot. -
-
@Timo_F Did you actually read http://doc.qt.io/qt-5.8/signalsandslots.html ?
Please read it first.
Yes, you need to inherit QObject and add Q_OBJECT macro to your class.
And you do not add mySignal() function, instead you add mySignal() signal:signals: void mySignal();
-
@jsulm Hm I read it, but where should I define the connect()?
That's my header file and I also implemented the listen, which should be contacted by a second thread.
I have no idea how it should be done.namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private slots: void on_pushButton_clicked(); void on_pushButton_4_clicked(); void on_pushButton_update_clicked(); void on_pushButton_delete_clicked(); void on_checkBox_stateChanged(int arg1); void on_radioButton_A_clicked(); void on_radioButton_PS_clicked(); void listen(QString a); signals: public: Ui::MainWindow *ui; QtDataVisualization::Q3DScatter * graph; QtDataVisualization::Q3DScatter * graph2; };
That is inside the thread class.
class myThread : public QObject { Q_OBJECT public: explicit myThread(QObject *parent = 0); signals: void mstart(); public slots: };
Thanks a lot.
Kindly regards,
Timo