当前位置:网站首页>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 !
边栏推荐
- EMQ Yingyun technology was listed on the 2022 "cutting edge 100" list of Chinese entrepreneurs
- 栈与队列——1047. 删除字符串中的所有相邻重复项
- Dongfang Guangyi refuted the rumor late at night, saying that the news that the hammer was filed for investigation was untrue!
- How to generate complex flow chart of XMIND
- Power of leetcode 231.2
- Is it safe for Anxin securities to open an account on mobile phone?
- JUC源码学习笔记3——AQS等待队列和CyclicBarrier,BlockingQueue
- Disassembly of Samsung Galaxy fold: the interior is extremely complex. Is the hinge the main cause of screen damage?
- Code shoe set - mt2095 · zigzag jump
- [LeetCode]75.颜色分类——题解(执行用时击败90% ,内存消耗击败 78%)
猜你喜欢

Custom view - Custom button

How to prevent XSS in PHP

Parse string

Adaptive design and responsive design
![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]

普林斯顿微积分读本02第一章--函数的复合、奇偶函数、函数图像
[email protected]"/>ZBar project introduction and installation configuration| [email protected]

矩阵的秩和图像的秩的一些了解

Dedecms editor supports automatic pasting of word pictures

After data management, the quality is still poor
随机推荐
Creation and inheritance of JS class
Traverse, delete, judge whether JSON data is empty, and recursion
[LeetCode]巧用位运算
Jenkins cli command details
图像label处理——json文件转化为png文件
Yolov6 trains its own data set
More than 40 Qualcomm chips have been exposed to security vulnerabilities, affecting billions of mobile phones!
Telephone system rules
[loj3247] [USACO 2020.1 platinum "non declining subsequences (DP, divide and conquer)
“天上天下,唯我独尊”——单例模式
如何防止跨站点脚本 (XSS) 攻击完整指南
收益率在百分之六以上的理财产品,请说一下
聊聊C指针
C TCP client form application asynchronous receiving mode
Chapter 2 using API Mgmnt service
Servlet框架(servlet+jsp)+Mysql实现的增删改查+分页(功能包学生信息录入、学生信息增删改查、分页等)
Introduction to kettle messy notes
Programming in CoDeSys to realize serial communication [based on raspberry pie 4B]
Druid integration shardingsphere appears xxmapper Reasons and solutions of XML error reporting
MySQL converts strings to numeric types and sorts them