qt moveToThread实例

tftp.pri

HEADERS += \
    $$PWD/tftp.h \
    $$PWD/tftpThread.h
SOURCES += \
    $$PWD/tftp.cpp \
    $$PWD/tftpThread.cpp

tftp.h

#ifndef TFTP_H
#define TFTP_H

#include 
#include "tftpThread.h"
#include 

class Tftp : public QObject{

  Q_OBJECT

public:
    explicit Tftp(QObject *parent = nullptr);
    ~Tftp();

signals:
    void signal_button1_3TftpReCfg_onClicked();
    void signal_readyReadStandard(QString str);
    void signal_tftpServiceStatus(QString str);
    void signal_checkTftpServiceStatus();
    void signal_tftpWorkStatus(bool str);
    void signal_button_3TftpReStart_onClicked();

public slots:
    void slot_button1_3TftpReCfg_onClicked();
    void slot_readyReadStandard(QString str);
    void slot_tftpServiceStatus(QString str);
    void slot_tftpWorkStatus(bool b);
    void slot_button_3TftpReStart_onClicked();

private:
    QThread   *gp_tftpThread;
    TftpThread *gp_tftpThreadObj;
};

#endif // TFTP_H

tftp.cpp

#include "tftp.h"
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

Tftp::Tftp(QObject *parent) :
    QObject(parent)
{
    gp_tftpThread = new QThread(this);
    gp_tftpThreadObj = new TftpThread();
    gp_tftpThreadObj->moveToThread(gp_tftpThread);
    QObject::connect(gp_tftpThread,SIGNAL(finished())
                     ,gp_tftpThreadObj,SLOT(deleteLater()));
    QObject::connect(this, SIGNAL(signal_button_3TftpReStart_onClicked()),
                     gp_tftpThreadObj, SLOT(slot_button_3TftpReStart_onClicked()));
    QObject::connect(gp_tftpThreadObj, SIGNAL(signal_readyReadStandard(QString)),
                     this, SLOT(slot_readyReadStandard(QString)));
    QObject::connect(gp_tftpThreadObj, SIGNAL(signal_tftpServiceStatus(QString)),
                     this, SLOT(slot_tftpServiceStatus(QString)), Qt::QueuedConnection);
    QObject::connect(gp_tftpThreadObj, SIGNAL(signal_tftpWorkStatus(bool)),
                     this, SLOT(slot_tftpWorkStatus(bool)));
    QObject::connect(this, SIGNAL(signal_button1_3TftpReCfg_onClicked()),
                     gp_tftpThreadObj, SLOT(slot_button1_3TftpReCfg_onClicked()));
    gp_tftpThread->start();
    emit signal_button_3TftpReStart_onClicked();
}

Tftp::~Tftp()
{
    gp_tftpThread->quit();
    gp_tftpThread->wait();
//    emit siganl_tftpStatus_onLoad();
}

