欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

QT获取文件名

程序员文章站 2022-06-08 10:26:20
...
//.h
#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QPushButton>
#include <QLineEdit>
#include <QGridLayout>
#include <QFileDialog>

class Dialog : public QDialog
{
    Q_OBJECT

public:
    Dialog(QWidget *parent = 0);
    ~Dialog();
private:
    QPushButton *fileButton;
    QLineEdit *fileLineEdit;
    QGridLayout *mainLayout;

private slots:
    void showfile();

};

#endif // DIALOG_H

//.cpp

//.cpp
#include "dialog.h"

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    fileButton = new QPushButton;
    fileButton->setText(tr("文件标准对话框"));
    fileLineEdit = new QLineEdit;

    mainLayout = new QGridLayout(this);
    mainLayout->addWidget(fileButton,0,0);
    mainLayout->addWidget(fileLineEdit,0,1);

    connect(fileButton,SIGNAL(clicked()),this,SLOT(showfile()));
}

Dialog::~Dialog()
{

}

void Dialog::showfile()
{
    /*
    QFileDialog的四个静态函数
    getOpenFileName :获取用户选择的文件名
    getSaveFileName :获取用户保存的文件名
    getExistingDirectory :获取用户选择的已保存的目录名
    getOpenFileNames :获取用户选择的文件名列表

*/
    //添加默认路径名
    QString s = QFileDialog::getOpenFileName(this,"open file dialog","./",
                 "C++ files(*.cpp) ;; C files(*.c);;Head files(*.h)");
    fileLineEdit->setText(s);
}