一个桌面管理小工具-DesktopMgr-

版权声明:版权声明:转载请说明出处! https://blog.csdn.net/Feeling_C/article/details/83934858

1.小工具界面

界面有些丑,但是好用。
下载 链接:https://download.csdn.net/download/feeling_c/10777151

小工具界面

2.用于打开EXE/路径/网址

添加EXE

3.主要知识点

1. 数据库:采用Sqlite 存储 数据;

2.MVC: 视图采用GridView,Model 采用QList<QObject*>;

3.Exe 图标提取;

点击保存之后:如果是exe 则进行图标提取保存;

在这里插入图片描述
如果是Path 和Link 采用默认图片;
在这里插入图片描述
在这里插入图片描述

4.代码

数据库操作:

创建操作:
void Register::initDB(){
    QString path=QDir::currentPath()+"/JHData.db";

    sql.connectSqlite(path,"CONNECTQT");

    if(sql.openDB()){
        QString createcmd="mainModel(id INTEGER PRIMARY KEY AUTOINCREMENT,path TEXT,label VARCHAR(255),type INTEGER,img VARCHAR(255),state INTEGER,state1 TEXT)";
        if(sql.createTable(createcmd)){
            qDebug()<<"Create Finished!";
        }else{
            qDebug()<<"Create Failed!";
        }
    }
}
保存操作:
bool Register::saveToDB(QString path,QString label,int type,QString img){
    if(sql.openDB()){
        QString cmd="INSERT INTO mainModel(path,label,type,img) VALUES(";
        cmd+="\""+path+"\",";
        cmd+="\""+label+"\",";
        cmd+=QString::number(type)+",";


        if(type==1){
            img=QDir::currentPath()+"/png/"+QDateTime::currentDateTime().toString("yjh_yyyyMMddhhmmsszzz.png");
            QDir dir(QDir::currentPath()+"/png/");


            if(!dir.exists()){
                dir.mkpath(QDir::currentPath()+"/png/");
            }

            bool ret=FunLib::extractIcons(path,img);

            if(ret){
                qDebug()<<"提取成功!";
            }else{
                img="";
                qDebug()<<"提取失败!";
            }
        }

        cmd+="\""+img+"\")";

        return sql.execCmd(cmd);
    }

    return false;
}

更新操作:
bool Register::updateToDB(int id,int type,QString path,QString label){
    if(sql.openDB()){
        QString cmd="UPDATE mainModel SET path=";//
        cmd+="\""+path+"\",label=";

        cmd+="\""+label+"\"";

        QString img="";
        if(type==1){
            QString name=QFileInfo(path).fileName();
            img=QDir::currentPath()+"/png/"+name+".png";
            QDir dir(QDir::currentPath()+"/png/");

            if(!dir.exists()){
                dir.mkpath(QDir::currentPath()+"/png/");
            }

            bool ret=FunLib::extractIcons(path,img);

            if(ret){
                qDebug()<<"提取成功!";
            }else{
                img="";
                qDebug()<<"提取失败!";
            }

            cmd+=",img=\""+img+"\" ";
        }

        cmd+=" where id="+QString::number(id);

        qDebug()<<cmd;
        return sql.execCmd(cmd);
    }
    return false;
}
删除操作
bool Register::deleteToDB(int id,int index)
{
    if(sql.openDB()){
        QString cmd="DELETE FROM mainModel WHERE id=";//
        cmd+=QString::number(id);
        qDebug()<<cmd;

        bool ret= sql.execCmd(cmd);

        return ret;
    }
    return false;
}

更新数据

void Register::updateModel(int t){
    QList<QObject*> temlist;
    temlist.append(exeImgModel);
    exeImgModel.clear();

    if(sql.openDB()){
        QSqlTableModel *model=sql.getTableData("mainModel");
        model->setFilter(QString("type=%1").arg(t));
        while(model->canFetchMore()){
            model->fetchMore();
        }
        model->select();
        int count=model->rowCount();

        for(int i=0;i<count;i++){
            exeImgModel.append(new Models(model->record(i).value("id").toInt(),
                                          model->record(i).value("path").toString(),
                                          model->record(i).value("label").toString(),
                                          model->record(i).value("type").toInt(),
                                          model->record(i).value("img").toString()
                                          ));
        }

        ctx->setContextProperty("MainModel",QVariant::fromValue(exeImgModel));
    }
    qDeleteAll(temlist.constBegin(),temlist.constEnd());
}
启动应用:
bool FunLib::startApp(QString path,int ms)
{
    pro=new QProcess(this);
    if(path.contains(" ")){
        path.prepend("\"");
        path.append("\"");
    }
    pro->start(path);
    if(pro->waitForStarted(ms)){
        return true;
    }else{
        return false;
    }
}
打开文件夹:
bool FunLib::startDir(QString path){
    QDesktopServices::openUrl(QUrl(QString("file:///%1").arg(path),QUrl::TolerantMode));
}
打开链接:
bool FunLib::startUrl(QString path)
{
    QDesktopServices::openUrl(QUrl(path,QUrl::TolerantMode));
}
bool  FunLib::extractIcons(const QString &sourceFile,QString &path)
{
      const QString nativeName = QDir::toNativeSeparators(sourceFile);
      const wchar_t *sourceFileC = reinterpret_cast<const wchar_t *>(nativeName.utf16());
      const UINT iconCount = ExtractIconEx(sourceFileC, -1, 0, 0, 0);
      if (!iconCount) {
          return false;
      }

      QScopedArrayPointer<HICON> icons(new HICON[iconCount]);
      const UINT extractedIconCount =ExtractIconEx(sourceFileC, 0, icons.data(), 0, iconCount);

      if (!extractedIconCount) {
          return  false;
      }

      for (UINT i = 0; i < extractedIconCount; ++i) {
          QPixmap entry;
          entry= QtWin::fromHICON(icons[i]);
          if (entry.isNull()) {

          }else{
              entry.save(path,"png",100);
              return true;
          }
      }
      return false;
}

本人初学者,欢迎各位指点 相互学习。

猜你喜欢

转载自blog.csdn.net/Feeling_C/article/details/83934858