Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to update a Q3DScatter on MainWindow from a new thread?
Qt 6.11 is out! See what's new in the release blog

How to update a Q3DScatter on MainWindow from a new thread?

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 2 Posters 4.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Timo_F
    wrote on last edited by
    #1

    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

    jsulmJ 1 Reply Last reply
    0
    • T Timo_F

      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

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • T Offline
        T Offline
        Timo_F
        wrote on last edited by
        #3

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

        jsulmJ 1 Reply Last reply
        0
        • T Timo_F

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

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • T Offline
            T Offline
            Timo_F
            wrote on last edited by
            #5

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

            1 Reply Last reply
            1
            • T Offline
              T Offline
              Timo_F
              wrote on last edited by
              #6

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

              jsulmJ 1 Reply Last reply
              0
              • T Timo_F

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

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by jsulm
                #7

                @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();

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  Timo_F
                  wrote on last edited by
                  #8

                  @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

                  jsulmJ 1 Reply Last reply
                  0
                  • T Timo_F

                    @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

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

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

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved