class MyThread : public QThread
{
public:
void run();
};
void MyThread::run()
{
QTcpSocket socket;
// connect QTcpSocket's signals somewhere meaningful
...
socket.connectToHost(hostName, portNumber);
exec();
}
main()
{
QCoreApplication app(...);
MyThread th;
...
app.exec();
...
th.quit();
th.wait();
return 0;
}
while(...)
{
ReadFile(...);
}
if(QThreadPool::globalInstance()->activeThreadCount()) QThreadPool::globalInstance()->waitForDone();
class MyThread : public QThread
{
bool m_bExit;
public:
void run();
};
void MyThread::run()
{
// ...
while( !m_bExit)
{
// ...
ReadFile(...);
}
}
main()
{
QCoreApplication app(...);
MyThread th;
...
app.exec();
...
th.quit();
th.wait();
return 0;
}
дойдет выполнение до th.quit(); ?#include <QThread>
class Thread : public QThread {
Q_OBJECT
private:
virtual void run(void);
public :
explicit Thread(QObject* parent) : QThread(parent) {}
virtual ~Thread() {}
signals:
void send(void);
};
class Owner : public QObject {
Q_OBJECT
private:
Thread m_thread;
virtual void timerEvent ( QTimerEvent * event );
public :
explicit Owner(QObject* parent = nullptr);
virtual ~Owner() {}
public slots:
void recv(void);
};
#include "main.h"
#include <QtCore/QCoreApplication>
#include <QDebug>
void Thread::run( void )
{
forever
{
emit send();
QThread::msleep(1000);
}
}
Owner::Owner( QObject* parent /*= nullptr*/ ) : QObject(parent), m_thread(this)
{
connect(&m_thread, SIGNAL(send()), this, SLOT(recv()));
m_thread.start();
startTimer(5000);
}
void Owner::recv( void )
{
qDebug() << "received signal";
}
void Owner::timerEvent( QTimerEvent * event )
{
m_thread.quit();
m_thread.wait();
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Owner owner;
return a.exec();
}
void Thread::run( void )
{
forever
{
emit send();
QThread::msleep(1000);
}
}
class MyThread : public QThread
{
public:
void run();
};
void MyThread::run()
{
QTcpSocket socket;
// connect QTcpSocket's signals somewhere meaningful
...
socket.connectToHost(hostName, portNumber);
exec();
}