Ho un thread in background e il thread chiama alcuni metodi che aggiornano l'interfaccia utente (per mostrare le barre di avanzamento e mostrare ulteriori informazioni nelle aree di testo).Qt: thread UI di aggiornamento thread di thread
Se si modificano alcuni valori del widget dell'interfaccia utente, viene generato un errore di asserzione "Impossibile inviare eventi a oggetti di proprietà di un thread diverso".
Guardando i forum, ho letto che potevo usare il metodo QMetaObject :: invokeMethod ma funziona solo se gli passiamo il flag Qt :: DirectConnection che solleva lo stesso errore mostrato sopra.
Se utilizzo Qt :: QueuedConnection o Qt :: AutoConnection, invokeMethod restituisce false.
Il mio codice è simile a questo:
.h:
class A : public QMainWindow
{
Q_OBJECT
QProgressBar* pb;
public slots:
bool m(bool, int);
};
class B
{
A* a;
public:
void handleEvent();
};
.cpp:
bool A::m(bool x, int y)
{
pb->setValue(y);
return x;
}
void B::handleEvent()
{
//a->m(true, 12); //raises an assertion error
bool r;
//bool ret = QMetaObject::invokeMethod(a, "m", Qt::DirectConnection, Q_RETURN_ARG(bool, r), Q_ARG(bool, true), Q_ARG(int, 12)); //raises the same assertion error error
bool ret = QMetaObject::invokeMethod(a, "m", Qt::AutoConnection, Q_RETURN_ARG(bool, r), Q_ARG(bool, true), Q_ARG(int, 12)); //is ignored and ret contains false.
}
Sai cosa sta succedendo o che cosa sto facendo male? o forse qualcuno può suggerirmi un altro approccio per affrontare il mio problema dei principianti?
Grazie in anticipo,
Ernesto