QObject::connect可以对不继承QObject的类也使用信号槽

时间:2022-02-21 22:58:54

#include <QCoreApplication>
#include <QDebug>
#include <QObject>
#include <QThread>
#include <string>
#include <iostream>
#include "mythread.h"
using namespace std;

//qt控制台程序在window直接退出方法。
int main(int argc, char *argv[])
{
QString str;
QTextStream out(stdout);
out<<"input data"<<endl;
QTextStream in(stdin);
in >> str;

out<<"input str="<<str<<endl;

QCoreApplication a(argc, argv);

MyThread *mythread=new MyThread();

//退出方法二 通过在子线程里面调用quit(),退出整个程序
QObject::connect(mythread,SIGNAL(finished()),&a,SLOT(quit()));
mythread->start();

qDebug()<<"test ok"<<endl;\

//这些直接退出失效?
//a.quit();
//a.exit(0);
// QCoreApplication::exit(0);
//QCoreApplication::quit();
qDebug()<<"test quit"<<endl;
//exit(0);//退出方法一
return a.exec();
//return 0;
}

#ifndef MYTHREAD_H
#define MYTHREAD_H

#include <QObject>
#include <QThread>

class MyThread : public QThread
{
Q_OBJECT
public:
explicit MyThread(QObject *parent = 0);

protected:
void run();
};

#endif // MYTHREAD_H

#include "mythread.h"
#include <string>
#include <iostream>
using namespace std;

MyThread::MyThread(QObject *parent):QThread(parent)
{

}

void MyThread::run()
{
char *pszTip = "Press 'Q' exit application.\n";
printf("%s\n", pszTip);
/*
while (true)
{
std::string line;
std::cin>>line;
std::cout<<line<<std::endl;
printf("%s", pszTip);
if (line.compare("Q")==0)
{
break;
}
}
*/

printf("Done.");

}
---------------------
作者:yunshouhu
来源:CSDN
原文:https://blog.csdn.net/earbao/article/details/53771496
版权声明:本文为博主原创文章,转载请附上博文链接!