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


  • Moderators

    @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.



  • @jsulm thanks a lot. My sub-process should wait till data is provided by another process (via boost message queue) and then the mainwindow should be updated... how could I create a signal that is provided without a button is pushed or sth like that?


  • Moderators

    @Timo_F Like any other signal. You can emit a signal at any time:

    emit mySignal();
    

    You are talking about processes, but I guess you mean threads?



  • @jsulm thanks!!
    No, I have a seperate vision-application, that is comunicating with Qt. The vision-app provides a set of points, that should be visualized in a Q3DScatter.



  • @jsulm ok I added the function mySignal() to the MainWindow. How can I start that function?
    Do I need to sub-class QObject? Sorry, but I never did that before. How do I use QObject::connect for MainWindow object?

    Thank you very much.


  • Moderators

    @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


  • Moderators

    @Timo_F Well, you connect where you have access to both instances: the sender and the receiver. I guess you create myThread instance in MainWindow? In this case connect in MainWindow.


Log in to reply
 

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