jeudi 12 mars 2015

Is this the correct way to use a QThread?


I am looking at some github projects, where one of them did the UDPlink in the following way, first it subclass QThread to create a class UDPLink:public QThread and its constructor and deconstructor is like:



UDPLink::UDPLink(UDPConfiguration* config)
: _socket(NULL)
, _connectState(false)
{
Q_ASSERT(config != NULL);
_config = config;
_config->setLink(this);

// We're doing it wrong - because the Qt folks got the API wrong:
// http://ift.tt/1fiDJT2
moveToThread(this);

// Set unique ID and add link to the list of links
_id = getNextLinkId();
qDebug() << "UDP Created " << _config->name();
}

UDPLink::~UDPLink()
{
// Disconnect link from configuration
_config->setLink(NULL);
_disconnect();
// Tell the thread to exit
quit();
// Wait for it to exit
wait();
this->deleteLater();
}


Though the code did compile and work, but I wonder whether this way of using a QThread would be correct?




Aucun commentaire:

Enregistrer un commentaire