Qt 使用笔记

Qt 多线程的简单使用

2019-05-26  本文已影响0人  paresly

背景
使用Qt开发界面程序的时候,免不了做一些数据计算,有时候,当计算量比较大,需要耗费一定的时间。一直在界面上等待肯定不是一个好的办法。这里介绍一种多线程的处理方法。

方法

代码示例

class FileOper : public QThread
{

signals:
          void processFinished();
 public:
        enum Class OperType : int
        {
                  PROCESS_INDEX = 0,
                  //  other enums
        }
    
       void setParams(const QString& msg)
       {
               // send param to the thread
       }

       void start()
      {
            switch(m_type)
            {
                  case PROCESS_INDEX :
                   process();  //处理界面数据的函数     
                   emit processFinished();
                   break;
            }
      }
}


class widget : public QWidget
{
      ...
      explicit widget(QWidget* parent = nullptr)
      {
               m_oper = new FileOper();
               connect(m_oper,&FileOper::processFinished,
                              this,&widget::onProcessFinished);
      }
        void  setParam(const QString& msg);//the thread params for calc
private slots:
        void onBtnClicked()
        {
                 m_oper->setOperType(FileOper::OperType::PROCESS_INDEX);
                 m_oper->setParam("the param to be calc");
                 m_oper->start();
        }
        void onProcessFinished()
        {
                QString result = m_oper->result();
                //TODO:ui opers
        }

private:
        FileOper* m_oper;
};
上一篇 下一篇

猜你喜欢

热点阅读