Qt u盘自动升级软件

思路:

1.检测U盘是否存在

2.检测升级文件是否存在,升级文件版本是否比当前软件版本新

3.软件升级

//获取U盘  判断U盘名字是否正确, 升级文件是否存在。
void MainWindow::getUDisk()
{
    QMap<QString,QString> namePath;
    foreach (const QStorageInfo &storage, QStorageInfo::mountedVolumes())
    {
        if (storage.isValid() && storage.isReady())
        {
            UDiskPath = storage.rootPath();
            namePath.insert(storage.displayName(),storage.rootPath());
        }
    }
    QString path = namePath.value("LADYBUG");
    QFile file(path+"/"+app::updateFile);
    if(file.exists())
    {
        //存在 弹窗是否升级
    }
    else
    {
        //不存在
    }
}

//升级

    OperateThread *thread = new OperateThread;
    thread->setUpgradeFile(strSrcFile);
    ShowProgressDialog dialog(this);
 
    connect(thread, SIGNAL(emitFinish(int)), &dialog, SLOT(onThreadFinished(int)));
 
    thread->start();
 
    dialog.exec();

//升级界面

class ShowProgressDialog : public QDialog
{
    Q_OBJECT
public:
    explicit ShowProgressDialog(QWidget *parent = 0);
    ~ShowProgressDialog();
 
    void setTitleText(const QString &strText);
    void setMessageText(const QString &strMsg);
 
public slots:
    void on_btnOk_clicked();
    void onThreadFinished(int nExitCode);
 
private:
    QLabel          *m_labelTitle;
    QLabel          *m_labelMsg;
    QPushButton     *m_btnOk;
    QLabel          *m_labelWait;
    QMovie          *m_pMovie;
};
 
ShowProgressDialog::ShowProgressDialog(QWidget *parent) : QDialog(parent)
{
    setWindowFlags(Qt::CustomizeWindowHint | Qt::FramelessWindowHint | Qt::Dialog);
    setAttribute(Qt::WA_X11DoNotAcceptFocus);
    setFocusPolicy(Qt::NoFocus);
    //更改背景色
    QPalette palette = this->palette();
    QPixmap pix(":/res/dialog_bg.png");
    palette.setBrush(QPalette::Background,QBrush(pix));
    setAutoFillBackground(true);
    this->setPalette(palette);
 
    resize(410, 260);
    setMinimumSize(QSize(410, 260));
    setMaximumSize(QSize(410, 260));
 
    m_labelTitle = new QLabel(this);
    m_labelMsg = new QLabel(this);
    m_btnOk = new QPushButton(this);
    m_labelWait = new QLabel(this);
    m_pMovie = new QMovie(":/res/loading.gif");
 
    m_labelTitle->setGeometry(QRect(10, 1, 220, 30));
    m_labelMsg->setGeometry(QRect(30, 60, 350, 60));
    m_labelMsg->setWordWrap(true);
    m_btnOk->setGeometry(QRect(320, 180, 58, 58));
    m_labelWait->setGeometry(QRect(30, 120, 322, 18));
 
    m_labelWait->setMovie(m_pMovie);
    m_pMovie->start();
 
    m_labelTitle->setStyleSheet("font: bold 17px; color: white;");
    m_labelMsg->setStyleSheet("font: bold 17px ; color: black; ");
 
    m_btnOk->setStyleSheet("QPushButton{background-image: url(:/res/dialog_ok.png);border: 0px;}"
                           "QPushButton:pressed{background-image: url(:/res/dialog_ok_p.png);border: 0px;}");
 
    connect(m_btnOk , SIGNAL(clicked()) , this , SLOT(on_btnOk_clicked()));
 
    setTitleText("升级");
    setMessageText("正在升级,请勿插拔U盘!");
    m_btnOk->hide();
}
 
ShowProgressDialog::~ShowProgressDialog()
{
    delete m_labelTitle;
    delete m_labelMsg;
    delete m_btnOk;
}
 
void ShowProgressDialog::setTitleText(const QString &strText)
{
    m_labelTitle->setText(strText);
}
 
void ShowProgressDialog::setMessageText(const QString &strMsg)
{
    m_labelMsg->setText(strMsg);
}
 
void ShowProgressDialog::on_btnOk_clicked()
{
    QDialog::accept();
}
 
void ShowProgressDialog::onThreadFinished(int nExitCode)
{
    qDebug() << "nExitCode" << nExitCode;
    if (nExitCode == 0)
    {
        setMessageText("升级成功,请重新上电。");
        m_btnOk->show();
        m_pMovie->stop();
    }
    else
    {
        setMessageText("升级出错!请断电以恢复!");
        m_btnOk->show();
        m_pMovie->stop();
    }
}

 线程,继承自QThread,完成复制工作。使用QProcess来完成Linux下的解压和删除的工作。关于Qt的多线程,可以去查找一下其他的教程,这里就不做过多的解释。

class OperateThread : public QThread
{
    Q_OBJECT
public:
    explicit OperateThread(QObject *parent = 0);
 
public:
    void setUpgradeFile(QString strUpgradeFile)
    {
        m_UpgradeFile = strUpgradeFile;
    }
 
protected:
    void run();
 
signals:
    void emitFinish(int);
 
public slots:
 
private:
    void OprUpgradeUp();
 
private:
    QString  m_UpgradeFile;
};
 
OperateThread::OperateThread(QObject *parent) : QThread(parent)
{
 
}
 
void OperateThread::run()
{
    //很复杂的数据处理
    OprUpgradeUp();
}
 
void OperateThread::OprUpgradeUp()
{
#define DEST_DIR    "../"
 
    QString destDir = QString("%1/update.tar.gz").arg(DEST_DIR);
 
    if( QFile::copy(m_UpgradeFile, destDir) )
    {
        qDebug()<<"-------成功-----------";
    }
    else
    {
        qDebug()<<"-------出错-----------";
        //升级失败
        emit emitFinish(1);
        return;
    }
 
#ifdef Q_OS_LINUX
    QString exe = QString("tar -zxvf %1 -C %2").arg(destDir).arg(DEST_DIR);
    QProcess::execute(exe);
 
    //升级成功,自动同步磁盘并删除ARM上的升级包
    exe = QString("sync");
    QProcess::execute(exe);
    exe = QString("rm -rf %1").arg(destDir);
    QProcess::execute(exe);
#endif
    //升级成功
    emit emitFinish(0);
}

猜你喜欢

转载自blog.csdn.net/weixin_41882459/article/details/111368662