qt控制台程序在window直接退出方法

来源:互联网 发布:淘宝网比价插件 编辑:程序博客网 时间:2024/06/15 08:29
#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_OBJECTpublic:    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.");}

0 0