QtConcurrent::run() with virtual function
Solved
General and Desktop
-
Hi all
I want to make a abstract class to process data from different IO devices.
I have an idea as follow:
class AbstractCommunication : public QObject { Q_OBJECT public: QMutex m_CacheMutex; QByteArray m_receiveCache; public: explicit AbstractCommunication(QIODevice *device); void sendData(QByteArray data); int receiveTick() const; private: virtual void processPacket(); private slots: void receiveData(); private: QIODevice *m_communication; int m_receiveTick; }; AbstractCommunication::AbstractCommunication(QIODevice *device) : m_communication(device) { m_receiveCache.clear(); if(m_communication->isOpen()) { connect(m_communication,SIGNAL(readyRead()),this,SLOT(receiveData())); QtConcurrent::run(this,&AbstractCommunication::processPacket); } else { qDebug()<<"device is not ready"; } } void AbstractCommunication::sendData(QByteArray data) { m_communication->write(data); } void AbstractCommunication::receiveData() { QMutexLocker lock(&m_CacheMutex); m_receiveCache.append(m_communication->readAll()); m_receiveTick = (m_receiveTick + 1) % MAX_RECEIVE_TICK; // then process the data with the same protocol } nt AbstractCommunication::receiveTick() const { return m_receiveTick; } // this virtual function will be overwrite in different module, which inherit this class. void AbstractCommunication::processPacket() { qDebug()<<"in virtual thread". }
first it doesn't work. then I improve the max count of thread pool
QThreadPool::globalInstance()->setMaxThreadCount(8);
,
sometimes it works, but sometimes it only print "in virtual thread".'does the pointer
this
mean this abstract class , not the class inheriting it?Best regards
Mihan -
-
@Mihan said in QtConcurrent::run() with virtual function:
QtConcurrent::run(this,&AbstractCommunication::processPacket);
Why do you do this in base class constructor?
This will callAbstractCommunication::processPacket()
and not any overload... So no need to made it virtual!