Qt多线程之一:子线程中创建的对象不应再其他线程中被调用,包括使用槽函数的形式
来源:互联网 发布:手机淘宝安全中心官网 编辑:程序博客网 时间:2024/06/16 21:21
#define WORKTHREAD_H#include <QThread>#include "DataBaseObject.h"#include "HttpObject.h"#include <QStringList>class WorkThread:public QThread{ Q_OBJECTpublic: WorkThread(QThread *parent = 0); ~WorkThread();signals: void next();private slots: void singleOperatre();protected: void run();private: DataBaseObject *db; HttpObject *http; bool exitFlag; QStringList city_code_list; int index; int max; QString url; QString fixedPartOfUrl; QString urlParam;};#endif // WORKTHREAD_H
<pre name="code" class="html">#include "workthread.h"#include <QDebug>WorkThread::WorkThread(QThread *parent): QThread(parent){ db = NULL; http = NULL; exitFlag = false; index = 0; city_code_list.clear();}WorkThread::~WorkThread(){ db->closeDB(); delete db; delete http;}void WorkThread::singleOperatre()//这个函数其实是在主线程中被调用,而不是新建的这个线程,虽然其调用的对象是在子线程中创建的{ if(index < max) { urlParam = city_code_list.at(index); qDebug()<<urlParam; url = fixedPartOfUrl + urlParam; http->getRequest(url); index++; qDebug()<<"\n thread ID:"<<QThread::currentThreadId()<<QThread::currentThread()<<__FUNCTION__<<"index:"<<index;// msleep(10);// emit next(); }}void WorkThread::run(){ qDebug()<<QThread::currentThreadId()<<__FUNCTION__<<QThread::currentThread(); fixedPartOfUrl = "http://apis.baidu.com/apistore/weatherservice/cityid?cityid="; db = new DataBaseObject; http = new HttpObject; connect(http,SIGNAL(weatherGetted(WEATHER_CITY)),db,SLOT(addCityWeather(WEATHER_CITY)));// connect(this,SIGNAL(next()),this,SLOT(singleOperatre()),Qt::QueuedConnection); db->setDataBase("127.0.0.1",3306,"weather_db","user","123456"); db->openDB(); max = db->getCityCode(city_code_list); qDebug()<<"city num:"<<max<<endl; emit next();// singleOperatre(); exec();}
</pre><pre code_snippet_id="1729225" snippet_file_name="blog_20160625_5_2648402" name="code" class="html">void WorkThread::singleOperatre()//这个函数其实是在主线程中被调用,而不是新建的这个线程,虽然其调用的对象是在子线程中创建的
<p style="margin-top: 0px; margin-bottom: 0px;"><span style=" font-family:'Courier'; font-size:10pt; color:#aa00aa;">QObject: Cannot create children for a parent that is in a different thread.</span></p><p style="margin-top: 0px; margin-bottom: 0px;"><span style=" font-family:'Courier'; font-size:10pt; color:#aa00aa;">(Parent is QNetworkAccessManager(0x66a928), parent's thread is WorkThread(0x780eb0), current thread is QThread(0x667658)</span></p>
0 0
- Qt多线程之一:子线程中创建的对象不应再其他线程中被调用,包括使用槽函数的形式
- 多线程,成员函数内创建线程,线程函数调用类内的其他成员函数
- 子线程 run() 函数中,调用其他函数
- Qt开发:释放线程中创建的QUdpSocket对象
- Qt开发:释放线程中创建的QUdpSocket对象
- 子线程中如何调用对话框的变量和函数
- Linux中fork()函数创建子线程的理解
- 【黑马程序员】关于多线程聊天室中出现的“由于其他线程拥有此对象,因此调用线程无法对其进行访问”问题的探究
- C#中创建线程的方式,以及主线程和子线程之间的资源调用
- MFC多线程的创建,包括工作线程和用户界面线程
- MFC多线程的创建,包括工作线程和用户界面线程
- MFC多线程的创建,包括工作线程和用户界面线程
- MFC多线程的创建,包括工作线程和用户界面线程
- MFC多线程的创建,包括工作线程和用户界面线程
- MFC多线程的创建,包括工作线程和用户界面线程
- MFC多线程的创建,包括工作线程和用户界面线程
- MFC多线程的创建,包括工作线程和用户界面线程
- MFC多线程的创建,包括工作线程和用户界面线程
- 让客户自我成交的秘笈
- (Android)The connection to adb is down, and a severe error has occured.解决方法
- C++设计模式浅识职责链模式
- iOS sqlite3数据库解析
- 生产者消费者模型(二)-引入ArrayBlockingQueue
- Qt多线程之一:子线程中创建的对象不应再其他线程中被调用,包括使用槽函数的形式
- 秦谊:区块链目前还未达到大型商业应用阶段
- java程序使用数据库驱动包连sqlserver数据库
- FPGA基础知识6(FPGA时钟系统及多时钟系统处理)
- 如何成为资源整合高手
- 安装get_homologues
- 找出数组中符合条件的数对的个数
- iOS多线程编程
- java web之面向接口编程