Q_OBJECT
public:
Parser() {}
public slots:
void parseFile
(const QString &fileName
) { result r = doTheParsingHere(fileName);
emit ready(r);
}
signals:
void ready(result);
};
// ...
QThread t;
// create a thread controller (possibly a class member variable) t.start(); // start a thread
Parser *p = new Parser; // create the parser object
p->moveToThread(&t); // push it to the thread
connect(p, SIGNAL(ready(result)), this, SLOT(useResult(result))); // connect to the result
// ...
QMetaObject::invokeMethod(p,
"parseFile", Qt
::QueuedConnection, Q_ARG
(QString,
"someFile.txt"));
// make the parser do some work // or alternatively to the last line:
connect(this,
SIGNAL(parseRequested
(QString)), p,
SLOT(parseFile
(QString)));
emit parseRequested("someFile.txt");
class Parser : public QObject {
Q_OBJECT
public:
Parser() {}
public slots:
void parseFile(const QString &fileName) {
result r = doTheParsingHere(fileName);
emit ready(r);
}
signals:
void ready(result);
};
// ...
QThread t; // create a thread controller (possibly a class member variable)
t.start(); // start a thread
Parser *p = new Parser; // create the parser object
p->moveToThread(&t); // push it to the thread
connect(p, SIGNAL(ready(result)), this, SLOT(useResult(result))); // connect to the result
// ...
QMetaObject::invokeMethod(p, "parseFile", Qt::QueuedConnection, Q_ARG(QString, "someFile.txt")); // make the parser do some work
// or alternatively to the last line:
connect(this, SIGNAL(parseRequested(QString)), p, SLOT(parseFile(QString)));
emit parseRequested("someFile.txt");
To copy to clipboard, switch view to plain text mode
Bookmarks