Hello, I am trying to deal with threading affinity. I think I did everything like it was suggested in the "you're doing it wrong" article, but still I receive error messages from the socket when I call waitForConnected method:
"QObject: Cannot create children for a parent that is in a different thread.
(Parent is QTcpSocket(0x2022608), parent's thread is QThread(0x1fc8320), current thread is QThread(0x40fde8)"
Here is my code, I create controller class that is executed in the separate thread.
test
::test(QWidget *parent, Qt
::WFlags flags
){
ui.setupUi(this);
controller = new Controller();
controller->moveToThread(&thread);
connect(&thread, SIGNAL(started()), controller, SLOT(start()));
connect(controller, SIGNAL(finished()), &thread, SLOT(quit()));
connect(this,
SIGNAL(fileAdded
(const QString &)), controller,
SLOT(enqueueFile
(const QString &)));
emit fileAdded("lalalala");
thread.start();
}
test::test(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
controller = new Controller();
controller->moveToThread(&thread);
connect(&thread, SIGNAL(started()), controller, SLOT(start()));
connect(controller, SIGNAL(finished()), &thread, SLOT(quit()));
connect(this, SIGNAL(fileAdded(const QString &)), controller, SLOT(enqueueFile(const QString &)));
emit fileAdded("lalalala");
thread.start();
}
To copy to clipboard, switch view to plain text mode
Controller class declaration:
#ifndef CONTROLLER_H
#define CONTROLLER_H
#include <QThread>
#include <QTcpSocket>
{
Q_OBJECT
public:
~Controller();
public slots:
void enqueueFile
(const QString &fileName
);
void start();
signals:
void finished();
private:
};
#endif // CONTROLLER_H
#ifndef CONTROLLER_H
#define CONTROLLER_H
#include <QThread>
#include <QTcpSocket>
class Controller: public QObject
{
Q_OBJECT
public:
Controller(QObject *parent = 0);
~Controller();
public slots:
void enqueueFile(const QString &fileName);
void start();
signals:
void finished();
private:
QTcpSocket socket;
};
#endif // CONTROLLER_H
To copy to clipboard, switch view to plain text mode
Controller class implementation:
#include "controller.h"
#include <QDebug>
#include <QTimer>
#include <QEventLoop>
Controller
::Controller(QObject *parent
) {
}
Controller::~Controller()
{
}
void Controller::start()
{
socket.connectToHost("some-host", 777);
socket.waitForConnected(); // here goes the error
socket.disconnectFromHost();
socket.waitForDisconnected(); // here goes the error again
}
void Controller
::enqueueFile(const QString &fileName
) {
qDebug() << fileName;
}
#include "controller.h"
#include <QDebug>
#include <QTimer>
#include <QEventLoop>
Controller::Controller(QObject *parent)
{
}
Controller::~Controller()
{
}
void Controller::start()
{
socket.connectToHost("some-host", 777);
socket.waitForConnected(); // here goes the error
socket.disconnectFromHost();
socket.waitForDisconnected(); // here goes the error again
}
void Controller::enqueueFile(const QString &fileName)
{
qDebug() << fileName;
}
To copy to clipboard, switch view to plain text mode
I know threads are not really needed for sockets, I could use signal/slot mechanism, but this is just for test. Please help me to understand what is wrong.
Bookmarks