Closed YANG-Mingcong closed 4 years ago
可以使用QThread继承,只不过你不要在new时传入this指针,qthread很特别,它的run函数和qthread自身不在一个线程里的,你应该改为
udpSocket = new QUdpSocket();
udpSocket->bind(1936, QUdpSocket::ShareAddress);
connect(udpSocket, &QUdpSocket::readyRead,
this, &ArtNetRxThread::processPendingDatagrams);
然后在线程的析构中把资源释放:
ArtNetRxThread::~ArtNetRxThread()
{
if(udpSocket)
delete udpSocket ;
}
非常感谢您的指教!向您学习! Thanks a lot~
抱歉打扰了,拜读您的关于“[TOC] #Qt使用多线程的一些心得——1.继承QThread的多线程使用方法”一文后,在自己的代码中测试关于QThread 与 QObject两种实现方式。 Sorry for bothering, I'm testing 2 different ways (QThread, and QObject) in my own code.
在测试QThread继承类实现多线程时,总是出现如下报错。 When testing the QThread inheritance class to achieve multithreading, always get the error below.
QObject: Cannot create children for a parent that is in a different thread. (Parent is ArtNetRxThread(0x7f894dd6ec10), parent's thread is QThread(0x7f894dd090d0), current thread is ArtNetRxThread(0x7f894dd6ec10)
我猜测是由于我的类中有这种构架的继承类导致的。 I guess it is due to the inheritance of this kind of things in my class.
.h file
.cpp file
我想知道,这种情况还有可能使用由QThread继承类来实现吗?还是只能使用QObject的方式呢? I wish to know, is this possible to use QThread inherited class to achieve? Or can I only use QObject method?
非常感谢! Thanks a lot!