qt_文件运用,调用桌面

qt_文件运用,调用桌面

QFileSystemWatcher

函数 描述
bool addPath(const QString &path) 添加一个path
QStringList addPaths(const QStringList &paths) 添加一个路径
QStringList directories() const 监控目录的顺序
bool removePath(const QString &path) 删除
QStringList removePaths(const QStringList &paths)

sinals:

函数 描述
void directoryChanged(const QString &path) 目录改变
void fileChanged(const QString &path) 文件改变
/*
 * 文件监视QFileSystemWatcher
 * addpath  removepath
 * directoryChanged     fileChanged
 * 监视整个路径,那就可以前后做比较,找出哪个文件发生了什么,最主要是qset的魅力
 *
 *
*/

FileIo::FileIo(QWidget *parent)
    : QWidget(parent)
{

    QTextStream cout(stdout);
    setWindowTitle("file system");
    QFileSystemWatcher *pfile_watch=new QFileSystemWatcher;
    path_str="E:/";
    QDir dir(path_str);
    pfile_watch->addPath(path_str);
    currentDirSet=QSet<QString>::fromList(dir.entryList(QDir::Dirs|QDir::Files));
    connect(pfile_watch,SIGNAL(directoryChanged(QString)),this,SLOT(direchange(QString)));



}
void FileIo::direchange(QString path)
{
   QTextStream cout(stdout);
   QDir dir(path_str);
   QSet<QString> newDirSet=QSet<QString>::fromList(dir.entryList(QDir::Dirs|QDir::Files));

   QStringList newFile = (newDirSet - currentDirSet).toList();    // 添加了文件
   QStringList deleteFile = (currentDirSet - newDirSet).toList(); // 文件已被移除
   currentDirSet=newDirSet;                                        //更新储存的目录内容
    if (!newFile.isEmpty() && !deleteFile.isEmpty())
       {
           // 文件/目录重命名
           if ((newFile.count() == 1) && (deleteFile.count() == 1))
           {
               cout << QString("File Renamed from %1 to %2").arg(deleteFile.first()).arg(newFile.first())<<endl;
           }
       }
    else
       {
           // 添加新文件/目录至Dir
           if (!newFile.isEmpty())
           {
               cout << "New Files/Dirs added: " ;

               foreach (QString file, newFile)
               {
                   // 处理操作每个新文件....
                   cout<<file<<endl;
               }
           }

           // 从Dir中删除文件/目录
           if (!deleteFile.isEmpty())
           {
              cout << "Files/Dirs deleted: " ;

               foreach(QString file, deleteFile)
               {
                   // 处理操作每个被删除的文件....
                   cout<<file<<endl;
               }
           }
       }
}
//源码见上传资源_文件目录监视https://download.csdn.net/download/qq_33564134/10618873

QDesktopServices

函数 描述
bool openUrl(const QUrl &url) 打开一个url
void setUrlHandler(const QString &scheme, QObject *receiver, const char *method) 定制.但是不会…
void unsetUrlHandler(const QString &scheme)

QFileIconProvider

enum QFileIconProvider::IconType

Constant Value
QFileIconProvider::Computer 0
QFileIconProvider::Desktop 1
QFileIconProvider::Trashcan 2
QFileIconProvider::Network 3
QFileIconProvider::Drive 4
QFileIconProvider::Folder 5
QFileIconProvider::File 6

这里写图片描述

/*
 * 两个途径获取文件的icon
 * pfileico->icon((QFileIconProvider::IconType)1)
 * pfileico->icon(info)
*/

FileIo::FileIo(QWidget *parent)
    : QWidget(parent)
{

    setWindowTitle("file system");
    QFileInfo info("E:/1.txt");
    QListWidgetItem *pItem = new QListWidgetItem;
    QListWidget  *list_widget=new QListWidget(this);
    QFileIconProvider *pfileico=new QFileIconProvider;
    list_widget->setFixedSize(400,400);
    list_widget->setIconSize(QSize(200,200));
    pItem->setIcon(pfileico->icon((QFileIconProvider::IconType)1));
    pItem->setText("电脑");
    list_widget->addItem(pItem);
    pItem=new QListWidgetItem;
    pItem->setIcon(pfileico->icon(info));
    pItem->setText("文本文件");
    list_widget->addItem(pItem);

}

QCryptographicHash加密

QByteArray byteArray;
byteArray.append("password");
QByteArray hash = QCryptographicHash::hash(byteArray, QCryptographicHash::Md5);
QString strMD5 = hash.toHex();

猜你喜欢

转载自blog.csdn.net/qq_33564134/article/details/81912062