#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include "Windows.h"
namespace Ui {
class Widget;
}
class QStandardItemModel;
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private slots:
void on_btnOpen_clicked();
void on_btnReadTime_clicked();
void on_btnWriteTime_clicked();
void on_btnCreateTime_clicked();
void on_checkBox_clicked(bool checked);
private:
void buildTime(PFILETIME fileTime);
void buildModel(QStandardItemModel *model);
Ui::Widget *ui;
QString dir;
QStandardItemModel *model;
bool bRecur;
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
#include <QFileDialog>
#include <QStandardItemModel>
#include <QDateTime>
#include <QDebug>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
dir = ".";
bRecur = false;
model = new QStandardItemModel();
ui->treeView->setModel(model);
QStringList list;
list << "File" << "Create" << "Write" << "Access";
model->setHorizontalHeaderLabels(list);
QHeaderView *view = ui->treeView->header();
// QStandardItemModel* standardModel = new QStandardItemModel ;
// standardModel->setHorizontalHeaderItem(0, new QStandardItem("file"));
// standardModel->setHorizontalHeaderItem(1, new QStandardItem("create"));
// standardModel->setHorizontalHeaderItem(2, new QStandardItem("write"));
// standardModel->setHorizontalHeaderItem(3, new QStandardItem("read"));
view->setDefaultAlignment(Qt::AlignCenter);
view->setStretchLastSection(false);
for(int i=1;i<4; ++i)
ui->treeView->setColumnWidth(i, 140);
view->setSectionResizeMode(0, QHeaderView::Stretch);
view->setSectionResizeMode(1, QHeaderView::Fixed);
view->setSectionResizeMode(2, QHeaderView::Fixed);
view->setSectionResizeMode(3, QHeaderView::Fixed);
ui->treeView->setSelectionMode(QTreeView::ContiguousSelection);
ui->dateTimeEdit->setDateTime(QDateTime::currentDateTime());
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_btnOpen_clicked()
{
QString tmp = QFileDialog::getExistingDirectory(this, "选择目录", dir);
if(tmp.isNull())
return;
dir = tmp;
buildModel(model);
}
void Widget::on_btnReadTime_clicked()
{
FILETIME fileTime;
buildTime(&fileTime);
QModelIndexList list = ui->treeView->selectionModel()->selectedRows();
for(auto item: list)
{
HANDLE hFile = CreateFile( (dir+"/"+item.data().toString()).toStdWString().c_str(), GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if(hFile!=INVALID_HANDLE_VALUE)
{
SetFileTime(hFile, 0, &fileTime, 0);
qDebug() << item.data().toString() << GetLastError();
CloseHandle(hFile);
}
}
}
void Widget::on_btnWriteTime_clicked()
{
FILETIME fileTime;
buildTime(&fileTime);
QModelIndexList list = ui->treeView->selectionModel()->selectedRows();
for(auto item: list)
{
HANDLE hFile = CreateFile( (dir+"/"+item.data().toString()).toStdWString().c_str(), GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if(hFile!=INVALID_HANDLE_VALUE)
{
SetFileTime(hFile, 0, 0, &fileTime);
qDebug() << item.data().toString() << GetLastError();
CloseHandle(hFile);
}
}
}
void Widget::on_btnCreateTime_clicked()
{
FILETIME fileTime;
buildTime(&fileTime);
QModelIndexList list = ui->treeView->selectionModel()->selectedRows();
for(auto item: list)
{
HANDLE hFile = CreateFile( (dir+"/"+item.data().toString()).toStdWString().c_str(), GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if(hFile!=INVALID_HANDLE_VALUE)
{
SetFileTime(hFile, &fileTime, 0, 0);
qDebug() << item.data().toString() << GetLastError();
CloseHandle(hFile);
}
}
}
void Widget::buildModel(QStandardItemModel *model)
{
model->removeRows(0, model->rowCount());
QDir dir(dir);
QStandardItem *item= model->invisibleRootItem();
for(auto fileInfo: dir.entryInfoList(QDir::Files))
{
// QStandardItem *item = new QStandardItem;
QStandardItem *item1 = new QStandardItem(fileInfo.fileName());
QStandardItem *item2 = new QStandardItem(fileInfo.created().toString("yyyy-MM-dd HH:mm:ss"));
QStandardItem *item3 = new QStandardItem(fileInfo.lastModified().toString("yyyy-MM-dd HH:mm:ss"));
QStandardItem *item4 = new QStandardItem(fileInfo.lastRead().toString("yyyy-MM-dd HH:mm:ss"));
QList<QStandardItem*> args;
args << item1<< item2<< item3<< item4;
item->appendRow(args);
// model->appendRow(item);
}
}
void Widget::buildTime(PFILETIME fileTime)
{
QDateTime time = ui->dateTimeEdit->dateTime();
SYSTEMTIME sysTime;
sysTime.wDay = time.date().day();
sysTime.wYear = time.date().year();
sysTime.wMonth = time.date().month();
sysTime.wHour = time.time().hour();
sysTime.wMinute = time.time().minute();
sysTime.wSecond = time.time().second();
::SystemTimeToFileTime(&sysTime, fileTime);
}
void Widget::on_btnFresh_clicked()
{
QFont font = ui->treeView->font();
QFontMetrics fm(font);
QRect rec = fm.boundingRect("2099-99-99 88:88:88");
// qDebug() << font.pointSize() << rec.width();
buildModel(model);
}