QT获取文件信息

简介:  //fileinformation.cpp include "fileinformation.h" # FileInformation::FileInformation( QWidget *parent, Qt::WindowFlags  f...

 //fileinformation.cpp

include "fileinformation.h"

#

FileInformation::FileInformation( QWidget *parent, Qt::WindowFlags  f )    : QDialog( parent, f ){      setWindowTitle(tr("File Information"));           LabelFileName = new QLabel();    LabelFileName->setText(tr("File Name:"));    LineEditFileName = new QLineEdit();       PushButtonFile = new QPushButton( this);    PushButtonFile->setText( tr( "File" ) );    LabelSize = new QLabel();    LabelSize->setText(tr("Size:"));    LineEditSize = new QLineEdit();      LabelCreated = new QLabel();    LabelCreated->setText(tr("Created:"));    LineEditCreated = new QLineEdit();        LabelLastModified = new QLabel();    LabelLastModified->setText(tr("Last Modified:"));    LineEditLastModified = new QLineEdit();        LabelLastRead = new QLabel();    LabelLastRead->setText(tr("Last Read:"));    LineEditLastRead = new QLineEdit();            LabelProperty = new QLabel();    LabelProperty->setText(tr("Property:"));    CheckBoxIsDir = new QCheckBox();    CheckBoxIsDir->setText(tr("Dir"));    CheckBoxIsFile = new QCheckBox();    CheckBoxIsFile->setText(tr("File"));    CheckBoxIsSymLink = new QCheckBox();    CheckBoxIsSymLink->setText(tr("SymLink"));    CheckBoxIsHidden = new QCheckBox();    CheckBoxIsHidden->setText(tr("Hidden"));    CheckBoxIsReadable = new QCheckBox();    CheckBoxIsReadable->setText(tr("Readable"));    CheckBoxIsWritable = new QCheckBox();    CheckBoxIsWritable->setText(tr("Writable"));    CheckBoxIsExecutable = new QCheckBox();    CheckBoxIsExecutable->setText(tr("Executable"));    PushButtonGet = new QPushButton( this);    PushButtonGet->setText( tr("Get" ));        QHBoxLayout *hbLayout1 = new QHBoxLayout ();    hbLayout1->addWidget(LabelFileName);    hbLayout1->addWidget(LineEditFileName);    hbLayout1->addWidget(PushButtonFile);        QHBoxLayout *hbLayout2 = new QHBoxLayout ();    hbLayout2->addWidget(LabelSize);    hbLayout2->addWidget(LineEditSize);    QHBoxLayout *hbLayout3 = new QHBoxLayout();    hbLayout3->addWidget(LabelCreated);    hbLayout3->addWidget(LineEditCreated);        QHBoxLayout *hbLayout4 = new QHBoxLayout();    hbLayout4->addWidget(LabelLastModified);    hbLayout4->addWidget(LineEditLastModified);        QHBoxLayout *hbLayout5 = new QHBoxLayout();    hbLayout5->addWidget(LabelLastRead);    hbLayout5->addWidget(LineEditLastRead);        QHBoxLayout *hbLayout6 = new QHBoxLayout();    hbLayout6->addWidget(LabelProperty);    hbLayout6->addStretch();                QHBoxLayout *hbLayout7 = new QHBoxLayout();    hbLayout7->addWidget(CheckBoxIsDir);    hbLayout7->addWidget(CheckBoxIsFile);    hbLayout7->addWidget(CheckBoxIsSymLink);    hbLayout7->addWidget(CheckBoxIsHidden);    hbLayout7->addWidget(CheckBoxIsReadable);    hbLayout7->addWidget(CheckBoxIsWritable);    hbLayout7->addWidget(CheckBoxIsExecutable);          QVBoxLayout *vbLayout = new QVBoxLayout( this );    vbLayout->addLayout( hbLayout1 );    vbLayout->addLayout( hbLayout2 );    vbLayout->addLayout( hbLayout3 );    vbLayout->addLayout( hbLayout4 );    vbLayout->addLayout( hbLayout5 );    vbLayout->addLayout( hbLayout6 );    vbLayout->addLayout( hbLayout7 );    vbLayout->addWidget( PushButtonGet );    connect(PushButtonFile,SIGNAL(clicked()),this,SLOT(slotFile()));    connect(PushButtonGet,SIGNAL(clicked()),this,SLOT(slotGet()));}FileInformation::~FileInformation(){}void FileInformation::slotFile(){    QString s = QFileDialog::getOpenFileName(   //获取文件路径 this, "open file dialog",        "/",        "files (*)");                                                    LineEditFileName->setText( s.toAscii().data());//文件路径}void FileInformation::slotGet(){    QFileInfo info(LineEditFileName->text());    //获取文件大小    //在不同的平台上,基本的C++类型如short,char,int,long,long long会有不同的字长。    //最好把它们转换为qint8,quint8,qint16,quint16,qint32,    //quint32,qint64,quint64,这些类型能确保字长是不随平台改变的。    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();    LineEditSize->setText (QString::number(size));    //显示文件时间信息    LineEditCreated->setText (created.toString());    LineEditLastModified->setText (lastModified.toString());    LineEditLastRead->setText (lastRead.toString());    //显示文件属性    CheckBoxIsDir->setCheckState (isDir?Qt::Checked:Qt::Unchecked);    CheckBoxIsFile->setCheckState (isFile?Qt::Checked:Qt::Unchecked);    CheckBoxIsSymLink->setCheckState (isSymLink?Qt::Checked:Qt::Unchecked);    CheckBoxIsHidden->setCheckState (isHidden?Qt::Checked:Qt::Unchecked);    CheckBoxIsReadable->setCheckState (isReadable?Qt::Checked:Qt::Unchecked);    CheckBoxIsWritable->setCheckState (isWritable?Qt::Checked:Qt::Unchecked);    CheckBoxIsExecutable->setCheckState (isExecutable?Qt::Checked:Qt::Unchecked);}----------------------------------------------------------------------------------------------

//fileinformation.h

#ifndef FILEINFORMATION_H

#define FILEINFORMATION_H#include <QtGui>class FileInformation : public QDialog {    Q_OBJECTpublic:    FileInformation( QWidget *parent=0, Qt::WindowFlags  f=0 );    ~FileInformation();public: QLabel* LabelFileName; QLineEdit* LineEditFileName; QPushButton* PushButtonFile; QLabel* LabelSize; QLineEdit* LineEditSize; QLabel* LabelCreated; QLineEdit* LineEditCreated; QLabel* LabelLastModified ; QLineEdit* LineEditLastModified;   QLabel* LabelLastRead ; QLineEdit* LineEditLastRead; QLabel* LabelProperty ;    QCheckBox* CheckBoxIsDir;  QCheckBox* CheckBoxIsFile; QCheckBox* CheckBoxIsSymLink; QCheckBox* CheckBoxIsHidden; QCheckBox* CheckBoxIsReadable;  QCheckBox* CheckBoxIsWritable;  QCheckBox* CheckBoxIsExecutable;         QPushButton* PushButtonGet;public slots: void slotFile();        void slotGet();};#endif----------------------------------------------------------------------------------------------//main.cpp#include "fileinformation.h"

#include <QApplication>int main( int argc, char **argv ){    QFont font("ZYSong18030",12);    QApplication::setFont(font);         QApplication a( argc, argv );         FileInformation fileinformation;    fileinformation.show();    return a.exec();}----------------------------------------------------------------------------------------------//fileinformation.pro

TEMPLATE = app

TARGET  = fileinformationHEADERS  = fileinformation.hSOURCES  = fileinformation.cpp /    main.cpp

目录
相关文章
|
5月前
【qt】如何读取文件并拆分信息?
【qt】如何读取文件并拆分信息?
61 0
|
5月前
|
网络协议
【qt】TCP客户端信息的接受和发送
【qt】TCP客户端信息的接受和发送
39 0
|
5月前
|
网络协议
【qt】TCP 服务端怎么收到信息?
【qt】TCP 服务端怎么收到信息?
68 0
|
6月前
|
C++
基于QT实现的拷贝文件以及实时进度条(简易版)
1.基于按钮或者菜单栏的槽里去写逻辑函数(我这边用的是菜单栏),ui实现的进度条 2.创建两个对象,一个是源文件,一个是目标文件分别用getopenfileName、getsavefileName函数即可。 3.利用QFile类去实现对两个文件的创建,因为QFile中可以获取文件的属性已经读写等。 4.循环的去读取源文件中的数据,然后写入目标文件
613 6
|
5月前
|
存储 算法 C++
【C++】C++ QT实现Huffman编码器与解码器(源码+课程论文+文件)【独一无二】
【C++】C++ QT实现Huffman编码器与解码器(源码+课程论文+文件)【独一无二】
153 4
|
5月前
【Qt 学习笔记】Qt窗口 | 标准对话框 | 文件对话框QFileDialog
【Qt 学习笔记】Qt窗口 | 标准对话框 | 文件对话框QFileDialog
1064 4
|
5月前
|
XML 开发框架 API
【Qt 学习笔记】QWidget的windowTitle属性 | windowIcon属性 | qrc文件机制
【Qt 学习笔记】QWidget的windowTitle属性 | windowIcon属性 | qrc文件机制
276 1
|
5月前
QT 软件打包为一个单独可执行.exe文件流程
QT 软件打包为一个单独可执行.exe文件流程
1076 0
|
5月前
|
安全 C++ Windows
Windows下C++使用gRPC(Qt和VS,含文件包和使用方法)
Windows下C++使用gRPC(Qt和VS,含文件包和使用方法)
|
5月前
|
数据安全/隐私保护
【qt】获取主机信息系统
【qt】获取主机信息系统
21 0