#ifndef QFILEDIALOGTEST_H
#define QFILEDIALOGTEST_H
#include <QDialog>
#include <QDialog>
#include <QLabel>
#include <QPushButton>
#include <QLineEdit>
#include <QHBoxLayout>//水平布局
#include <QVBoxLayout>//垂直布局
#include <QFileDialog>
class QFileDialogTest : public QDialog
{
Q_OBJECT
public:
QFileDialogTest(QWidget *parent = nullptr);
~QFileDialogTest();
private:
QLabel * FileNameLabel;
QLineEdit * FileNameLineEdit;
QPushButton *FileButton;
QLabel * FileSizeLabel;
QLineEdit * FileSizeLabelLineEdit;
QPushButton * GetFileInfoButton;
private slots:
void GetFileInfoFunc();//用于打开文件
void GetFileSizeFunc();//用于获取文件大小
};
#endif // QFILEDIALOGTEST_H
#include "qfiledialogtest.h"
QFileDialogTest::QFileDialogTest(QWidget *parent)
: QDialog(parent)
{
setWindowTitle("QFileDialoge类(文件对话框类)");
//1:创建控件
FileNameLabel = new QLabel("文件名称:");
FileNameLineEdit = new QLineEdit();
FileButton = new QPushButton("选择...");
FileSizeLabel = new QLabel("文件大小:");
FileSizeLabelLineEdit = new QLineEdit();
GetFileInfoButton =new QPushButton("获取文件大小信息");
//2:排列布局
QGridLayout *glayoout = new QGridLayout();
glayoout->addWidget(FileNameLabel,0,0);
glayoout->addWidget(FileNameLineEdit,0,1);
glayoout->addWidget(FileButton,0,2);
glayoout->addWidget(FileSizeLabel,1,0);
glayoout->addWidget(FileSizeLabelLineEdit,1,1);
QHBoxLayout * hlayout = new QHBoxLayout();
hlayout->addWidget(GetFileInfoButton);
QVBoxLayout *vlayout = new QVBoxLayout(this);
vlayout->addLayout(glayoout);
vlayout->addLayout(hlayout);
//槽函数链接
connect(FileButton,SIGNAL(clicked()),this,SLOT(GetFileInfoFunc()));
connect(GetFileInfoButton,SIGNAL(clicked()),this,SLOT(GetFileSizeFunc()));
}
QFileDialogTest::~QFileDialogTest()
{
}
void QFileDialogTest::GetFileInfoFunc()//用于打开文件
{
QString strFileName = QFileDialog::getOpenFileName(this,"打开","/","Files(*)");
FileNameLineEdit->setText(strFileName);
}
void QFileDialogTest::GetFileSizeFunc()//用于获取文件大小
{
//获取单行编辑框里面文件路径
QString strFileNameS = FileNameLineEdit->text();
QFileInfo fileinfo(strFileNameS);
qint64 FileSize = fileinfo.size();
FileSizeLabelLineEdit->setText(QString::number(FileSize));
}
53-Qt文件对话框实战
最新推荐文章于 2024-11-08 15:01:32 发布