文章目录

一、获取系统的文件夹显示

UI设计

创建项目并进行简单的布局

Qt(八)文件夹与文件(文件读写)_开发语言

代码开发

mainwindow.h

#ifndef
#define

#include <QMainWindow>
#include <QFileSystemModel>
#include <QDir>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();

private slots:
void on_treeView_clicked(const QModelIndex &index);

private:
QFileSystemModel* fileModel;
Ui::MainWindow *ui;
};
#endif// MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);

fileModel = new QFileSystemModel(this);
fileModel->setRootPath(QDir::currentPath());
ui->treeView->setModel(fileModel);
ui->listView->setModel(fileModel);
ui->tableView->setModel(fileModel);

ui->tableView->verticalHeader()->setVisible(false);
connect(ui->treeView, SIGNAL(clicked(QModelIndex)), ui->listView, SLOT(setRootIndex(QModelIndex)));
connect(ui->treeView, SIGNAL(clicked(QModelIndex)), ui->tableView, SLOT(setRootIndex(QModelIndex)));

}

MainWindow::~MainWindow()
{
delete ui;
}


void MainWindow::on_treeView_clicked(const QModelIndex &index)
{
QString viewName = fileModel->fileName(index);
QString viewPath = fileModel->filePath(index);
QString viewType = fileModel->type(index);
unsigned viewSize = fileModel->size(index)/1024;
QString printSize;
if (viewSize>1024)
printSize = QString::asprintf("%.2f MB",(float)viewSize/1024);
else
printSize = QString::asprintf("%d KB", viewSize);
QString print_info = QString::fromUtf8("File_Info: %1 %2 %3 %4")
.arg(viewName)
.arg(viewPath)
.arg(viewType)
.arg(printSize);
ui->statusbar->showMessage(print_info);
}

演示效果

Qt(八)文件夹与文件(文件读写)_#include_02

二、文件读写

UI设计

Qt(八)文件夹与文件(文件读写)_ui设计_03


main.cpp

#include "mainwindow.h"

#include <QApplication>
#include <QTextCodec>

int main(int argc, char *argv[])
{
//解决汉字乱码问题
QTextCodec *codec = QTextCodec::codecForName("UTF-8");
QTextCodec::setCodecForLocale(codec);

QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}

mainwindow.h

#ifndef
#define

#include <QMainWindow>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();

private slots:
void on_actOpenFile_triggered();
void on_actSave_triggered();
void on_actOpenStream_triggered();
void on_actSaveStream_triggered();

private:
Ui::MainWindow *ui;
};
#endif// MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QTextStream>


MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
}


void MainWindow::on_actOpenFile_triggered()
{
QString curPath = QCoreApplication::applicationDirPath();
QString dilTile = "打开文件";
QString filter = "程序(*.h *.cpp);;文本(*.txt);;所有文件(*.*)";
QString filename = QFileDialog::getOpenFileName(this,dilTile,curPath,filter);

if(filename.isEmpty()) return;

QFile file(filename);
if(file.open(QIODevice::ReadOnly|QIODevice::Text)){
ui->plainTextEditIODivice->setPlainText(
QString::fromLocal8Bit(file.readAll()));
file.close();
ui->tabWidget->setCurrentIndex(0);
}
}

void MainWindow::on_actSave_triggered()
{
QString curPath = QCoreApplication::applicationDirPath();
QString dilTile = "保存文件";
QString filter = "程序(*.h *.cpp);;文本(*.txt);;所有文件(*.*)";
QString filename = QFileDialog::getSaveFileName(this,dilTile,curPath,filter);

if(filename.isEmpty()) return;

QFile file(filename);
if(file.open(QIODevice::WriteOnly|QIODevice::Text)){
QString str = ui->plainTextEditIODivice->toPlainText();
QByteArray strBytes = str.toUtf8();
file.write(strBytes,strBytes.length());
ui->tabWidget->setCurrentIndex(0);
}
}

