PDA

View Full Version : Two windows in the same main



nkint
12th June 2013, 10:57
Hi!
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?

Then...
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;
r.show();
WindowSender s;
s.show();

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?

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

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

nkint
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?

wysota
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 :)

nkint
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?

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

nkint
13th June 2013, 13:47
:)
sorry?

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

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

anda_skoa
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

Cheers,
_