当前位置:网站首页>File browser? QT can also be achieved!
File browser? QT can also be achieved!
2022-07-24 16:24:00 【User 6557940】
introduction
Are familiar with Windows File browser under , You can open a disk or a folder with the mouse , Or a file in a subfolder , You can also return to the previous menu , You can also go to the specified directory according to the input .
Borrow here Qt To realize such a file browser , Implement the following functions :
- You can display the list of files in the specified directory ;
- Double click the folder in the file list , You can enter its subfolder , You can also return to the previous level .
Environmental Science :vs2008+Qt4.8.6+Qt The designer ,win7 System
01
preparation
Make a simple one UI Well , Here's the picture :
- QLineEdit: Used to input and display the current path ;
- QListWidget: Used to display the files and folders under the current path .
preservation ,OK!
02
Header work
My whole project is named FileBrowser, Class names are also called FileBrowser. The header file code is as follows :
#ifndef FILEBROWER_H
#define FILEBROWER_H
#include <QtGui/QWidget>
#include "ui_FileBrower.h"
/// Icon
#include <QIcon>
/// Catalog
#include <QDir>
class FileBrower : public QWidget
{
Q_OBJECT
public:
FileBrower(QWidget *parent = 0, Qt::WFlags flags = 0);
~FileBrower();
/// stay QListWidget The details under the current directory are displayed in
void showFileInfoList(QFileInfoList pInfoList);
/// Get the icon according to the nature of the file
///iType=1: Folder
///iType=2: file
QIcon *getItemPropertyIcon(int iType);
public slots:
/// Display the folders and files in the current directory
void showCurrentDirFiles();
/// Display the files under the folder in the list double clicked by the mouse
void showNextDirFiles(QListWidgetItem *item);
private:
Ui::FileBrowerClass ui;
};
#endif // FILEBROWER_HIt should be noted that :
1. Icon acquisition function :getItemPropertyIcon(int iType);
This is not necessary , Just to be able to distinguish folders and files more intuitively . I made two icons myself , Put it in the project directory , Later in CPP You can see how to use .
2.QListWidgetItem
This class can look at the official documents by itself , When to use , How to use it? . Quote the original :
The QListWidgetItem class provides an item for use with the QListWidget item view class.
03
Method realization
First of all, the signal slot connection must be carried out in the constructor :
connect(ui.lineEdit,SIGNAL(returnPressed()),this,SLOT(showCurrentDirFiles()));
connect(ui.listWidget,SIGNAL(itemDoubleClicked(QListWidgetItem*)),this,SLOT(showNextDirFiles(QListWidgetItem*)));the second connect There's nothing to say , It is the display function after double clicking the mouse . The key lies in the first , When QLineEdit Of returnPressed() The signal can be transmitted ? Here I searched the official documents :
This signal is emitted when the Return or Enter key is pressed. Note that if there is a validator() or inputMask() set on the line edit, the returnPressed() signal will only be emitted if the input follows the inputMask() and the validator() returns QValidator::Acceptable.
The document says that when the return key or enter key is pressed , This signal can be transmitted . There are a lot of complicated , I didn't go to study . in other words , When lineEdit Input in completed , After pressing enter , The signal will be transmitted , Then execute the slot function . In this case, the first connect There is no problem .
But in many blogs and many people are asking questions , The general question is as follows :
1. The connect The connection fails , The connected slot function cannot be triggered at all
Whether the connection is successful or not , You can verify that connect The return value of , Return on success true, Otherwise return to false;
2. tangle returnPressed() How to use it ?
For example, some blogs will connect like this
connect(ui.lineEdit, SIGNAL(returnPressed()), this, SLOT(showCurrentDirFiles(QDir)));I have also tried this connection , The return value is false, That is, the connection failed . Many foreign technical Q & A websites are also right returnPressed() There was a lot of discussion about . Avoid this in this method , Adopt the above connect, Verification is feasible . All roads lead to Rome , You don't have to pass parameters , Besides, , It is originally a member variable of a class , Accessible at any time .
04
Each method implements
void FileBrower::showNextDirFiles(QListWidgetItem *item)
{
/// Get the name of the file double clicked by the mouse
QString strName = item->text();
QDir dir;
// Set the path to the current directory path
dir.setPath(ui.lineEdit->text());
// Reset path
dir.cd(strName);
// Update current display path , And display all the files in the current directory
ui.lineEdit->setText(dir.absolutePath());
showCurrentDirFiles();
}
void FileBrower::showCurrentDirFiles()
{
// Get the currently entered directory
QDir currentDir(ui.lineEdit->text());
QStringList fileList;
fileList<<"*";
QFileInfoList infoList = currentDir.entryInfoList(fileList,QDir::AllEntries,QDir::DirsFirst);
// stay QListWidget A list of documents is displayed in
this->showFileInfoList(infoList);
}Here's a description QDir Methods entryInfoList(), This method returns a list containing all the files and folders in this directory . Check official documents , The method has three arguments :
QFileInfoList QDir::entryInfoList ( const QStringList & nameFilters, Filters filters = NoFilter, SortFlags sort = NoSort ) constThe second is filter , The official documents contain the following categories and their meanings :
Here we choose to show all . The third parameter is the sort order , By time 、 size 、 Names and so on , Instructions are also given in official documents :
void FileBrower::showFileInfoList(QFileInfoList pInfoList)
{
ui.listWidget->clear();
for(int i=0;i<pInfoList.size();i++)
{
QFileInfo tmpInfo = pInfoList.at(i);
QString pFileName = tmpInfo.fileName();
QListWidgetItem *tmpItem = new QListWidgetItem(pFileName);
if(tmpInfo.isDir())
tmpItem->setIcon(*getItemPropertyIcon(1));
else
tmpItem->setIcon(*getItemPropertyIcon(2));
ui.listWidget->addItem(tmpItem);
}
}
QIcon * FileBrower::getItemPropertyIcon(int iType)
{
QDir dir;
QString path = dir.currentPath();
switch(iType)
{
case 1:
return new QIcon(path+"/Folder.png");
break;
case 2:
return new QIcon(path+"/File.png");
break;
}
return NULL;
}05
test result
stay lineEdit Internal input D:/, Enter key , Pictured
double-click AutoCAD2015, Pictured :
Double click the second ( Two points ), You can return to the previous menu . stay lineEdit Internal input “/”, Is the root directory of the disk where the project is currently located , For example, I put it on the desktop , That will show C Folder under disk .
Source code address :https://github.com/FengJungle/Qt_Project I hope that's helpful !
边栏推荐
- "Heaven and the world, self-respect" -- single case mode
- Fast RCNN trains its own data set
- 栈与队列——1047. 删除字符串中的所有相邻重复项
- Best practices for preventing XSS in PHP web applications
- Scala functions and their higher-order applications
- Getting started with ARP
- [LeetCode]38.报数——题解(执行用时击败91% ,内存消耗击败 97%)
- 【LOJ3247】「USACO 2020.1 Platinum」Non-Decreasing Subsequences(DP,分治)
- .net review the old and know the new: [6] what is LINQ
- Yolov3 trains its own data set
猜你喜欢

