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. modifying a variable from a different thread
QtWS25 Last Chance

modifying a variable from a different thread

Scheduled Pinned Locked Moved Unsolved General and Desktop
qthreadmultithreadvariablevaraiablehandl
7 Posts 4 Posters 1.1k 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.
  • E Offline
    E Offline
    ElSrPalmito
    wrote on last edited by
    #1

    Hi,
    I was trying to modify a Pushbutton's position every frame.
    the thing is I'm trying to modify the position's variable from my mainWindow QPushButton("Calculacion") while the loop is running, so I was wondering wondering if there is a solution to handle this mistake, cause i've been searching for hours with no result. :(

    here is my current code:
    my main.cpp

    int main(int argc, char *argv[])
    {
    
    
        QApplication a(argc, argv);
    
        QTranslator translator;
        const QStringList uiLanguages = QLocale::system().uiLanguages();
        for (const QString &locale : uiLanguages) {
            const QString baseName = "Calculemur_" + QLocale(locale).name();
            if (translator.load(":/i18n/" + baseName)) {
                a.installTranslator(&translator);
                break;
            }
        }
    
        Animacion *obj = new Animacion;
        Calculemur *w = new Calculemur;
        obj->Calcular = w->get_Calculacion();
        obj->start();
        w->show();
    
    
        return a.exec();
    
    
    }
    

    Animacion.h (different class to implement my thread while the main window is executing(Calculemur)):

    #ifndef ANIMACION_H
    #define ANIMACION_H
    #include "Timer.h"
    #include <iostream>
    #include "ui_calculemur.h"
    #include <chrono>
    #include <QThread>
    #include <QtWidgets>
    #include<QString>
    
    
    class Animacion : public QThread{
    
    public:
        void run();
        Animacion();
        bool isrunning = true;
        QMutex mutex;
    
        QPushButton* Calcular;
        int cosa_posx= 0;
        int cosa_posy= 0;
        int cosa_tamx= 0;
        int cosa_tamy= 0;
    
    };
    
    
    #endif // ANIMACION_H
    
    

    my mainWindow.h:

    
    class Calculemur : public QMainWindow
    {
        Q_OBJECT
    
    public:
        Calculemur(QWidget *parent = nullptr);
        ~Calculemur();
        QPushButton *get_Calculacion();
    
    public:
        Ui::Calculemur *ui;
    
    
    private slots:
        void on_Calculacion_clicked();
    };
    
    
    

    ![alt text](393cff8b-ffd4-45df-9d50-864190741ca2-image.png image url)

    this is the error i got.
    in case if it isn't a way it could work, can i implements signals and slots?
    Thank you!

    Pl45m4P 1 Reply Last reply
    0
    • E ElSrPalmito

      Hi,
      I was trying to modify a Pushbutton's position every frame.
      the thing is I'm trying to modify the position's variable from my mainWindow QPushButton("Calculacion") while the loop is running, so I was wondering wondering if there is a solution to handle this mistake, cause i've been searching for hours with no result. :(

      here is my current code:
      my main.cpp

      int main(int argc, char *argv[])
      {
      
      
          QApplication a(argc, argv);
      
          QTranslator translator;
          const QStringList uiLanguages = QLocale::system().uiLanguages();
          for (const QString &locale : uiLanguages) {
              const QString baseName = "Calculemur_" + QLocale(locale).name();
              if (translator.load(":/i18n/" + baseName)) {
                  a.installTranslator(&translator);
                  break;
              }
          }
      
          Animacion *obj = new Animacion;
          Calculemur *w = new Calculemur;
          obj->Calcular = w->get_Calculacion();
          obj->start();
          w->show();
      
      
          return a.exec();
      
      
      }
      

      Animacion.h (different class to implement my thread while the main window is executing(Calculemur)):

      #ifndef ANIMACION_H
      #define ANIMACION_H
      #include "Timer.h"
      #include <iostream>
      #include "ui_calculemur.h"
      #include <chrono>
      #include <QThread>
      #include <QtWidgets>
      #include<QString>
      
      
      class Animacion : public QThread{
      
      public:
          void run();
          Animacion();
          bool isrunning = true;
          QMutex mutex;
      
          QPushButton* Calcular;
          int cosa_posx= 0;
          int cosa_posy= 0;
          int cosa_tamx= 0;
          int cosa_tamy= 0;
      
      };
      
      
      #endif // ANIMACION_H
      
      

      my mainWindow.h:

      
      class Calculemur : public QMainWindow
      {
          Q_OBJECT
      
      public:
          Calculemur(QWidget *parent = nullptr);
          ~Calculemur();
          QPushButton *get_Calculacion();
      
      public:
          Ui::Calculemur *ui;
      
      
      private slots:
          void on_Calculacion_clicked();
      };
      
      
      

      ![alt text](393cff8b-ffd4-45df-9d50-864190741ca2-image.png image url)

      this is the error i got.
      in case if it isn't a way it could work, can i implements signals and slots?
      Thank you!

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #2

      @ElSrPalmito said in modifying a variable from a different thread:

      can i implements signals and slots?

      You should. This is one of the best ways to communicate between classes in Qt framework. It also works across threads (even though I haven't quite understood yet what your thread really does).


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      E 2 Replies Last reply
      3
      • Pl45m4P Pl45m4

        @ElSrPalmito said in modifying a variable from a different thread:

        can i implements signals and slots?

        You should. This is one of the best ways to communicate between classes in Qt framework. It also works across threads (even though I haven't quite understood yet what your thread really does).

        E Offline
        E Offline
        ElSrPalmito
        wrote on last edited by
        #3

        @Pl45m4
        Oh thank you!, i will implement it and see if it solves my problem. :)

        Basically I have two classes, “Calculemur” which is my main window and the other is “Animación” which inherit from Qthread class. In Animación I implemented the void run() that acces to “Calcular” variable that contains a QPushbutton called “Calculacion” from my main window and modify their current position into a loop (I’m just testing now if it works, because then I want to do some sort of animation by making some Widgets to move). that’s why “obj” (variable of type Animación) is accesing to “w.get_Calculacion()” which it return the pointer of a QPushbutton called “Calculacion” in my main window, explained by this way “obj->Calcular = w->get_Calculacion();”. Unfortunately I can’t figure it out why it throws me that error while my loop is running trying to modify the obj->Calcular position every frame.

        J.HilkJ 1 Reply Last reply
        0
        • E ElSrPalmito

          @Pl45m4
          Oh thank you!, i will implement it and see if it solves my problem. :)

          Basically I have two classes, “Calculemur” which is my main window and the other is “Animación” which inherit from Qthread class. In Animación I implemented the void run() that acces to “Calcular” variable that contains a QPushbutton called “Calculacion” from my main window and modify their current position into a loop (I’m just testing now if it works, because then I want to do some sort of animation by making some Widgets to move). that’s why “obj” (variable of type Animación) is accesing to “w.get_Calculacion()” which it return the pointer of a QPushbutton called “Calculacion” in my main window, explained by this way “obj->Calcular = w->get_Calculacion();”. Unfortunately I can’t figure it out why it throws me that error while my loop is running trying to modify the obj->Calcular position every frame.

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @ElSrPalmito why reinvent the wheel ?
          Qt offers an animation framework
          https://doc.qt.io/qt-5/animation-overview.html


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          E 1 Reply Last reply
          4
          • J.HilkJ J.Hilk

            @ElSrPalmito why reinvent the wheel ?
            Qt offers an animation framework
            https://doc.qt.io/qt-5/animation-overview.html

            E Offline
            E Offline
            ElSrPalmito
            wrote on last edited by
            #5

            @J-Hilk
            Oh, i didn’t realize that exist. I’m going to trae a view.
            Thaks for the repliy 🙏

            1 Reply Last reply
            1
            • Pl45m4P Pl45m4

              @ElSrPalmito said in modifying a variable from a different thread:

              can i implements signals and slots?

              You should. This is one of the best ways to communicate between classes in Qt framework. It also works across threads (even though I haven't quite understood yet what your thread really does).

              E Offline
              E Offline
              ElSrPalmito
              wrote on last edited by
              #6

              @Pl45m4
              Hey, it works using signals!. Thank you so much :)

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Hi and welcome to devnet,

                From the looks of it, you are likely accessing the QPushButton directly from your custom thread which is forbidden. GUI element manipulation shall happen in the main thread.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1

                • Login

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