modifying a variable from a different thread
-
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.cppint 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]( 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! -
@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).
-
@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.
-
@ElSrPalmito why reinvent the wheel ?
Qt offers an animation framework
https://doc.qt.io/qt-5/animation-overview.html -
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.