Fast RCNN trains its own data set

如何防止跨站点脚本 (XSS) 攻击完整指南

After data management, the quality is still poor

Creation and inheritance of JS class

15、ARM嵌入式系统:如何用PC调试单板

Using native JS to realize magnifying glass function
![Programming in CoDeSys to realize serial communication [based on raspberry pie 4B]](/img/9c/05118efe2152dc5461a92720732076.jpg)
Programming in CoDeSys to realize serial communication [based on raspberry pie 4B]

How to generate complex flow chart of XMIND

Getting started with ARP

电话系统规则
随机推荐
2022 / 7 / 20 training record
Solution to deepin taskbar disappearance
安信证券开户在手机开户安全吗?
【LeetCode】Day103-搜索二维矩阵 II
Qt键盘事件(二)——长按按键反复触发event事件问题解决
Codeforces round 690 (Div. 3) C. unique number conventional solution
Introduction to bermudagrass
Mysql8 encountered the problem of stopping after the service was started
How to generate complex flow chart of XMIND
Please talk about the financial products with a yield of more than 6%
How to choose the appropriate data type for fields in MySQL?
ZBar project introduction and installation configuration| [email protected]
简易版QQ?Qt也可以实现!(一)
Code shoe set - mt2095 · zigzag jump
Can flush accounts be opened directly? Is it safe to open an account? How to open an account??
电话系统规则
253 Conference Room II
Traverse, delete, judge whether JSON data is empty, and recursion
Intel plans to sell baseband chip business, but Apple has given up?
Qualcomm reconciled with apple and received at least $4.5 billion in settlement fees! Next goal: Huawei!