Qt之QFileInfo

#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);
}

---this

相關文章
相關標籤/搜索