• 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. ";
    printf("%s ", 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
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    js的BOM对象完全解析
    转:JavaScript中的this陷阱的最全收集
    转载:冷门js技巧
    MVVM的架构设计与团队协作 with StoryBoard
    ReactiveCocoa & FRP & MVVM
    ReactiveCocoa的冷信号与热信号 探讨
    Git本地项目上传 & SourceTree & GitHub 简单使用
    Monad / Functor / Applicative 浅析
    map & flatMap 浅析
    Swift -> Let & Var 背后编程模式 探讨
  • 原文地址:https://www.cnblogs.com/findumars/p/10974617.html
Copyright © 2020-2023  润新知