View Full Version : Two windows in the same main

12th June 2013, 10:57
Sorry if this is a dumb question but I have no past experience with threads and/or network communication.
I'm making a simple tcp communication: I have a sender and I have a receiver.
For now I have 2 different project in QtCreator, one for the sender and one for the receiver. I'm testing the network communication in this way: I open the receiver and then and I open the sender.
But I'd like to have a single progect called SenderReceiverTest that open 2 window, one for the receiver, one for the sender, in the same QtCreator project.
First question: Is this single-project stuff a good idea?

If i have something like this:

#include <QtGui/QApplication>
#include "windowsender.h"
#include "windowreceiver.h"

int main(int argc, char *argv[])

QApplication a(argc, argv);
QApplication a(argc, argv);

WindowReceiver r;
WindowSender s;

QObject::connect(&r, SIGNAL(windowClosed()), // custom virtual void closeEvent(QCloseEvent *e)
&s, SLOT(close()));
QObject::connect(&s, SIGNAL(windowClosed()), // custom virtual void closeEvent(QCloseEvent *e)
&r, SLOT(close()));

return a.exec();

Second question:
The two window are in the same thread, aren't they? I'd like to have the two windows in different thread for testing real async communication.. and for that I should have two dirrefent thread.. right? How can I do this?

12th June 2013, 15:43
You can't. Qt GUI objects can exist only in main thread.

12th June 2013, 17:31
For testing asynchronous communication you don't need threads.

12th June 2013, 18:26
thank you for the answer.

so the way i'm handling it now (two window.show in the same main) is a right way?
is there a better way?

13th June 2013, 09:21
It's ok but don't create two instances of QApplication. One is enough. Your current code would not even compile :)

13th June 2013, 10:16
Ah yeah sorry I have done some cut and paste for deleting comments and renaming the objects and there was a mistake.. : )

So it is ok, I can test how many TCP package can be sent for a second and stuffs like this, good, thank you!

One more question:
what happens if I use moveToThread methods on a QWidget subclass?

13th June 2013, 12:38
Most likely - BOOM!
Less likely, a warning and a no-op.

13th June 2013, 13:47

boom could mean runtime crash but.. no-op?

anyway thank you for the answer i have understand i don't have to do it.

13th June 2013, 14:57
Yes, boom refers to crashing :)
no-op is short for "no operation", a term using in computer science to say that an instruction will actually not do anything.
Like an empty method