void Tftp::slot_button1_3TftpReCfg_onClicked()
{
//    qDebug()<

tftpThread.h

#ifndef TFTPTHREAD_H
#define TFTPTHREAD_H

#include 
#include 
#include 
#include 
#include 

class TftpThread : public QObject
{
    Q_OBJECT

public:
    TftpThread(QObject *parent = nullptr);
    ~TftpThread();

signals:
    void signal_readyReadStandard(QString str);
    void signal_tftpServiceStatus(QString str);
    void signal_tftpWorkStatus(bool b);

public slots:
    void slot_button1_3TftpReCfg_onClicked();
    void slot_readyReadStandardOutput();
    void slot_readyReadStandardError();
    void slot_checkTftpServiceStatus();
    void slot_button_3TftpReStart_onClicked();

private:
    bool                  gv_isStop;
    QMutex                gv_stopMutex;
    QProcess              *gp_cmd;
    QString               gv_strOutput;
    bool                  gv_boolTftpError;
    QNetworkAccessManager *gp_manager;
    QNetworkReply         *gp_reply;
    bool                  gv_boolTftpService;
    bool                  gv_boolXinetdService;
    QString               gv_tftpServicePath;
    QString               gv_xinetdServicePath;

public:
    void gm_stop();
};

#endif // TFTPTHREAD_H

tftpThread.cpp

#include "tftpThread.h"
#include 
#include 
#include 
#include 
#include 
#include "localMethod/localMethod.h"
#include 

TftpThread::TftpThread(QObject *parent) : QObject(parent)
  ,gv_isStop(false)
  ,gv_stopMutex()
  ,gv_strOutput("")
  ,gv_boolTftpError(false)
  ,gv_boolTftpService(false)
  ,gv_boolXinetdService(false)
  ,gv_tftpServicePath("/usr/sbin/in.tftpd")
  ,gv_xinetdServicePath("/usr/sbin/xinetd")
{
    gp_cmd = new QProcess(this);
    QObject::connect(gp_cmd, SIGNAL(readyReadStandardOutput()),
                     this, SLOT(slot_readyReadStandardOutput()));
    QObject::connect(gp_cmd, SIGNAL(readyReadStandardError()),
                     this, SLOT(slot_readyReadStandardError()));
}

TftpThread::~TftpThread()
{
//    qDebug()<%2,thread id:%3").arg(__FUNCTION__).arg(__FILE__).arg(quintptr(QThread::currentThreadId()));
    gp_cmd->terminate();
}

void TftpThread::gm_stop()
{
    QMutexLocker locker(&gv_stopMutex);
//    qDebug()<<(QString("%1->%2,thread id:%3").arg(__FUNCTION__).arg(__FILE__).arg(quintptr(QThread::currentThreadId())));
    gv_isStop=true;
}

void TftpThread::slot_button1_3TftpReCfg_onClicked()
{
    QMutexLocker locker(&gv_stopMutex);
//    qDebug()<<"slot_tftp_menuItem_onClicked";
    gp_cmd->start("bash");
    gp_cmd->waitForStarted();
    gp_cmd->write(QString::fromLocal8Bit("yum -y remove tftp\n").toLocal8Bit());
    gp_cmd->write(QString::fromLocal8Bit("yum -y remove xinetd\n").toLocal8Bit());
    gp_cmd->write(QString::fromLocal8Bit("yum -y remove tftp-server\n").toLocal8Bit());
    gp_cmd->write(QString::fromLocal8Bit("yum -y install tftp\n").toLocal8Bit());
    gp_cmd->write(QString::fromLocal8Bit("yum -y install xinetd\n").toLocal8Bit());
    gp_cmd->write(QString::fromLocal8Bit("yum -y install tftp-server\n").toLocal8Bit());
    gp_cmd->waitForFinished();
    //#![service]
    QString lv_tftpResFile=":/files/body/status/tftp/service/tftp";
    QString lv_tftpDesFile="/etc/xinetd.d/tftp";
    QFile lv_tftpConfFile;
    lv_tftpConfFile.setFileName(lv_tftpDesFile);
    lv_tftpConfFile.remove();
    lv_tftpConfFile.copy(lv_tftpResFile, lv_tftpDesFile);
    //#![tftpboot]
    QString lv_tftpResDir=":/files/body/status/tftp/tftpboot/tftpboot";
    QString lv_tftpDesDir="/var/lib/tftpboot";
    LocalMethod::removeDir(lv_tftpDesDir);
    QStringList lv_extractList=JlCompress::extractDir(lv_tftpResDir, lv_tftpDesDir);
    int lv_extractListCount=lv_extractList.count();
//    qDebug()<<"extractDirCount"<start("bash");
    gp_cmd->waitForStarted();
    gp_cmd->write(QString::fromLocal8Bit("service tftp restart\n").toLocal8Bit());
    gp_cmd->write(QString::fromLocal8Bit("service xinetd restart\n").toLocal8Bit());
    gp_cmd->write(QString::fromLocal8Bit("chkconfig tftp on\n").toLocal8Bit());
    gp_cmd->write(QString::fromLocal8Bit("chkconfig xinetd on\n").toLocal8Bit());
    gp_cmd->waitForFinished();
    gv_stopMutex.unlock();
    TftpThread::slot_checkTftpServiceStatus();
    gv_stopMutex.lock();
}

void TftpThread::slot_readyReadStandardOutput()
{
//    qDebug()<readAllStandardOutput().data();
    gv_strOutput = gp_cmd->readAllStandardOutput().data();
//    qDebug()<closeWriteChannel();
}

void TftpThread::slot_readyReadStandardError()
{
//    qDebug()<readAllStandardError().data();
    emit signal_readyReadStandard(gp_cmd->readAllStandardError().data());
    gp_cmd->closeWriteChannel();
}

void TftpThread::slot_checkTftpServiceStatus()
{
    QMutexLocker locker(&gv_stopMutex);
    gp_cmd->start("bash");
    gp_cmd->waitForStarted();
    gp_cmd->write(QString::fromLocal8Bit("service tftp status\n").toLocal8Bit());
    gp_cmd->waitForFinished();
    QStringList lv_currentWords;
    QString lv_var;
    lv_currentWords=gv_strOutput.split('\n');
//    qDebug()<start("bash");
    gp_cmd->waitForStarted();
    gp_cmd->write(QString::fromLocal8Bit("service xinetd status\n").toLocal8Bit());
    gp_cmd->waitForFinished();
    QStringList lv_currentWords_1;
    QString lv_var_1;
    lv_currentWords_1=gv_strOutput.split('\n');
    for(int i=0;istart("bash");
    gp_cmd->waitForStarted();
    gp_cmd->write(QString::fromLocal8Bit("echo \"sucess, all the work has been done!\"\n").toLocal8Bit());
    gp_cmd->waitForFinished();
}

void TftpThread::slot_button_3TftpReStart_onClicked()
{
    QMutexLocker locker(&gv_stopMutex);
    gp_cmd->start("bash");
    gp_cmd->waitForStarted();
    gp_cmd->write(QString::fromLocal8Bit("service tftpd restart\n").toLocal8Bit());
    gp_cmd->write(QString::fromLocal8Bit("service tftpd restart\n").toLocal8Bit());
    gp_cmd->waitForFinished();
    gv_stopMutex.unlock();
    TftpThread::slot_checkTftpServiceStatus();
    gv_stopMutex.lock();
}


 

你可能感兴趣的:(qt)