I got big problem and don't know how to look for solution. I got thread which is emitting signals to MainWindow and execute slot wich completes UI labels/texts. It's like:
main.cpp:
MainWIndow main{};
Receiver recv{ &main};
std::thread th{&Receiver::receive, &recv};
th.detach();
Receiver.h:
class Receiver {
public:
Receiver(MainWindow* _main) : main(_main) {};
void receive ()
{
Observer observer{};
main->setObserver(observer);
main->connectInit();
//bla bla listen to the channel and receive msgs
}
private:
MainWindow* main;
}
Observer.h:
class Observer {
public:
void process(UniqueType msg) {
emit signal(msg);
}
signals:
void signal(UniqueType);
};
Q_DECLARE_METATYPE(UniqueType);
MainWindow.cpp:
void MainWindow::connectInit() const
{
qRegisterMetaType<UniqueType>("UniqueType");
connect(observer, SIGNAL(signal(UniqueType)),
this, SLOT(slot(UniqueType)));
}
void MainWindow::slot(UniqueType) {
widget.label->setText(UniqueType.text);
}
Receiver receive a msg and execute process method in observer class. It works fine but sometimes randomly emitted signal doesn't execute the slot or execute it twice. Any idea how to resolve this problem?
This fragment of code is an example based on my code.
Aucun commentaire:
Enregistrer un commentaire