amdreallyfast
25th May 2014, 16:16
I spent about 10 hours yesterday with Qt stuff and I'm kind of exhausted, so I'm really bad at searching for answers on my own right now. If you guys could help me out, that would be appreciated.
The basic idea is that I want to start up TCP socket communication (it reads accelerometer data from a microcontroller), and I want to start up a QGLWidget to do other stuff. The QTcpSocket starts up in it's own thread (apparently on its own) though and can't be set as the QObject child of the QGLWidget, or at least that was my conclusion based on the threading errors that popped up. Then I tried to get a basic QTcpSocket to work and it broke. I am failing here.
What is wrong with the following code? I spits out the following runtime error:
QObject: Cannot create children for a parent that is in a different thread.
(Parent is in my_TCP_Q_socket_with_readyRead(*some address*), parent's thread is QThread(*some address*), current thread is QThread(*some address*)
For the record, I am I using QtCreator now. I figured out how to move between Visual Studio and QtCreator, so I just picked one.
Here is main.cpp:
#include <QTcpSocket>
#include <QObject>
#include <iostream>
using std::cout;
using std::endl;
class my_TCP_Q_socket_with_readyRead : public QObject
{
public:
explicit my_TCP_Q_socket_with_readyRead(QObject *parent = 0) :
QObject(parent)
{
m_socket_ptr = new QTcpSocket(this); // complains about parent-child threading
//m_socket_ptr = new QTcpSocket(0); // no error (??but what is it doing? it is silent??)
}
private:
Q_OBJECT
QTcpSocket *m_socket_ptr;
};
#include "main.moc"
int main(int argc, char *argv[])
{
my_TCP_Q_socket_with_readyRead s;
return 0;
}
What am I doing wrong? I found that this code doesn't complain if I provide a 0 (integer zero) as the argument to the QTcpSocket, but why?
As a related followup question, is it possible to use signals and slots to pass data from the thread that has the QTcpSocket to the thread that contains my QGLWidget? I think I can figure out how to make signals and slots work to call a function in QGLWidget once my custom QTcpSocket object is done reading data, but I don't know how to pass data from one to the other. Or am I missing the way this works? I haven't discovered how or when Qt starts threads on its own (which it clearly does considering that the error shown above specifies two threads).
The basic idea is that I want to start up TCP socket communication (it reads accelerometer data from a microcontroller), and I want to start up a QGLWidget to do other stuff. The QTcpSocket starts up in it's own thread (apparently on its own) though and can't be set as the QObject child of the QGLWidget, or at least that was my conclusion based on the threading errors that popped up. Then I tried to get a basic QTcpSocket to work and it broke. I am failing here.
What is wrong with the following code? I spits out the following runtime error:
QObject: Cannot create children for a parent that is in a different thread.
(Parent is in my_TCP_Q_socket_with_readyRead(*some address*), parent's thread is QThread(*some address*), current thread is QThread(*some address*)
For the record, I am I using QtCreator now. I figured out how to move between Visual Studio and QtCreator, so I just picked one.
Here is main.cpp:
#include <QTcpSocket>
#include <QObject>
#include <iostream>
using std::cout;
using std::endl;
class my_TCP_Q_socket_with_readyRead : public QObject
{
public:
explicit my_TCP_Q_socket_with_readyRead(QObject *parent = 0) :
QObject(parent)
{
m_socket_ptr = new QTcpSocket(this); // complains about parent-child threading
//m_socket_ptr = new QTcpSocket(0); // no error (??but what is it doing? it is silent??)
}
private:
Q_OBJECT
QTcpSocket *m_socket_ptr;
};
#include "main.moc"
int main(int argc, char *argv[])
{
my_TCP_Q_socket_with_readyRead s;
return 0;
}
What am I doing wrong? I found that this code doesn't complain if I provide a 0 (integer zero) as the argument to the QTcpSocket, but why?
As a related followup question, is it possible to use signals and slots to pass data from the thread that has the QTcpSocket to the thread that contains my QGLWidget? I think I can figure out how to make signals and slots work to call a function in QGLWidget once my custom QTcpSocket object is done reading data, but I don't know how to pass data from one to the other. Or am I missing the way this works? I haven't discovered how or when Qt starts threads on its own (which it clearly does considering that the error shown above specifies two threads).