Solved threads and created objects
-
@SGaist said in threads and created objects:
If it's a QObject that is properly patented, it will move with it. Note that classes like QTcoSocket must be created in the new thread. You can however pass the descriptor like shown in the threaded QTcpSocket/Server example.
Actually a comment on that. Sockets that haven't been opened/bound can still be moved normally, however I agree it's a bad practice to depend on the actual implementation.
-
@SGaist said in threads and created objects:
If it's a QObject that is properly patented, it will move with it.
This is my current creation:
class Worker : public QObject { Q_OBJECT private: SerialPort m_serial; ... SerialPort::SerialPort(QObject *parent) : QObject (parent) { ...
Note that classes like QTcoSocket must be created in the new thread.
But not in the c'tor, correct? Because that is called prior to the thread move, so it would pertain to the original thread?
-
I fixed the typo of parented (damn autocorrect...)
Yes, m_serial will not be moved.
-
@SGaist said in threads and created objects:
I fixed the typo of parented (damn autocorrect...
Well, while you're at it..."QTcoSocket?"
-
If you want the
QObject
s to to be moved alongside with their parent to another thread, then you must pass them the parent. No parent, no way Qt to know whether the object's supposed to be moved. -
@mzimmers said in threads and created objects:
@SGaist said in threads and created objects:
I fixed the typo of parented (damn autocorrect...
Well, while you're at it..."QTcoSocket?"
Argle........ Done
-
@kshegunov I don't know how to do that in this case. If my SerialPort object is defined:
class SerialPort : public QObject { Q_OBJECT public: explicit SerialPort(Worker *parent = nullptr); ... SerialPort::SerialPort(Worker *parent) : QObject(parent)
Then how do I declare an instance of SerialPort in my worker object? If I do it like this:
SerialPort m_serial;
Then it doesn't seem to get moved.
-
The usual: pointer and allocate it in the constructor with a parent.
-
@SGaist OK, thanks. I had (incorrectly) inferred from kshegunov's post that it somehow could be done as an ordinary member variable.
-
@mzimmers No the inference is correct. This shall work, shan't it?
class Worker : public QObject { Q_OBJECT public: Worker(); private: SerialPort m_serial; }; Worker::Worker() : m_serial(this) { }
-
@kshegunov well...yes it does. I was missing the " : m_serial(this)" clause. Thanks!