QFileDialog实战小项目

项目路径:

 实现效果:

代码

 1 #ifndef DIALOG_H
 2 #define DIALOG_H
 3 
 4 #include <QDialog>
 5 #include<QLabel>
 6 #include<QLineEdit>
 7 #include<QPushButton>
 8 
 9 #include<QVBoxLayout>
10 #include<QHBoxLayout>
11 #include<QFileDialog>
12 
13 class Dialog : public QDialog
14 {
15     Q_OBJECT
16 
17 public:
18     Dialog(QWidget *parent = nullptr);
19     ~Dialog();
20 private:
21     QLabel *FileNameLabel;
22     QLineEdit *FileNameEdit;
23     QPushButton *FileNameButton;
24 
25     QLabel *FileSizeLabel;
26     QLineEdit *FileSizeEdit;
27 
28     QPushButton *getFileSizeButton;
29 private slots:
30     void getFileName();
31     void getFileSize();
32 
33 };
34 #endif // DIALOG_H
dialog.h
 1 #include "dialog.h"
 2 
 3 Dialog::Dialog(QWidget *parent)
 4     : QDialog(parent)
 5 {
 6     FileNameLabel = new QLabel("文件名称:");
 7     FileNameEdit = new QLineEdit();
 8     FileNameButton = new QPushButton("获取文件");
 9 
10     FileSizeLabel = new QLabel("文件大小:");
11     FileSizeEdit = new QLineEdit();
12 
13     QGridLayout *qgdLayout = new QGridLayout();
14     qgdLayout->addWidget(FileNameLabel,0,0);
15     qgdLayout->addWidget(FileNameEdit,0,1);
16     qgdLayout->addWidget(FileNameButton,0,2);
17 
18     qgdLayout->addWidget(FileSizeLabel,1,0);
19     qgdLayout->addWidget(FileSizeEdit,1,1,1,2);
20 
21     QVBoxLayout *qvbLayout = new QVBoxLayout(this);
22     qvbLayout->addLayout(qgdLayout);
23 
24     getFileSizeButton = new QPushButton("获取文件大小");
25     QHBoxLayout *qhbLayout = new QHBoxLayout;
26     qhbLayout->addWidget(getFileSizeButton);
27 
28     qvbLayout->addLayout(qhbLayout);
29 
30     connect(FileNameButton,SIGNAL(clicked()),this,SLOT(getFileName()));
31     connect(getFileSizeButton,SIGNAL(clicked()),this,SLOT(getFileSize()));
32 }
33 
34 Dialog::~Dialog() {}
35 
36 
37 void Dialog::getFileName()
38 {
39     QString FileName = QFileDialog::getOpenFileName(this,"打开","/","Files(*)");
40     FileNameEdit->setText(FileName);
41 }
42 void Dialog::getFileSize()
43 {
44     QString FileNameToSize = FileNameEdit->text();
45     QFileInfo qFileInfo = QFileInfo(FileNameToSize);
46     qint64 qFileInfoSize = qFileInfo.size();
47     FileSizeEdit->setText(QString::number(qFileInfoSize));
48 }
dialog.cpp
 1 #include "dialog.h"
 2 
 3 #include <QApplication>
 4 
 5 int main(int argc, char *argv[])
 6 {
 7     QApplication a(argc, argv);
 8     Dialog w;
 9     w.show();
10     return a.exec();
11 }
main.cpp

 

posted @ 2024-05-08 10:33  左耳听风  阅读(4)  评论(0编辑  收藏  举报