void MainWindow::on_actOpenStream_triggered()
{
QString curPath = QCoreApplication::applicationDirPath();
QString dilTile = "打开文件";
QString filter = "程序(*.h *.cpp);;文本(*.txt);;所有文件(*.*)";
QString filename = QFileDialog::getOpenFileName(this,dilTile,curPath,filter);

if(filename.isEmpty()) return;

QFile file(filename);
if(file.open(QIODevice::ReadOnly|QIODevice::Text)){
QTextStream txtstream(&file);
txtstream.setAutoDetectUnicode(true);
while (!txtstream.atEnd()) {
ui->plainTextEditTextStream->appendPlainText(txtstream.readLine());
}
//ui->plainTextEditTextStream->setPlainText(txtstream.readAll());
file.close();
ui->tabWidget->setCurrentIndex(1);
}
}

void MainWindow::on_actSaveStream_triggered()
{
QString curPath = QCoreApplication::applicationDirPath();
QString dilTile = "保存文件";
QString filter = "程序(*.h *.cpp);;文本(*.txt);;所有文件(*.*)";
QString filename = QFileDialog::getSaveFileName(this,dilTile,curPath,filter);

if(filename.isEmpty()) return;

QFile file(filename);
if(file.open(QIODevice::WriteOnly|QIODevice::Text)){
QTextStream txtstream(&file);
txtstream.setAutoDetectUnicode(true);
QString str = ui->plainTextEditTextStream->toPlainText();

txtstream << str;
ui->tabWidget->setCurrentIndex(1);
}
}

演示效果

Qt(八)文件夹与文件(文件读写)_ui设计_04

三、文件目录

获取目录所有文件,双击打开文件

UI设计

Qt(八)文件夹与文件(文件读写)_qt_05


mainwindow.h

#ifndef
#define

#include <QMainWindow>
#include <QFileDialog>
#include <QDir>
#include <QFile>
#include <QIODevice>
#include <QStringList>
#include <QStringLiteral>
#include <QListWidgetItem>
#include <QMessageBox>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();

private slots:
void on_actOpenFile_triggered();

void on_actOpenDir_triggered();

void on_listWidget_itemDoubleClicked(QListWidgetItem *item);

void on_listWidget_itemClicked(QListWidgetItem *item);

private:
Ui::MainWindow *ui;
};
#endif// MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowTitle(QString::fromLocal8Bit("单击显示文件路径_双击打开文件"));
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_actOpenFile_triggered()
{
QString curPath = QCoreApplication::applicationDirPath();
QString dilTile = QString::fromLocal8Bit("打开文件");
QString filter = QString::fromLocal8Bit("程序(*.h *.cpp);;文本(*.txt);;所有文件(*.*)");
QStringList filenames = QFileDialog::getOpenFileNames(this,dilTile,curPath,filter);
ui->listWidget->addItems(filenames);

}

void MainWindow::on_actOpenDir_triggered()
{
QString file_path = QFileDialog::getExistingDirectory(this, QString::fromLocal8Bit("Select Folder"),QDir::currentPath());
if(file_path.isEmpty()) return;

QDir dir(file_path);
QStringList filters;
filters << "*.h" << "*.cpp";

//获取文件夹下所有文件名
//QStringList files = dir.entryList(filters, QDir::Files|QDir::Readable);
//ui->listWidget->addItems(files);

//获取文件夹下所有文件详情
QFileInfoList list = dir.entryInfoList(filters);
for(int i=0; i<list.length() ;i++){
QFileInfo tmpFileInfo = list.at(i);
if(tmpFileInfo.isFile()){
QString fileName = tmpFileInfo.filePath();
ui->listWidget->addItem(fileName);
}
}
}

void MainWindow::on_listWidget_itemDoubleClicked(QListWidgetItem *item)
{
if(item->text().isEmpty()) return;
QFile file(item->text());
if(file.open(QIODevice::ReadOnly|QIODevice::Text)){
ui->textEdit->setText(QString::fromLocal8Bit(file.readAll()));
file.close();
}
}


void MainWindow::on_listWidget_itemClicked(QListWidgetItem *item)
{
if(item->text().isEmpty()) return;
ui->statusbar->showMessage(item->text());
}

演示效果

Qt(八)文件夹与文件(文件读写)_ui_06