eekhoorn12
16th June 2010, 18:46
Hey All
I am writing a program that uses QTcpSocket to send data, because i want multiple connections at the same time and keep the application responsive i want to use QThread to send the data in the background.
Before I want to implement this in my program i was trying to create a small test program to test everything and learn how it works. But i keep getting the same problem every time.
When i close the application i get a warning saying:
QWaitCondition: Destroyed while threads are still waiting
I have search these forums and other forums for a solution but couldn't find the right one.
A few days ago i found this post http://www.qtcentre.org/forum/f-qt-programming-2/t-non-gui-thread-blocking-gui-3848.html#14
But when i add a QTcpSocket to the object created in the thread and close the application i get the error.
This is the code i have right now:
#include <QtGui>
#include <QtDebug>
#include <QTcpSocket>
class TestObject : public QObject
{
Q_OBJECT
public:
TestObject(QObject* parent = 0) : QObject(parent)
{
testSocket = new QTcpSocket();
testSocket->connectToHost("www.google.com",80);
}
private:
QTcpSocket *testSocket;
public slots:
void testObjectSlot()
{
qDebug() << "TestObject::testObjectSlot()" << QThread::currentThread();
}
void exitObjectSlot()
{
qDebug() << "TestObject::exitObjectSlot()" << QThread::currentThread();
if(testSocket != NULL){
testSocket->disconnectFromHost();
if (testSocket->state() == QAbstractSocket::UnconnectedState ||
testSocket->waitForDisconnected(1000))
qDebug("Socket is disconnected!");
testSocket->deleteLater();
testSocket = NULL;
}
}
};
///////////////////////////////////////////////////////////////////////////////
class TestThread : public QThread
{
Q_OBJECT
public:
TestThread(QObject* parent = 0) : QThread(parent)
{
start();
}
~TestThread()
{
qDebug() << "TestThread::~TestThread()" << QThread::currentThread();
quit();
wait();
}
public slots:
void testThreadSlot()
{
qDebug() << "TestThread::testThreadSlot()" << QThread::currentThread();
}
signals:
void testObjectSignal();
void exitObjectSignal();
protected:
void run()
{
TestObject object;
connect(this, SIGNAL(testObjectSignal()), &object, SLOT(testObjectSlot()));
connect(this, SIGNAL(exitObjectSignal()), &object, SLOT(exitObjectSlot()));
exec();
}
};
///////////////////////////////////////////////////////////////////////////////
class TestWidget : public QWidget
{
public:
TestWidget(QWidget* parent = 0) : QWidget(parent)
{
QPushButton* button1 = new QPushButton("Test QThread directly", this);
connect(button1, SIGNAL(clicked()), &thread, SLOT(testThreadSlot()));
QPushButton* button2 = new QPushButton("Test QObject created in QThread::run()", this);
connect(button2, SIGNAL(clicked()), &thread, SIGNAL(testObjectSignal()));
QPushButton* button3 = new QPushButton("Stop Thread", this);
connect(button3, SIGNAL(clicked()), &thread, SIGNAL(exitObjectSignal()));
QVBoxLayout* layout = new QVBoxLayout;
layout->addWidget(button1);
layout->addWidget(button2);
layout->addWidget(button3);
setLayout(layout);
}
~TestWidget()
{
}
private:
TestThread thread;
};
///////////////////////////////////////////////////////////////////////////////
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
qDebug() << "main()" << QThread::currentThread();
TestWidget w;
w.show();
return a.exec();
}
#include "main.moc"
Hope you guys can help me
Marcel
I am writing a program that uses QTcpSocket to send data, because i want multiple connections at the same time and keep the application responsive i want to use QThread to send the data in the background.
Before I want to implement this in my program i was trying to create a small test program to test everything and learn how it works. But i keep getting the same problem every time.
When i close the application i get a warning saying:
QWaitCondition: Destroyed while threads are still waiting
I have search these forums and other forums for a solution but couldn't find the right one.
A few days ago i found this post http://www.qtcentre.org/forum/f-qt-programming-2/t-non-gui-thread-blocking-gui-3848.html#14
But when i add a QTcpSocket to the object created in the thread and close the application i get the error.
This is the code i have right now:
#include <QtGui>
#include <QtDebug>
#include <QTcpSocket>
class TestObject : public QObject
{
Q_OBJECT
public:
TestObject(QObject* parent = 0) : QObject(parent)
{
testSocket = new QTcpSocket();
testSocket->connectToHost("www.google.com",80);
}
private:
QTcpSocket *testSocket;
public slots:
void testObjectSlot()
{
qDebug() << "TestObject::testObjectSlot()" << QThread::currentThread();
}
void exitObjectSlot()
{
qDebug() << "TestObject::exitObjectSlot()" << QThread::currentThread();
if(testSocket != NULL){
testSocket->disconnectFromHost();
if (testSocket->state() == QAbstractSocket::UnconnectedState ||
testSocket->waitForDisconnected(1000))
qDebug("Socket is disconnected!");
testSocket->deleteLater();
testSocket = NULL;
}
}
};
///////////////////////////////////////////////////////////////////////////////
class TestThread : public QThread
{
Q_OBJECT
public:
TestThread(QObject* parent = 0) : QThread(parent)
{
start();
}
~TestThread()
{
qDebug() << "TestThread::~TestThread()" << QThread::currentThread();
quit();
wait();
}
public slots:
void testThreadSlot()
{
qDebug() << "TestThread::testThreadSlot()" << QThread::currentThread();
}
signals:
void testObjectSignal();
void exitObjectSignal();
protected:
void run()
{
TestObject object;
connect(this, SIGNAL(testObjectSignal()), &object, SLOT(testObjectSlot()));
connect(this, SIGNAL(exitObjectSignal()), &object, SLOT(exitObjectSlot()));
exec();
}
};
///////////////////////////////////////////////////////////////////////////////
class TestWidget : public QWidget
{
public:
TestWidget(QWidget* parent = 0) : QWidget(parent)
{
QPushButton* button1 = new QPushButton("Test QThread directly", this);
connect(button1, SIGNAL(clicked()), &thread, SLOT(testThreadSlot()));
QPushButton* button2 = new QPushButton("Test QObject created in QThread::run()", this);
connect(button2, SIGNAL(clicked()), &thread, SIGNAL(testObjectSignal()));
QPushButton* button3 = new QPushButton("Stop Thread", this);
connect(button3, SIGNAL(clicked()), &thread, SIGNAL(exitObjectSignal()));
QVBoxLayout* layout = new QVBoxLayout;
layout->addWidget(button1);
layout->addWidget(button2);
layout->addWidget(button3);
setLayout(layout);
}
~TestWidget()
{
}
private:
TestThread thread;
};
///////////////////////////////////////////////////////////////////////////////
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
qDebug() << "main()" << QThread::currentThread();
TestWidget w;
w.show();
return a.exec();
}
#include "main.moc"
Hope you guys can help me
Marcel