[Qt-interest] global thread not running until main app starts
Paul England
pengland at cmt-asia.com
Wed Jan 14 02:09:29 CET 2009
I have a QThread (well, a couple) which have global scope which contain
a QTcpSocket which gets data from a server.
dialog* main_window;
double data;
int main( int argc, char* argv[] )
{
QApplication app( argc, argv );
my_thread threads[3];
for ( int i = 0; i < 3; i++ ) {
threads[i].setup( host, port );
threads[i].start();
}
sleep( 2 );
dialog my_dialog;
main_window = &my_dialog;
main_window->show();
return app.exec()
}
Here's the implementation of the thread...truncated.
my_thread::run()
{
socket = new QTcpSocket;
connect( socket, SIGNAL( readyRead() ), this, SLOT(
socket_ready_read() ) );
exec();
}
void my_thread::socket_ready_read()
{
printf( "Got something\n" );
while ( socket->canReadline() ) {
// parse line
// store value in 'data', which has global scope.
}
}
Generally these things connect and immediately have data. I want the
GUI to be built around some values of that data. As you can see, I
print out a line whenever I get something. "dialog" is a QDialog. In
it's constructor, I check if I've got any data from a thread. If it's
not there, it dies (and it always dies). If I use some hard coded value
instead, it starts, and right when the constructor returns, the data
gushes in (as it's been cached for 2 seconds).
Shouldn't this thread execute separtely from the GUI? Am I missing
something?
Thanks
Paul
More information about the Qt-interest-old
mailing list