QFtp操作记录-创新互联

#include "widget.h"
#include "./ui_widget.h"
#include#include#include#include#include#include#include#include#include#include#include#include#includeWidget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{ui->setupUi(this);
  ftp = new QFtp;
  if (ftp == nullptr) {qDebug()<< __func__<< ":No Mem alloc for ftp instance ... ... \n";
    exit(1);
  }
  connect(ftp, SIGNAL(commandFinished(int, bool)), this,
          SLOT(checkFTPstate(int, bool)));
  connect(ftp, SIGNAL(dataTransferProgress(qint64, qint64)), this,
          SLOT(progressBar_update(qint64, qint64)));
  connect(this, SIGNAL(EmitError(QString,QString)), this, SLOT(errorMessage(QString,QString)));
  connect(ftp,SIGNAL(stateChanged(int)),SLOT(FTPstate(int)));
  connect(this,SIGNAL(EmitPutFile(QFileInfo)),this,SLOT(PutFile(QFileInfo)));
  connect(ftp,SIGNAL(commandStarted(int)),this,SLOT(ftp_start(int)));
}

Widget::~Widget()
{delete ui;
}

void Widget::on_driver_path_sel_clicked()
{upgrade_file_path = QFileDialog::getOpenFileName(this,"选择升级的驱动文件","./");
    driver_file.setFile(upgrade_file_path);
    ui->dirver_path->setText(this->upgrade_file_path); 
}

void Widget::on_fpga_path_sel_clicked()
{upgrade_file_path =
        QFileDialog::getOpenFileName(this, "选择升级的FPGA文件", "./");
    fpga_file.setFile(upgrade_file_path);
    ui->fpga_path->setText(this->upgrade_file_path);
}

void Widget::on_database_path_sel_clicked()
{upgrade_file_path =
        QFileDialog::getOpenFileName(this, "选择升级的数据库文件", "./");
    database_file.setFile(upgrade_file_path);
    ui->database_path->setText(this->upgrade_file_path);
}

void Widget::on_start_clicked()
{QString type;
    if (ui->FILE_trans_type->currentText() == "FTP") {type = "ftp";
    } else if (ui->FILE_trans_type->currentText() == "SFTP") {type="sftp"; 
   }

   std::vector>CheckList  {std::make_pair(ui->driver_check,driver_file),std::make_pair(ui->fpga_check,fpga_file),std::make_pair(ui->database_check,database_file)};

   for (auto i : CheckList) {if (i.first->isChecked()) {  emit EmitPutFile(i.second);
        }
   }
}

void Widget::progressBar_update(qint64 readBytes, qint64 all) {auto percent = ((qreal)readBytes / all) * 100;
   ui->upgrade_bar->setValue(percent);
   ui->upgrade_label_status->setText(QString().fromStdString(std::to_string(readBytes/1024))+"/"+ QString().fromStdString(std::to_string(all/1024))+" KBytes");
   if(percent == 100){}
}

void Widget::errorMessage(QString title,QString msg)  {QMessageBox::information(this,title,msg);
}

void Widget::successMessage(QString msg) {QMessageBox::information(this,"成功提示",msg);
}

void Widget::checkFTPstate(int st, bool error) {if (ftp->currentCommand() == QFtp::Command::Put) {if (error) {  emit EmitError("上传失败", ftp->errorString());
        } else {this->setWindowTitle("上传成功");
        }
        for (auto i = FILE_LIST.begin() ; i< FILE_LIST.end() ; i++) {if (i->second == st) {  if (i->first != nullptr) {qDebug()<< "Delete "<< i->first->fileName()
              << " cmd id: "<< st;
                delete i->first;
                i->first = nullptr;
              }
              i = FILE_LIST.erase(i);
            } else {  ++i;
          }
        }
        if (!ftp->hasPendingCommands()) {  EmitError("升级提示","升级成功");
        }
  } else if (ftp->currentCommand() == QFtp::Command::ConnectToHost) {if (error) {  emit EmitError("链接失败", ftp->errorString());
        } else {  this->setWindowTitle("链接成功");
        }
  } else if (ftp->currentCommand() == QFtp::Command::Login) {if (error) {  emit EmitError("登陆失败", ftp->errorString());
        } else {  this->setWindowTitle("登陆成功");
        }
  } else if (ftp->currentCommand() == QFtp::Command::Close) {emit EmitError("链接提示", "断开链接");
  }
}

void Widget::FTPstate(int st) {if (st == QFtp::State::Connecting) {this->setWindowTitle("正在链接");
  } else if (st == QFtp::State::Unconnected) {this->setWindowTitle("失去链接");
  }else if(st == QFtp::State::LoggedIn){  this->setWindowTitle("登录成功");
      ui->login->setText("断开链接");
  }
}

void Widget::ftp_start(int){if(ftp->currentCommand() == QFtp::Put){   this->setWindowTitle("开始上传");
    }
}

void Widget::PutFile(QFileInfo FileName) {if(ftp->state() == QFtp::Connected || ftp->state() == QFtp::LoggedIn){file = new QFile;
        if(file != nullptr){file->setFileName(FileName.absoluteFilePath());
        if (file->open(QIODevice::ReadOnly)) {  ui->upgrade_bar->setValue(0);
          this->setWindowTitle("正在升级: " + FileName.fileName());
          FILE_LIST.append(
              std::make_pair(file, ftp->put(file, FileName.fileName())));
        }}
   }
}

void Widget::on_login_clicked()
{if(ui->login->text() == "断开链接"){ftp->close();
        ui->login->setText("链接板卡");
    }else{QString hostinfo = ui->ip_addr->text();
    ftp->connectToHost(hostinfo);
    ftp->login("admin", "admin");
    }
}

血泪教训, QT5 ~ QT6 是一个大的版本跨越,NETWORK 的框架,和基类,变的要骂人了,找的QFtp 的模块,改了变天源码,卡在了, QString 的 ASSERT 上面,我就日了,回退到,5.15,

创新互联长期为上千客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为乐东黎族企业提供专业的网站制作、成都网站建设乐东黎族网站改版等技术服务。拥有十多年丰富建站经验和众多成功案例,为您定制开发。

虽然没弄过QT之前,但是,看C++ 的代码, 难怪,Linus 喷, 好多地方,真的挺恶心, 一坨坨的感觉

这算是一个DEMO 的版本

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


文章名称:QFtp操作记录-创新互联
转载源于:http://azwzsj.com/article/dccjsd.html