当前位置:   article > 正文

QtCreator清理项目_qt 清理项目

qt 清理项目

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

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);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191

Setting.ini

C:\Users\XX\AppData\Roaming\QtProject\QtCreator.ini
  • 1
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/292657
推荐阅读
相关标签
  

闽ICP备14008679号