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 can I use functions from a class in a thread?
QtWS25 Last Chance

How can I use functions from a class in a thread?

Scheduled Pinned Locked Moved Unsolved General and Desktop
threadqthread
2 Posts 2 Posters 317 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.
  • U Offline
    U Offline
    Uriel Machado
    wrote on 11 Oct 2022, 15:44 last edited by
    #1

    Hello, I am working on a project which I need to use the functions and variables of a class in a thread. The RadarGUI class is a widget that is created in the main.cpp, and the thread is a Qthread that is created as a class. I try to add the header of the RadarGUI widget in the thread, but does not let me use the functions, anyone know how I can solve this?
    I cant use static functions because the functions needs to use the variables of the class.

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        RadarGUI w; //I need to use the functions and variables of w;
        w.show();
        return a.exec();
    }
    

    header of RadarGUI:

    class RadarGUI : public QMainWindow
    {
        Q_OBJECT
    public:
        RadarGUI(QWidget *parent = nullptr); 
        ~RadarGUI();                          
        void radarwidget();                   
        void plotearM(ADC);                   
        void plotear_punto(int,int,int);      
    private:
        Ui::RadarGUI *ui;                           
        QChartView *chartView;                        
        QPolarChart *grafPol = new QPolarChart();     
        QValueAxis *angularAxis = new QValueAxis();   
        QValueAxis *radialAxis = new QValueAxis();  
        hilo_cargarMatrizMain hilo1;
        hilo_plotMain hilo2;
    };
    

    header of thread:

    class hilo_plotWorker: public QThread
    {
    public:
        hilo_plotWorker();
        void run(); 
    };
    
    J 1 Reply Last reply 11 Oct 2022, 16:08
    0
    • U Uriel Machado
      11 Oct 2022, 15:44

      Hello, I am working on a project which I need to use the functions and variables of a class in a thread. The RadarGUI class is a widget that is created in the main.cpp, and the thread is a Qthread that is created as a class. I try to add the header of the RadarGUI widget in the thread, but does not let me use the functions, anyone know how I can solve this?
      I cant use static functions because the functions needs to use the variables of the class.

      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          RadarGUI w; //I need to use the functions and variables of w;
          w.show();
          return a.exec();
      }
      

      header of RadarGUI:

      class RadarGUI : public QMainWindow
      {
          Q_OBJECT
      public:
          RadarGUI(QWidget *parent = nullptr); 
          ~RadarGUI();                          
          void radarwidget();                   
          void plotearM(ADC);                   
          void plotear_punto(int,int,int);      
      private:
          Ui::RadarGUI *ui;                           
          QChartView *chartView;                        
          QPolarChart *grafPol = new QPolarChart();     
          QValueAxis *angularAxis = new QValueAxis();   
          QValueAxis *radialAxis = new QValueAxis();  
          hilo_cargarMatrizMain hilo1;
          hilo_plotMain hilo2;
      };
      

      header of thread:

      class hilo_plotWorker: public QThread
      {
      public:
          hilo_plotWorker();
          void run(); 
      };
      
      J Offline
      J Offline
      JonB
      wrote on 11 Oct 2022, 16:08 last edited by JonB 10 Nov 2022, 16:09
      #2

      @Uriel-Machado
      Hello and welcome.

      Before you go any further: since it is a Qt rule you must not access any UI stuff in a secondary thread --- quite apart from the need for mutexes --- what do you think you are going to access in RadarGUI from a thread anyway?

      Threads can send signals to advise the UI thread of something happening and the UI thread can perform the UI action for them.

      1 Reply Last reply
      2

      1/2

      11 Oct 2022, 15:44

      • Login

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