1.最近再开发一个批量查询上传的小工具,要增加一个批量选择的复选框,全选或者全不选;
QCheckBox *checkBox = new QCheckBox(ui.tableWidget); checkBox->resize(40, 24); ui.tableWidget->setCellWidget(0, 0, checkBox); checkBox->setStyleSheet("margin-left:4px;");//左侧的偏移量,不设置QCheckBox会紧靠左边,没有和下面的对齐; checkBox->setChecked(false); ui.tableWidget->setColumnWidth(0, 24); ui.tableWidget->setColumnWidth(1, 40); connect(checkBox, &QCheckBox::clicked, this, &signature::SelectAllFiles);
其他函数实现如下
#include "signature.h" #include "MultiFileDirDialog.h" #include <QFileInfoList> #include "curl.h" #include<QProcess> #include <QMessageBox> #include <QCheckBox> #pragma execution_character_set("utf-8") signature::signature(QWidget *parent) : QMainWindow(parent) { ui.setupUi(this); setWindowTitle(tr("批量数字签名工具")); connect(ui.pBSouuce, &QPushButton::clicked, this, [=]() { QString strPath = SlotGetPath(); ui.lineEditInputPath->setText(strPath); StartSearchFile(); }); connect(ui.pBTarget, &QPushButton::clicked, this, [=]() { QString strPath = SlotGetPath(); ui.lineEditOutPutPath->setText(strPath); m_strTargetPath = strPath; }); ui.tableWidget->setColumnCount(3); ui.tableWidget->verticalHeader()->setVisible(false); /*connect(ui.pBSearch, &QPushButton::clicked, this, [=]() { StartSearchFile(); });*/ connect(ui.pushCopyFile, &QPushButton::clicked, this, [=]() { StartCopyFile(); }); /*connect(ui.pushReturn, &QPushButton::clicked, this, [=]() { CopyFileToSourcePath(); });*/ connect(ui.pBUpload, &QPushButton::clicked, this, [=]() { StartUploadFileToSignature(); }); m_mapExceptFile["vcredist_x86_2015.exe"] = "vcredist_x86_2015.exe"; m_mapExceptFile["vcredist_x86_2008.exe"] = "vcredist_x86_2008.exe"; m_mapExceptFile["vcredist_x64_2015.exe"] = "vcredist_x64_2015.exe"; m_mapExceptFile["vcredist_x64_2008.exe"] = "vcredist_x64_2008.exe"; m_mapExceptFile["JITComVCTK_S.exe"] = "JITComVCTK_S.exe"; m_mapExceptFile["7za.exe"] = "7za.exe"; m_mapExceptFile["openssl.exe"] = "openssl.exe"; m_mapExceptFile["iDentifyCmd.exe"] = "iDentifyCmd.exe"; m_mapExceptFile["WinPcap_4_1_3.exe"] = "WinPcap_4_1_3.exe"; m_mapExceptFile["unins000.exe"] = "unins000.exe"; //curl_global_init(CURL_GLOBAL_ALL); ui.widgetProgress->hide(); connect(&m_UploadFileThread, &UploadFileThread::signalProgress, this, [=](int progrss) { ui.progressBar->setValue(progrss); }, Qt::QueuedConnection); connect(&m_UploadFileThread, &UploadFileThread::finished, this, [=]() { QMessageBox::information(NULL, "提示", QString("已完成%1程序文件的数字签名!").arg(m_mapInfo.size()), QMessageBox::Ok); ui.widgetProgress->hide(); ui.pBUpload->setDisabled(false); ui.pushCopyFile->setDisabled(false); //ui.pushReturn->setDisabled(false); ui.pBSouuce->setDisabled(false); //ui.pBSearch->setDisabled(false); ui.pBTarget->setDisabled(false); }, Qt::QueuedConnection); QCheckBox *checkBox = new QCheckBox(ui.tableWidget); checkBox->resize(40, 24); ui.tableWidget->setCellWidget(0, 0, checkBox); checkBox->setStyleSheet("margin-left:4px;"); checkBox->setChecked(false); ui.tableWidget->setColumnWidth(0, 24); ui.tableWidget->setColumnWidth(1, 40); connect(checkBox, &QCheckBox::clicked, this, &signature::SelectAllFiles); } signature::~signature() { //curl_global_cleanup(); } //获取文件夹路径 QString signature::SlotGetPath() { MultiFileDirDialog fileDialog(NULL); fileDialog.setOption(QFileDialog::ShowDirsOnly, true); fileDialog.setWindowTitle("选择文件夹"); //m_pMultiFileDialog->setFileMode(QFileDialog::Directory); if (fileDialog.exec() == QDialog::Accepted) { QStringList selectFiles = fileDialog.selectedFiles(); if (selectFiles.size() > 0) { return selectFiles[0]; } } return ""; } /*查询源文件夹exe文件*/ void signature::StartSearchFile() { QString strInputPath = ui.lineEditInputPath->text(); if (strInputPath=="") { return; } QStringList listFile; RecursiveInputDirFiles(strInputPath, listFile); if (listFile.size()==0) { return; } while (ui.tableWidget->rowCount() > 0) { ui.tableWidget->removeRow(0); } QTableWidgetItem* pItem = NULL; for (int i = 0; i < listFile.size(); i++) { QTableWidgetItem *check = new QTableWidgetItem(); check->setCheckState(Qt::Unchecked); //是否设置选中框 ui.tableWidget->insertRow(i); ui.tableWidget->setItem(i, 0, check); //插入复选框 pItem = new QTableWidgetItem(); pItem->setText(QString::number(i+1)); ui.tableWidget->setItem(i, 1, pItem); pItem = new QTableWidgetItem(); pItem->setText(listFile[i]); ui.tableWidget->setItem(i,2, pItem); } } /*复制源文件夹exe到目标文件夹下*/ void signature::StartCopyFile() { if (m_strTargetPath=="") { QMessageBox::warning(NULL, "提示", "目标文件夹为空,请选择目标文件夹", QMessageBox::Ok); return; } GetAllSelectFile(); if (m_mapInfo.size()==0) { QMessageBox::warning(NULL, "提示", "请选择要复制的文件", QMessageBox::Ok); return; } RemoveAllFile(m_strTargetPath); ui.textEdit->setText(""); QString strExeName = ""; for (auto it= m_mapInfo.begin();it!=m_mapInfo.end();it++) { QString strTargetPath=""; QString strPath = it.key(); QFileInfo file(strPath); strTargetPath = m_strTargetPath + "/" + file.fileName(); QFile::copy(strPath, strTargetPath);//从源路径将文件复制到目标路径 strExeName += file.fileName(); strExeName += "\n"; } ui.textEdit->setText(strExeName); } /*开始上传文件进行签名*/ void signature::StartUploadFileToSignature() { GetAllSelectFile(); if (m_mapInfo.size()==0) { QMessageBox::warning(NULL, "提示","未选中的需要上传的exe文件!",QMessageBox::Ok); return; } ui.progressBar->setValue(0); ui.widgetProgress->show(); m_UploadFileThread.SetFiles(m_mapInfo); ui.pBUpload->setDisabled(true); ui.pushCopyFile->setDisabled(true); //ui.pushReturn->setDisabled(true); ui.pBSouuce->setDisabled(true); //ui.pBSearch->setDisabled(true); ui.pBTarget->setDisabled(true); } //选择或这全不选 void signature::SelectAllFiles(bool isChecked) { int count = ui.tableWidget->rowCount(); QTableWidgetItem* pItem = NULL; QString strExeName = ""; for (int i = 0; i < count; i++) { pItem = ui.tableWidget->item(i, 0); if (pItem == NULL) { continue; } pItem->setCheckState(isChecked? Qt::Checked:Qt::Unchecked); } } //查找所有勾选的文件 void signature::GetAllSelectFile() { m_mapInfo.clear(); int count = ui.tableWidget->rowCount(); QTableWidgetItem* pItem = NULL; QString strExeName = ""; for (int i = 0; i < count; i++) { pItem = ui.tableWidget->item(i, 0); if (pItem == NULL) { continue; } if (pItem->checkState() != Qt::Checked) { continue; } pItem = ui.tableWidget->item(i, 2); QString strTargetPath = ""; if (pItem != NULL) { QString strPath = pItem->text(); QFileInfo file(strPath); m_mapInfo.insert(strPath, file.fileName()); } } } /*递归遍历文件夹下面的子文件夹,查找exe程序*/ void signature::RecursiveInputDirFiles(QString strDir, QStringList& ExeFiles) { QDir dirFile(strDir); QDir dirDir(strDir); if (!dirDir.exists()) { return; } dirFile.setSorting(QDir::Time);//优先显示目录 dirDir.setSorting(QDir::Time); //列出dir(path)目录文件下所有文件和目录信息,存储到file_list容器 QStringList fileNameFilter; //视频格式 fileNameFilter << "*.exe"; dirFile.setNameFilters(fileNameFilter); QFileInfoList file_list = dirFile.entryInfoList(QDir::Files );//| QDir::Hidden | QDir::NoSymLinks //列出dir(path)目录下所有子文件夹 QFileInfoList folder_list = dirDir.entryInfoList(QDir::Dirs | QDir::Hidden | QDir::NoDotAndDotDot); if (file_list.size() == 0 && folder_list.size() == 0) { return; } //添加视频文件到列表 QString strfilepath = ""; for (int i = 0; i < file_list.size(); i++) { strfilepath = file_list[i].absoluteFilePath(); QFileInfo file(strfilepath); QString strFileName = file.fileName(); if (m_mapExceptFile.find(strFileName) != m_mapExceptFile.end()) { continue; } ExeFiles.append(strfilepath); } //进行子文件夹folder_list递归遍历,将内容存入file_list容器 for (int i = 0; i != folder_list.size(); i++) { RecursiveInputDirFiles(folder_list.at(i).absoluteFilePath(), ExeFiles); } } /*先清空目标文件夹下的exe文件*/ void signature::RemoveAllFile(QString strPath) { QDir dirFile(strPath); QFileInfoList file_list = dirFile.entryInfoList(QDir::Files);//| QDir::Hidden | QDir::NoSymLinks //列出dir(path)目录下所有子文件夹 //添加视频文件到列表 QString strfilepath = ""; for (int i = 0; i < file_list.size(); i++) { strfilepath = file_list[i].absoluteFilePath(); QFile::remove(strfilepath); } } /*用目标文件夹下的签名文件反向替换源文件夹下的未签名文件*/ void signature::CopyFileToSourcePath() { for (auto it=m_mapInfo.begin();it!=m_mapInfo.end();it++) { QString strTartget = m_strTargetPath + "/"+it.value(); QString strSource = it.key(); QFile::remove(strSource); QFile::copy(strTartget, strSource);//从源路径将文件复制到目标路径 } }
标签:pItem,exe,QtableWidget,表头,复选框,tableWidget,ui,QString,signature From: https://www.cnblogs.com/bclshuai/p/18095232