欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Qt之QFileInfo

程序员文章站 2024-02-19 17:58:46
...
#include "dialog.h"
#include <QFileDialog>
#include <QDateTime>
Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    fileNameLabel = new QLabel(tr("文件名:"));
    fileNameLineEdit = new QLineEdit;
    fileBtn = new QPushButton(tr("文件"));

    sizeLabel = new QLabel(tr("大小:"));
    sizeLineEdit = new QLineEdit;

    createTimeLabel = new QLabel(tr("创建时间:"));
    createTimeLineEdit = new QLineEdit;

    lastModifiedLabel = new QLabel(tr("最后修改时间:"));
    lastModifiedLineEdit = new QLineEdit;

    lastReadLabel = new QLabel(tr("最后访问时间:"));
    lastReadLineEdit = new QLineEdit;

    propertyLabel = new QLabel(tr("属性:"));

    isDirCheckBox = new QCheckBox(tr("目录"));
    isFileCheckBox = new QCheckBox(tr("文件"));
    isSymLinkCheckBox = new QCheckBox(tr("符号连接"));
    isHiddenCheckBox = new QCheckBox(tr("隐藏"));
    isReadableCheckBox = new QCheckBox(tr("读"));
    isWritableCheckBox = new QCheckBox(tr("写"));
    isExecutableCheckBox = new QCheckBox(tr("执行"));

    getBtn = new QPushButton(tr("获得文件信息"));

    QGridLayout *gridLayout = new QGridLayout;
    gridLayout->addWidget(fileNameLabel,0,0);
    gridLayout->addWidget(fileNameLineEdit,0,1);
    gridLayout->addWidget(fileBtn,0,2);

    gridLayout->addWidget(sizeLabel,1,0);
    gridLayout->addWidget(sizeLineEdit,1,1,1,2);

    gridLayout->addWidget(createTimeLabel,2,0);
    gridLayout->addWidget(createTimeLineEdit,2,1,1,2);

    gridLayout->addWidget(lastModifiedLabel,3,0);
    gridLayout->addWidget(lastModifiedLineEdit,3,1,1,2);

    gridLayout->addWidget(lastReadLabel,4,0);
    gridLayout->addWidget(lastReadLineEdit,4,1,1,2);

    QHBoxLayout *layout2 = new QHBoxLayout;
    layout2->addWidget(propertyLabel);
    layout2->addStretch();

    QHBoxLayout *layout3 = new QHBoxLayout;
    layout3->addWidget(isDirCheckBox);
    layout3->addWidget(isFileCheckBox);
    layout3->addWidget(isSymLinkCheckBox);
    layout3->addWidget(isHiddenCheckBox);
    layout3->addWidget(isReadableCheckBox);
    layout3->addWidget(isWritableCheckBox);
    layout3->addWidget(isExecutableCheckBox);

    QHBoxLayout *layout4 = new QHBoxLayout;
    layout4->addWidget(getBtn);

    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->addLayout(gridLayout);
    mainLayout->addLayout(layout2);
    mainLayout->addLayout(layout3);
    mainLayout->addLayout(layout4);

    connect(fileBtn,SIGNAL(clicked()),this,SLOT(slotFile()));
    connect(getBtn,SIGNAL(clicked()),this,SLOT(slotGet()));
}

Dialog::~Dialog()
{

}

void Dialog::slotFile()
{
    QString fileName = QFileDialog::getOpenFileName(this,"打开","/","files (*)");
    fileNameLineEdit->setText(fileName);
}

void Dialog::slotGet()
{
    QString file = fileNameLineEdit->text();
    QFileInfo info(file);

    qint64 size = info.size();         //获取大小
    QDateTime created = info.created();//创建时间
    QDateTime lastModified = info.lastModified();//最后修改时间
    QDateTime lastRead = info.lastRead();//最后访问时间

    bool isDir = info.isDir();   //是否是目录
    bool isFile = info.isFile();  //是否是文件
    bool isSymLink = info.isSymLink(); //符合链接
    bool isHidden = info.isHidden();  //隐藏属性
    bool isReadable = info.isReadable(); //读属性
    bool isWritable = info.isWritable();  //写属性
    bool isExecutable = info.isExecutable(); //可执行属性

    sizeLineEdit->setText(QString::number(size));
    createTimeLineEdit->setText(created.toString());
    lastModifiedLineEdit->setText(lastModified.toString());
    lastReadLineEdit->setText(lastRead.toString());

    isDirCheckBox->setCheckState(isDir?Qt::Checked:Qt::Unchecked);
    isFileCheckBox->setCheckState(isFile?Qt::Checked:Qt::Unchecked);
    isSymLinkCheckBox->setCheckState(isSymLink?Qt::Checked:Qt::Unchecked);
    isHiddenCheckBox->setCheckState(isHidden?Qt::Checked:Qt::Unchecked);
    isReadableCheckBox->setCheckState(isReadable?Qt::Checked:Qt::Unchecked);
    isWritableCheckBox->setCheckState(isWritable?Qt::Checked:Qt::Unchecked);
    isExecutableCheckBox->setCheckState(isExecutable?Qt::Checked:Qt:: Unchecked);
}

Qt之QFileInfo

---