• QT 线程应用


    Worker类

    class Worker : public QObject
    {
        Q_OBJECT
    
    public slots:
        void doWork(const QString &parameter) {
            QString result;
            /* ... here is the expensive or blocking operation ... */
            emit resultReady(result);
        }
    
    signals:
        void resultReady(const QString &result);
    };

    Controller类

    class Controller : public QObject
    {
        Q_OBJECT
        QThread workerThread;
    public:
        Controller() {
            Worker *worker = new Worker;
            worker->moveToThread(&workerThread);
            connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater);
            connect(&workerThread, &QThread::finished, &workerThread, &QThread::deleteLater); 
            connect(this, &Controller::operate, worker, &Worker::doWork);
            connect(worker, &Worker::resultReady, this, &Controller::handleResults);
            workerThread.start();
        }
        ~Controller() {
            workerThread.quit();
            workerThread.wait();
        }
    public slots:
        void handleResults(const QString &);
    signals:
        void operate(const QString &);
    };
  • 相关阅读:
    路径规划算法总结
    常用滤波器整理
    Debian 9 strech 安装 ROS lunar
    understand 安装笔记
    protobuf 安装与卸载
    maven-surefire-plugin
    spring数据源、数据库连接池
    日志插件总结
    pom.xml常用元素解析
    BeanFactory笔记
  • 原文地址:https://www.cnblogs.com/Joezhang433/p/13271702.html
Copyright © 2020-2023  润新知