• 560阅读
  • 2回复

demo源码管理器 [复制链接]

上一主题 下一主题
离线xiaoke123
 

只看楼主 倒序阅读 楼主  发表于: 08-12
— 本帖被 20091001753 从 Qt 作品展 移动到本区(2020-08-12) —
实现功能:
1.支持动态更新,只需拷贝到程序根目录即可,删除添加都及时更新到界面
2.支持文件详细信息读取
3.支持显示zip、png、jpg、gif等文件显示
4.支持双击打开列表文件或目录
5.支持文件快速搜索
显示

搜索




相关实现代码,如下:
  1. void Widget::initTableData()
  2. {
  3.     //ui->tableWidget->clear();      // 连表头清掉
  4.     ui->tableWidget->clearContents();// 只清理数据
  5.     QString appDir = QCoreApplication::applicationDirPath()+"/examples";
  6.     qDebug() << "app dir: " << appDir;
  7.     QStringList Folderlist;
  8.     GetAllFileFolder(appDir,Folderlist);
  9.     int rowCount = Folderlist.count();
  10.     ui->tableWidget->setRowCount(rowCount);
  11.     for (int i = 0; i < rowCount; i++)
  12.     {
  13.         qDebug() << "FileFolder Path ----------- ="<< Folderlist.at(i);
  14.         QStringList Filelist;
  15.         countFile(Folderlist.at(i),Filelist);
  16.         int first = Folderlist.at(i).lastIndexOf ("/");
  17.         QString FolderName = Folderlist.at(i).right (Folderlist.at(i).length ()-first-1);
  18.         qDebug() << "FolderName ----------- ="<< FolderName;
  19.         ui->tableWidget->setRowHeight(i, 150);
  20.         // 默认以遍历的文件主
  21.         QString file,desc,pic,gif;
  22.         foreach (QString filePath , Filelist)
  23.         {
  24.             if(filePath.contains(".zip")    || filePath.contains(".7z") ||
  25.                filePath.contains(".tar.xz") || filePath.contains(".tar.gz"))
  26.                 file = filePath;
  27.             if(filePath.contains(".info")|| filePath.contains(".txt"))
  28.                 desc = filePath;
  29.             if(filePath.contains(".png") || filePath.contains(".jpg"))
  30.                 pic = filePath;
  31.             if(filePath.contains(".gif") )
  32.                 gif = filePath;
  33.         }
  34.         // 没有源码文件 就显示目录
  35.         if(file.isEmpty())
  36.             file = Folderlist.at(i)+"/";
  37.         //ReadInfo(file);
  38.         QTableWidgetItem *itemID = new QTableWidgetItem(QString::number(i + 1));
  39.         QTableWidgetItem *itemName = new QTableWidgetItem(FolderName);
  40.         QTableWidgetItem *itemFile = new QTableWidgetItem(file);
  41.         QTableWidgetItem *itemTime = new QTableWidgetItem(QFileInfo(file).created().toString("创建时间 yyyy-MM-dd hh:mm:ss"));//QTableWidgetItem *itemTime = new QTableWidgetItem(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
  42.         QTableWidgetItem *itemSize = new QTableWidgetItem(QString("%1 Bytes").arg(QFileInfo(file).size()));
  43.         QTableWidgetItem *itemDesc = new QTableWidgetItem(ReadDesc(desc));
  44.         itemID->setTextAlignment(Qt::AlignCenter);
  45.         itemName->setTextAlignment(Qt::AlignCenter);
  46.         itemFile->setTextAlignment(Qt::AlignCenter);
  47.         itemSize->setTextAlignment(Qt::AlignCenter);
  48.         itemTime->setTextAlignment(Qt::AlignCenter);
  49.         itemDesc->setTextAlignment(Qt::AlignCenter);
  50.         ui->tableWidget->setItem(i, 0, itemID);
  51.         ui->tableWidget->setItem(i, 1, itemName);
  52.         ui->tableWidget->setItem(i, 2, itemFile);
  53.         ui->tableWidget->setItem(i, 3, itemSize);
  54.         ui->tableWidget->setItem(i, 4, itemTime);
  55.         ui->tableWidget->setItem(i, 5, itemDesc);
  56.         // 方法二
  57.         //{
  58.         //    QLabel* labdesc = new QLabel(ReadDesc(desc));
  59.         //    labdesc->setProperty("flag", desc);
  60.         //    labdesc->setAlignment(Qt::AlignCenter);
  61.         //    ui->tableWidget->setCellWidget(i, 4, labdesc);
  62.         //}
  63.         {   // 缩略图 先判断默认的 没有再判断同名是否存在
  64.             if(!fileIsExist(pic))
  65.             {
  66.                 if(fileIsExist(QString("%1/%2%3").arg(Folderlist.at(i)).arg(FolderName).arg(".png")))
  67.                     pic = QString("%1/%2%3").arg(Folderlist.at(i)).arg(FolderName).arg(".png");
  68.                 else if(fileIsExist(QString("%1/%2%3").arg(Folderlist.at(i)).arg(FolderName).arg(".jpg")))
  69.                     pic = QString("%1/%2%3").arg(Folderlist.at(i)).arg(FolderName).arg(".jpg");
  70.                 else
  71.                     pic = "无相关图片!";
  72.             }
  73.             if(pic != "无相关图片!")
  74.             {
  75.                 QLabel* labpng = new QLabel;
  76.                 QPixmap *pixmap = new QPixmap(pic);
  77.                 pixmap->scaled(labpng->size(), Qt::KeepAspectRatio);
  78.                 labpng->setMargin(3);
  79.                 labpng->setScaledContents(true);
  80.                 labpng->setPixmap(*pixmap);
  81.                 labpng->setProperty("flag", pic);
  82.                 ui->tableWidget->setCellWidget(i, 6, labpng);
  83.             }else
  84.             {
  85.                 QLabel* labpng = new QLabel(pic);
  86.                 labpng->setProperty("flag", pic);
  87.                 labpng->setAlignment(Qt::AlignCenter);
  88.                 ui->tableWidget->setCellWidget(i, 6, labpng);
  89.             }
  90.         }
  91.         {   // 动图 先判断默认的 没有再判断同名是否存在
  92.             if(!fileIsExist(gif))
  93.             {
  94.                 if(fileIsExist(QString("%1/%2%3").arg(Folderlist.at(i)).arg(FolderName).arg(".gif")))
  95.                     gif = QString("%1/%2%3").arg(Folderlist.at(i)).arg(FolderName).arg(".gif");
  96.                 else
  97.                     gif = "无相关动图!";
  98.             }
  99.             if(gif != "无相关动图!")
  100.             {
  101.                 QLabel* emotionIcon = new QLabel;
  102.                 QMovie* movie = new QMovie;
  103.                 movie->setScaledSize(QSize(180,150));
  104.                 movie->setFileName(gif);
  105.                 movie->start();
  106.                 emotionIcon->setMargin(3);
  107.                 emotionIcon->setMovie(movie);
  108.                 emotionIcon->setProperty("flag", gif);
  109.                 //
  110.                 ui->tableWidget->setCellWidget(i, 7, emotionIcon);
  111.             }else
  112.             {
  113.                 QLabel* emotionIcon = new QLabel(gif);
  114.                 emotionIcon->setProperty("flag", gif);
  115.                 emotionIcon->setAlignment(Qt::AlignCenter);
  116.                 ui->tableWidget->setCellWidget(i, 7, emotionIcon);
  117.             }
  118.         }
  119.     }
  120. }
  121. QFileInfo Widget::ReadInfo(const QString &fileName)
  122. {
  123.     QFileInfo info(fileName);
  124.     QString infoList;
  125.     if (info.exists())
  126.     {
  127.         qDebug() << QString("文件名称: %1 \n").arg(info.fileName());
  128.         qDebug() << QString("文件大小: %1 Bytes\n").arg(info.size());
  129.         qDebug() << QString("创建时间: %1 \n").arg(info.created().toString("yyyy-MM-dd hh:mm:ss"));
  130.         qDebug() << QString("修改时间: %1 \n").arg(info.lastModified().toString("yyyy-MM-dd hh:mm:ss"));
  131.         qDebug() << QString("读取时间: %1 \n").arg(info.lastRead().toString("yyyy-MM-dd hh:mm:ss"));
  132.         infoList += tr("文件名称: %1 \n").arg(info.fileName());
  133.         infoList += tr("文件大小: %1 Bytes\n").arg(info.size());
  134.         infoList += tr("创建时间: %1 \n").arg(info.created().toString("yyyy-MM-dd hh:mm:ss"));
  135.         infoList += tr("修改时间: %1 \n").arg(info.lastModified().toString("yyyy-MM-dd hh:mm:ss"));
  136.         infoList += tr("读取时间: %1 \n").arg(info.lastRead().toString("yyyy-MM-dd hh:mm:ss"));
  137.     }
  138.     return info;
  139. }
  140. QString Widget::ReadDesc(const QString &filePath)
  141. {
  142.     QString strAll;
  143.     QFile file(filePath);
  144.     if (!file.open(QIODevice::ReadOnly|QIODevice::Text))//打开指定文件
  145.     {
  146.         //QMessageBox::about(NULL, "文件", "文件打开失败");
  147.         qDebug() << "文件打开失败! ----------- ="<< filePath;
  148.         strAll = "无相关描述!";
  149.         return strAll;
  150.     }
  151.     QTextCodec *codec = QTextCodec::codecForName("UTF-8");
  152.     strAll=codec->toUnicode(file.readAll());
  153.     file.close();
  154.     return strAll;
  155. }
  156. bool Widget::fileIsExist(const QString &strFile)
  157. {
  158.     QFile tempFile(strFile);
  159.     return tempFile.exists();
  160. }
  161. bool Widget::checkFile(const QString &fileName)
  162. {
  163.     if (fileName.startsWith("moc_") || fileName.startsWith("ui_") || fileName.startsWith("qrc_"))
  164.     {
  165.         return false;
  166.     }
  167.     QFileInfo file(fileName);
  168.     QString suffix = "*." + file.suffix();
  169.     QString filter = "*.zip *.7z *.gz *.xz *.txt *.info *.png *.jpg *.gif";//ui->txtFilter->text().trimmed();
  170.     QStringList filters = filter.split(" ");
  171.     return filters.contains(suffix);
  172. }
  173. void Widget::countFile(const QString &filePath, QStringList &files)
  174. {
  175.     QDir dir(filePath);
  176.     foreach (QFileInfo fileInfo , dir.entryInfoList())
  177.     {
  178.         if (fileInfo.isFile())
  179.         {
  180.             QString strFileName = fileInfo.fileName();
  181.             if (checkFile(strFileName))
  182.             {
  183.                 files << fileInfo.filePath();
  184.                 qDebug()<<"fileInfo.filePath ----------- ="<< fileInfo.filePath();
  185.             }
  186.         } else
  187.         {
  188.             if(fileInfo.fileName() == "." || fileInfo.fileName() == "..")
  189.             {
  190.                 continue;
  191.             }
  192.             // 递归找出文件
  193.             countFile(fileInfo.absoluteFilePath(),files);
  194.         }
  195.     }
  196. }
  197. void Widget::GetAllFileFolder(const QString &dirPath, QStringList &folders)
  198. {
  199.     QDir dir(dirPath);
  200.     dir.setFilter(QDir::Dirs);
  201.     foreach(QFileInfo fullDir, dir.entryInfoList())
  202.     {
  203.         if(fullDir.fileName() == "." || fullDir.fileName() == "..") continue;
  204.         folders.push_back(fullDir.absoluteFilePath());
  205.         this->GetAllFileFolder(fullDir.absoluteFilePath(),  folders);
  206.     }
  207. }
  208. void Widget::cellDoubleClicked(int x, int y)
  209. {
  210.     qDebug() << QStringLiteral("cellClicked row=%1  column=%2-------------------- item ").arg(x).arg(y);
  211.     if(y == 1 || y == 2 || y == 4)
  212.     {
  213.         QString strfolder = ui->tableWidget->item(x,2)->text();
  214.         qDebug() << "open folder: "<<  strfolder.left(strfolder.lastIndexOf ("/"));
  215.         QDesktopServices::openUrl(QUrl(QString("file:///%1").arg(strfolder.left(strfolder.lastIndexOf ("/")))));
  216.     }
  217.     if(y == 5 || y == 6)
  218.     {
  219.         QWidget *cellWidget = ui->tableWidget->cellWidget(x, y);
  220.         qDebug() << cellWidget;
  221.         if(cellWidget)
  222.         {
  223.             // 利用flag 属性 获取内容
  224.             QLabel *Label = (QLabel *)cellWidget;
  225.             if(Label)
  226.             {
  227.                 qDebug() << "open file: " <<Label->property("flag").toString();
  228.                 QDesktopServices::openUrl(QUrl(QString("file:///%1").arg(Label->property("flag").toString())));
  229.             }
  230.         }
  231.     }
  232. }












4条评分好评度+1贡献值+1金钱+10威望+1
20091001753 好评度 +1 - 08-12
20091001753 贡献值 +1 - 08-12
20091001753 威望 +1 - 08-12
20091001753 金钱 +10 - 08-12
离线nigoole

只看该作者 1楼 发表于: 08-13
放个完整的工程出来吧!感觉挺不错的!
有句话说得好:好好学习,天天向上。加油~~!有上船的朋友联系企鹅393320854
离线w642833823

只看该作者 2楼 发表于: 08-14
求看完整工程。
快速回复
限100 字节
 
上一个 下一个