What we've done is basically something like this: (written by memory, as I don't have our code checked out on this computer)
class Sleeper : public QThread {
public:
void sleep(int ms) { QThread::sleep(ms); }
};
void sleep(int ms);
// in a .cpp file:
static Sleeper slp;
void sleep(int ms) {
slp.sleep(ms);
}
The key is that the QThread::sleep
function causes the calling thread to sleep, not the threaf represented by the QThread
instance. So just create a wrapper which calls it via a custom QThread
subclass.
Unfortunately, QThread is a mess. The documentation tells you to use it incorrectly. A few blog posts, as you've found, tell you a better way to do it, but then you can't call functions like sleep
, which should never have been a protected thread member in the first place.
And best of all, even no matter which way you use QThread, it's designed to emulate what's probably the worst thread API ever conceived of, the Java one. Compared to something sane, like boost::thread
, or even better, std::thread
, it's bloated, overcomplicated and needlessly hard to use and requiring a staggering amount of boilerplate code.
This is really one of the places where the Qt team blew it. Big time.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…