项目结构
pro 文件
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
cell_main.cpp
HEADERS += \
cell_main.h
FORMS += \
cell_main.ui
DESTDIR = $$PWD/bin
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
头文件
#ifndef CELL_MAIN_H
#define CELL_MAIN_H
#include <QMainWindow>
#include <QStandardItemModel>
QT_BEGIN_NAMESPACE
namespace Ui { class Cell_Main; }
QT_END_NAMESPACE
class Cell_Main : public QMainWindow
{
Q_OBJECT
public:
Cell_Main(QWidget *parent = nullptr);
~Cell_Main();
void updateFile();
private:
Ui::Cell_Main *ui;
QString m_strDataPath;
QStandardItemModel m_model;
};
#endif // CELL_MAIN_H
cell_main.cpp 文件
#include "cell_main.h"
#include "ui_cell_main.h"
#include <QDir>
#include <QtDebug>
#include <QDateTime>
Cell_Main::Cell_Main(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::Cell_Main)
{
ui->setupUi(this);
//获取当前应用所在目录 auto修饰变量
m_strDataPath= QApplication::applicationDirPath()+"/data";
QDir d(m_strDataPath);
if(!d.exists())
{
d.mkdir(m_strDataPath);//数据保存
}
updateFile();
}
Cell_Main::~Cell_Main()
{
delete ui;
}
void Cell_Main::updateFile()
{
QDir d(m_strDataPath);
QStringList lFiter;
lFiter <<"*.txt"<<"*.md";
QFileInfoList lFilesInfo = d.entryInfoList(lFiter,QDir::Files);
m_model.clear();
QList<QStandardItem*> lRow;
for(int i=0;i<lFilesInfo.size();i++)
{
lRow.clear();
QFileInfo &info=lFilesInfo[i];
//序号
QStandardItem *p1 = new QStandardItem(info.completeSuffix());
QStandardItem *p2 = new QStandardItem(info.absoluteFilePath());
QStandardItem *p3 = new QStandardItem(info.baseName());
QStandardItem *p4 = new QStandardItem(info.birthTime().toString("yyy/mm/dd hh:mm:ss"));
QStandardItem *p5 = new QStandardItem(info.lastModified().toString("yyy/mm/dd hh:mm:ss"));
QStandardItem *p6 = new QStandardItem(QString::number(info.size()/1024)+"KB");
lRow<<p1<<p2<<p3<<p4<<p5<<p6;
m_model.appendRow(lRow);
}
QStringList lHeaders{"文件类型","绝对路径","文件名","创建日期","最后一次修改","文件大小"};
ui->tableView->setModel(&m_model);
}
ui
http://t.csdnimg.cn/TQbfe