赞
踩
QtClearProject.h
#ifndef QTCLEARPROJECT_H #define QTCLEARPROJECT_H #include <QFile> #include <QMenu> #include <QAction> #include <QDialog> #include <QCheckBox> #include <QHeaderView> #include <QHBoxLayout> #include <QTableWidget> #include <QCoreApplication> #include <QContextMenuEvent> class QtClearProject : public QDialog { Q_OBJECT private: QHBoxLayout* mainLayout; QTableWidget* tableProject; public: QtClearProject(QWidget *parent = nullptr); ~QtClearProject(); private: QString iniPath; QMenu *mainMenu; private slots: void SlotActionDelete(); void SlotActionClear(); void SlotActionSave(); void contextMenuEvent(QContextMenuEvent *event); private: void InitTableWidget(); }; #endif // QTCLEARPROJECT_H
QtClearProject.cpp
#include "QtClearProject.h" QtClearProject::QtClearProject(QWidget *parent) : QDialog(parent),iniPath("") { Qt::WindowFlags flags = Qt::Dialog; flags |= Qt::WindowCloseButtonHint; setWindowFlags(flags); setMinimumSize(640,480); mainLayout=new QHBoxLayout; tableProject=new QTableWidget; QStringList headerStr; headerStr<<QStringLiteral("")<<QStringLiteral("项目名称")<<QStringLiteral("项目地址"); tableProject->setColumnCount(3); tableProject->setHorizontalHeaderLabels(headerStr); tableProject->setColumnWidth(0, 20); tableProject->setColumnWidth(1, 150); tableProject->setColumnWidth(2, 400); tableProject->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Fixed); tableProject->verticalHeader()->hide(); mainLayout->setMargin(10); mainLayout->setSpacing(0); mainLayout->addWidget(tableProject); setLayout(mainLayout); //初始化表格 QFile file(QCoreApplication::applicationDirPath()+"/Setting.ini"); if(file.open(QIODevice::ReadOnly)){ iniPath=file.readAll(); file.close(); InitTableWidget(); } //创建菜单 QAction* actionDel=new QAction; QAction* actionClear=new QAction; QAction* actionSave=new QAction; actionDel->setText(QStringLiteral("删除")); actionClear->setText(QStringLiteral("清空")); actionSave->setText(QStringLiteral("保存")); mainMenu = new QMenu(); mainMenu->addAction(actionDel); mainMenu->addAction(actionClear); mainMenu->addSeparator(); mainMenu->addAction(actionSave); connect(actionDel,&QAction::triggered,this,&QtClearProject::SlotActionDelete); connect(actionClear,&QAction::triggered,this,&QtClearProject::SlotActionClear); connect(actionSave,&QAction::triggered,this,&QtClearProject::SlotActionSave); } QtClearProject::~QtClearProject() {} void QtClearProject::InitTableWidget() { if(iniPath.isEmpty()||iniPath.isEmpty()) return; QFile file(iniPath); if(file.exists()==false) return; QString pathsKey="RecentProjects\\FileNames"; QString namesKey="RecentProjects\\DisplayNames"; QString fileNames,filePaths; if(file.open(QIODevice::ReadOnly)){ while (!file.atEnd()){ QString line = file.readLine(); if(line.contains(namesKey)){ fileNames=line; } else if(line.contains(pathsKey)){ filePaths=line; } } file.close(); } if(fileNames.isEmpty() || filePaths.isEmpty()) return; int index=fileNames.indexOf(namesKey); fileNames=fileNames.right(fileNames.length()-namesKey.length()-index-1); index=filePaths.indexOf(pathsKey); filePaths=filePaths.right(filePaths.length()-pathsKey.length()-index-1); fileNames.replace("\r\n",""); filePaths.replace("\r\n",""); fileNames.replace(" ",""); filePaths.replace(" ",""); QStringList fileNameList=fileNames.split(','); QStringList filePathList=filePaths.split(','); for(auto &fileName:fileNameList){ QString filePath; for(auto &tmpPath:filePathList){ if(tmpPath.contains(fileName)){ filePath=tmpPath; break; } } if(filePath.isNull()||filePath.isEmpty()) continue; int nRowCount=tableProject->rowCount(); tableProject->insertRow(nRowCount); QCheckBox* check=new QCheckBox(tableProject); tableProject->setCellWidget(nRowCount,0,check); tableProject->setItem(nRowCount, 1, new QTableWidgetItem(fileName)); tableProject->setItem(nRowCount, 2, new QTableWidgetItem(filePath)); } } void QtClearProject::contextMenuEvent(QContextMenuEvent *event) { mainMenu->exec(QCursor::pos()); event->accept(); } void QtClearProject::SlotActionDelete() { int row=tableProject->rowCount(); QCheckBox* cbox=Q_NULLPTR; for(int i=row-1;i>=0;--i){ cbox=Q_NULLPTR; cbox=static_cast<QCheckBox*>(tableProject->cellWidget(i,0)); if(cbox==Q_NULLPTR){} else{ if(cbox->checkState()==Qt::Checked){ tableProject->removeRow(i); } } } } void QtClearProject::SlotActionClear() { int row=tableProject->rowCount(); for(int i=row-1;i>=0;--i){ tableProject->removeRow(i); } } void QtClearProject::SlotActionSave() { if(iniPath.isEmpty()||iniPath.isEmpty()) return; QFile fileSrc(iniPath); if(fileSrc.exists()==false) return; QFile fileDst(iniPath+"1"); QString pathsKey="RecentProjects\\FileNames"; QString namesKey="RecentProjects\\DisplayNames"; QString pathsContent=pathsKey.append("="); QString namesContent=namesKey.append("="); int row=tableProject->rowCount(); for(int i=0;i<row;++i){ namesContent.append(tableProject->item(i,1)->text()); pathsContent.append(tableProject->item(i,2)->text()); namesContent.append(","); pathsContent.append(","); } if(row>0){ namesContent=namesContent.left(namesContent.length()-1); pathsContent=pathsContent.left(pathsContent.length()-1); namesContent.append("\n"); pathsContent.append("\n"); } pathsKey=pathsKey.left(pathsKey.length()-1); namesKey=namesKey.left(namesKey.length()-1); if(fileSrc.open(QIODevice::ReadOnly)){ if(fileDst.open(QIODevice::WriteOnly|QIODevice::Text)){ while (!fileSrc.atEnd()){ QString line = fileSrc.readLine(); if(line.contains(namesKey)){ line=namesContent; } else if(line.contains(pathsKey)){ line=pathsContent; } fileDst.write(line.toLocal8Bit()); } fileDst.close(); } fileSrc.close(); } QFile::remove(iniPath); QFile::rename(iniPath+"1",iniPath); }
Setting.ini
C:\Users\XX\AppData\Roaming\QtProject\QtCreator.ini
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。