QThreads , QObject and sleep function

前端 未结 4 1093
醉梦人生
醉梦人生 2020-12-16 04:18

The problem I encountered is that I decided to implement QThreads the way they are supposed to, based on numerous articles:
http://blog.qt.digia.com/blog/20

4条回答
  •  Happy的楠姐
    2020-12-16 04:54

    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.

提交回复
热议问题