QObject::connect可以對不繼承QObject的類也使用信號槽



#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
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM