QFileDialog类(文件对话框)
#ifndef DIALOG_H #define DIALOG_H #include <QDialog> #include <QLabel> #include <QLineEdit> #include <QPushButton> #include <QHBoxLayout> //水平布局 #include <QVBoxLayout> //垂直布局 #include <QFileDialog> class Dialog : public QDialog { Q_OBJECT public: Dialog(QWidget *parent = 0); ~Dialog(); private: QLabel *FileNameLabel; QLineEdit *FileNameLineEdit; QPushButton *FileButton; QLabel *FileSizeLabel; QLineEdit *FileSizeLineEdit; QPushButton *GetFileInfoButton; private slots: void GetFileInfoFunc(); //用于打开文件 void GetFileSizeFunc(); //用于获取文件的大小 }; #endif // DIALOG_H
#include "dialog.h" #include "QDebug" Dialog::Dialog(QWidget *parent) : QDialog(parent) { setWindowTitle("QFileDialog类(文件对话框测试)"); //创建对应的控件 FileNameLabel =new QLabel("文件名称:"); FileNameLineEdit = new QLineEdit; FileButton=new QPushButton("选择..."); FileSizeLabel=new QLabel("文件大小:"); FileSizeLineEdit=new QLineEdit; GetFileInfoButton = new QPushButton("获取文件大小信息"); //排列布局 QGridLayout *glayout=new QGridLayout; glayout->addWidget(FileNameLabel,0,0); glayout->addWidget(FileNameLineEdit,0,1); glayout->addWidget(FileButton,0,2); glayout->addWidget(FileSizeLabel,1,0); glayout->addWidget(FileSizeLineEdit,1,1,1,2); glayout->addWidget(GetFileInfoButton,2,0,1,3); // QHBoxLayout *hlayout =new QHBoxLayout; // hlayout->addWidget(GetFileInfoButton); QVBoxLayout *vlayout=new QVBoxLayout(this); vlayout->addLayout(glayout); // vlayout->addLayout(hlayout); connect(FileButton,SIGNAL(clicked()),this,SLOT(GetFileInfoFunc())); connect(GetFileInfoButton,SIGNAL(clicked()),this,SLOT(GetFileSizeFunc())); } Dialog::~Dialog() { } void Dialog::GetFileInfoFunc() //用于打开文件 { QString strFileName=QFileDialog::getOpenFileName(this,"打开","/","File(*)"); qDebug() << strFileName; FileNameLineEdit->setText(strFileName); } void Dialog::GetFileSizeFunc() //用于获取文件大小 { // 获取单行编辑框控件里的路径 QString strFileName=FileNameLineEdit->text(); QFileInfo fileinfo(strFileName); qint64 fileSize=fileinfo.size(); FileSizeLineEdit->setText(QString::number(fileSize)); }
标签:文件,glayout,对话框,QFileDialog,Dialog,addWidget,new,include From: https://www.cnblogs.com/baisedeyu/p/17881859.html