日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

4.QT中进程操作,线程操作

發(fā)布時(shí)間:2024/9/27 c/c++ 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 4.QT中进程操作,线程操作 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

  • QT中的線程操作

  • T19Process.pro

    SOURCES += \

    ??? main.cpp

    ?

    CONFIG += C++11

    main.cpp

    #include <QCoreApplication> #include <QProcess> #include <QDebug> int main(int argc, char** argv) { ??? QCoreApplication app(argc, argv); ? ??? QProcess process; ??? // process.start("/home/xuegl/T0718/build-T18Database-Desktop-Debug/T18Database"); ??? process.start("ssh root@42.121.13.248"); ??? // process.start("ssh", QStringList() << "root@42.121.13.248" << "aa" << "bbb"); ??? // process.write("1\n", 2); ??? process.waitForFinished(); ? ??? // process.waitForFinished(); ??? qDebug() << process.readAll(); ??? // qDebug() << process.exitCode(); ? ??? return app.exec(); }
  • 多線程(可以通過moveToThread(QThread *)的方法指定給指定的線程

  • 新建項(xiàng)目T20Thread,項(xiàng)目代碼如下:

    T20Thread.pro

    HEADERS += \

    ??? Worker.h \

    ??? MyThread.h

    ?

    SOURCES += \

    ??? Worker.cpp \

    ??? MyThread.cpp \

    ??? main.cpp

    Worker.h

    #ifndef WORKER_H

    #define WORKER_H

    ?

    #include <QObject>

    #include <QThread>? //要開啟線程的時(shí)候需要使用頭文件<QThread>

    #include <QDebug>

    class Worker : public QObject

    {

    ??? Q_OBJECT

    public:

    ??? explicit Worker(QObject *parent = 0);

    ?

    ??? QThread _thread;

    ?

    ??? bool event(QEvent *ev)

    ??? {

    ??????? //通過QThread::currentThread()可以獲得當(dāng)前線程信息

    ??????? qDebug() << "event:" << QThread::currentThread();

    ??????? return QObject::event(ev);

    ??? }

    signals:

    ?

    public slots:

    ??? void doWork()

    ??? {

    ??????? qDebug() << QThread::currentThread();

    ??? }

    };

    ?

    #endif // WORKER_H

    Worker.cpp

    #include "Worker.h"

    ?

    Worker::Worker(QObject *parent) :

    ??? QObject(parent)

    {

    ??? //this->moveToThread(&_thread);

    ??? _thread.start();

    ??? connect(&_thread, SIGNAL(finished()), this, SLOT(deleteLater()));

    }

    MyThread.h

    #ifndef MYTHREAD_H

    #define MYTHREAD_H

    ?

    #include <QThread>

    #include <QDebug>

    class MyThread : public QThread

    {

    ??? Q_OBJECT

    public:

    ??? explicit MyThread(QObject *parent = 0);

    ?

    ??? void foo()

    ??? {

    ??????? qDebug() << QThread::currentThread();

    ??? }

    ?

    ??? void run()

    ??? {

    ??????? foo();

    ??????? qDebug() << "thread is run";

    ??? }

    ?

    signals:

    ?

    public slots:

    ?

    };

    ?

    #endif // MYTHREAD_H

    MyThread.cpp

    #include "mythread.h"

    ?

    MyThread::MyThread(QObject *parent) :

    ??? QThread(parent)

    {

    }

    main.cpp

    #include <QCoreApplication>

    #include "mythread.h"

    #include "worker.h"

    #include <QTimer>

    int main(int argc, char* argv[])

    {

    ??? QCoreApplication app(argc, argv);

    #if 0

    ??? MyThread thread;

    ??? thread.start();

    ?

    ??? thread.foo();

    #endif

    ?

    ??? qDebug() << "main thread is"<<QThread::currentThread();

    ??? Worker* worker = new Worker();

    ??? QTimer* timer = new QTimer;

    ??? //worker->moveToThread(&thread);

    ?

    ??? QObject::connect(timer, SIGNAL(timeout()), worker, SLOT(doWork()));

    ??? timer->setInterval(1000);

    ??? timer->start();

    ?

    ??? return app.exec();

    }

    運(yùn)行結(jié)果:

    ?

    總結(jié)

    以上是生活随笔為你收集整理的4.QT中进程操作,线程操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。