- QT中的线程操作
T19Process.pro |
SOURCES += \ main.cpp CONFIG |
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 [email protected]"); // process.start("ssh", QStringList() << "[email protected]" << "aa" << "bbb"); // process.write("1\n", 2); process.waitForFinished(); // process.waitForFinished(); qDebug() << process.readAll(); // qDebug() << process.exitCode(); return app.exec(); } |
- 多线程(可以通过moveToThread(QThread *)的方法指定给指定的线程)
新建项目T20Thread,项目代码如下:
T20Thread.pro |
HEADERS += \ Worker.h MyThread.h SOURCES Worker.cpp MyThread.cpp main.cpp |
Worker.h |
#ifndef WORKER_H #define #include #include #include class { Q_OBJECT public: explicit QThread bool { //通过QThread::currentThread()可以获得当前线程信息 qDebug() return } signals: public void { qDebug() } }; #endif |
Worker.cpp |
#include "Worker.h" Worker::Worker(QObject QObject(parent) { //this->moveToThread(&_thread); _thread.start(); connect(&_thread, } |
MyThread.h |
#ifndef MYTHREAD_H #define #include #include class { Q_OBJECT public: explicit void { qDebug() } void { foo(); qDebug() } signals: public }; #endif |
MyThread.cpp |
#include "mythread.h" MyThread::MyThread(QObject QThread(parent) { } |
main.cpp |
#include <QCoreApplication> #include #include #include int { QCoreApplication #if MyThread thread.start(); thread.foo(); #endif qDebug() Worker* QTimer* //worker->moveToThread(&thread); QObject::connect(timer, timer->setInterval(1000); timer->start(); return } |
运行结果: |