Hi all,
i think this is a dumb question but i can’t understand the solution:
So i have my worker class cardWorker.h
...
class CardWorker : public QObject {
Q_OBJECT
public:
CardWorker();
~CardWorker();
public slots:
void process();
signals:
void finished();
void error(QString err);
void myValueHasChanged(int value);
private:
// add your variables here
};
...
The implementation: cardWorker.cpp
...
// --- CONSTRUCTOR ---
CardWorker::CardWorker() {
// you could copy data from constructor arguments to internal variables here.
}
// --- DESTRUCTOR---
CardWorker::~CardWorker() {
// free resources
}
// --- PROCESS ---
// Start processing data.
void CardWorker::process() {
// allocate resources using new here
qDebug("Hello World!");
emit myValueHasChanged(10);
emit finished();
}
...
In my main.cpp i have;
...
QThread* thread = new QThread;
CardWorker* worker = new CardWorker();
worker->moveToThread(thread);
QObject::connect(thread, SIGNAL(started()), worker, SLOT(process()));
QObject::connect(worker, SIGNAL(finished()), thread, SLOT(quit()));
QObject::connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()));
QObject::connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();
...
Now the problem is: if i have my “main.qml” with a “Text” item how can i “see” the signal “myValueHasChanged()” emitted by the worker?
I tryed this:
main.qml
...
Text{
id:myText
Connections{
target: myCard
onMyValueHasChanged{
...do things..
}
}
}
...
and i added, just after the “thread ->start()” command:
view.rootContext()->setContextProperty("myCard", worker);
but this doesn’t work. I know this is wrong but i can’t understand how to solve this problem.
All tips are appreciated.
Thanks in advance!
Claudio
↧









