马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
在Qt中可以使用线程指针对子线程进行操作。子线程可以使用使用根本指针,shared_ptr,两种指针进行操作(QSharedPointer指针操作有问题还未办理,先不讲)。
- // c++标准库
- shared_ptr<QThread> thread_;
- // 基本指针
- QThread* thread_;
- // Qt智能指针。
- // QSharedPointer<QThread> thread_;
复制代码 绑定信号槽,智能指针调用方法get获取根本指针。
- connect(exit_action_,
- &QAction::triggered,
- handler_.get(),
- &Main_window_handler::signal_exit_application);
复制代码 启动子线程:
- // c++标准智能指针。
- thread_ = make_shared<QThread>();
- handler_ = make_shared<Main_window_handler>();
- // handler_->moveToThread(thread_.get());
- // thread_->start();
-
- // 基本指针。
- thread_ = new QThread();
- handler_->moveToThread(thread_);
- thread_->start();
复制代码 在析构方法中开释子线程资源:
- // 基本指针资源释放。
- if (nullptr != thread_) {
- thread_->quit();
- thread_->wait();
- delete thread_;
- thread_ = nullptr;
- }
- // 释放c++标准智能指针shared_ptr。
- // while (thread_->isRunning()) {
- // thread_->quit();
- // thread_->wait();
- // }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |