//include file declaring MyObject
class MainWindow
: public QGLWidget //The fact that I'm using GL here is not important{
Q_OBJECT
...
signals:
void ConnectSocket();
...
public slots:
//... various slots
...
private:
MyObject* my_object;
};
MainWindow::MainWindow
{
my_object = new MyObject(this); //main_window is not the parent of my_object, it's a receiver
my_object->moveToThread(thd);
cerr << "GUI thread: " << thread() << endl;
cerr << "Object thread: " << thd << endl;
cerr << "my_object thread: " << my_object->thread() << endl;
thd->start();
connect(this, SIGNAL(ConnectSocket()), my_object, SLOT(ConnectSocket()), Qt::QueuedConnection);
//I think this maneuver is the *only* way to ensure the QLocalSocket instance is in the worker thread.
emit ConnectSocket();
}
{
Q_OBJECT
public:
MyObject(MainWindow* _gui);
signals:
...
//various signals sent to the MainWindow instance
public slots:
void ConnectSocket();
void ReadyRead();
void LostConnection();
void SocketError(QLocalSocket::LocalSocketError err);
private:
MainWindow* gui;
QLocalSocket* my_local_socket;
};
MyObject::MyObject(MainWindow* _gui) : gui(_gui)
{
}
void MyObject::ConnectSocket()
{
bool b(true);
my_local_socket = new QLocalSocket();
my_local_socket->connectToServer("my_local_server", QLocalSocket::ReadOnly);
cerr << "my_local_socket thread: " <<my_local_socket->thread() << endl;
b &= connect(my_local_socket, SIGNAL(readyRead()), this, SLOT(ReadyRead()));
b &= connect(my_local_socket ,SIGNAL(error(QLocalSocket::LocalSocketError)),this, SLOT(SocketError(QLocalSocket::LocalSocketError)));
b &= connect(this,SIGNAL(my_object_signal()), gui, SLOT(gui_slot()), Qt::QueuedConnection);
// ... and several more of these
cerr << "Connect success, MyObject::ConnectSocket: " << b << endl;
}
//include file declaring MyObject
class MainWindow : public QGLWidget //The fact that I'm using GL here is not important
{
Q_OBJECT
MainWindow(QGLWidget* parent)
...
signals:
void ConnectSocket();
...
public slots:
//... various slots
...
private:
MyObject* my_object;
};
MainWindow::MainWindow
{
QThread* thd = new QThread();
my_object = new MyObject(this); //main_window is not the parent of my_object, it's a receiver
my_object->moveToThread(thd);
cerr << "GUI thread: " << thread() << endl;
cerr << "Object thread: " << thd << endl;
cerr << "my_object thread: " << my_object->thread() << endl;
thd->start();
connect(this, SIGNAL(ConnectSocket()), my_object, SLOT(ConnectSocket()), Qt::QueuedConnection);
//I think this maneuver is the *only* way to ensure the QLocalSocket instance is in the worker thread.
emit ConnectSocket();
}
class MyObject : public QObject
{
Q_OBJECT
public:
MyObject(MainWindow* _gui);
signals:
...
//various signals sent to the MainWindow instance
public slots:
void ConnectSocket();
void ReadyRead();
void LostConnection();
void SocketError(QLocalSocket::LocalSocketError err);
private:
MainWindow* gui;
QLocalSocket* my_local_socket;
};
MyObject::MyObject(MainWindow* _gui) : gui(_gui)
{
}
void MyObject::ConnectSocket()
{
bool b(true);
my_local_socket = new QLocalSocket();
my_local_socket->connectToServer("my_local_server", QLocalSocket::ReadOnly);
cerr << "my_local_socket thread: " <<my_local_socket->thread() << endl;
b &= connect(my_local_socket, SIGNAL(readyRead()), this, SLOT(ReadyRead()));
b &= connect(my_local_socket ,SIGNAL(error(QLocalSocket::LocalSocketError)),this, SLOT(SocketError(QLocalSocket::LocalSocketError)));
b &= connect(this,SIGNAL(my_object_signal()), gui, SLOT(gui_slot()), Qt::QueuedConnection);
// ... and several more of these
cerr << "Connect success, MyObject::ConnectSocket: " << b << endl;
}
To copy to clipboard, switch view to plain text mode
Bookmarks