Qt 编程

您所在的位置:网站首页 option按钮在哪里 Qt 编程

Qt 编程

2024-01-06 07:45| 来源: 网络整理| 查看: 265

文档声明: 以下资料均属于本人在学习过程中产出的学习笔记,如果错误或者遗漏之处,请多多指正。并且该文档在后期会随着学习的深入不断补充完善。感谢各位的参考查看。

笔记资料仅供学习交流使用,转载请标明出处,谢谢配合。 如果存在相关知识点的遗漏,可以在评论区留言,看到后将在第一时间更新。 作者:Aliven888

1、简述

QFileDialog类是 Qt 提供的一个用于指定操作文件的对话框小部件。

2、使用详解 2.1、头文件

在使用文件对话框之前,需要调用如下头文件。

#include //文件对话框 2.2、获取选中的目录路径 // 这是一个静态函数,它将返回用户当前选择的目录。 // QString getExistingDirectory(QWidget *parent = Q_NULLPTR, const QString &caption = QString(),const QString &dir = QString(), Options options = ShowDirsOnly) //在Qt 5.2中引入 —— 使用方式和 getExistingDirectory 一样,但是支持远程网络路径(URL)的选择 QUrl getExistingDirectoryUrl(QWidget *parent = Q_NULLPTR, const QString &caption = QString(), const QUrl &dir = QUrl(), Options options = ShowDirsOnly, const QStringList &supportedSchemes = QStringList()) QString str = QFileDialog::getExistingDirectory(this, "getExistingDirectory", "D:\\Snipaste\\L"); qDebug("getExistingDirectory - [%s]", str.toUtf8().data());

参数介绍:

QWidget *parent = Q_NULLPTR:父窗口对象,通常是this。 const QString &caption = QString() :文件对话框标题。 const QString &dir = QString():设置的默认本地路径。 const QUrl &dir = QUrl() : 设置的默认本地或者网络路径。 Options options = ShowDirsOnly:路径下文件或者目录的展示方式,详细参数值,参见附录。 const QStringList &supportedSchemes : 允许限制用户能够选择的URL类型(是本地还是网络)。

演示结果: 在这里插入图片描述 在这里插入图片描述

2.3、获取选中的文件路径 //获取单个文件的本地路径 QString getOpenFileName(QWidget *parent = Q_NULLPTR, const QString &caption = QString(), const QString &dir = QString(), const QString &filter = QString(), QString *selectedFilter = Q_NULLPTR, Options options = Options()) //获取多个文件的本地路径 QStringList getOpenFileNames(QWidget *parent = Q_NULLPTR, const QString &caption = QString(), const QString &dir = QString(), const QString &filter = QString(), QString *selectedFilter = Q_NULLPTR, Options options = Options()) 同时,这两个函数也有与之对应的可以支持网络路径(URL)的函数。 //获取单个文件的本地或者网络路径 QUrl getOpenFileUrl(QWidget *parent = Q_NULLPTR, const QString &caption = QString(), const QUrl &dir = QUrl(), const QString &filter = QString(), QString *selectedFilter = Q_NULLPTR, Options options = Options(), const QStringList &supportedSchemes = QStringList()) //获取多个文件的本地或者网络路径 QList getOpenFileUrls(QWidget *parent = Q_NULLPTR, const QString &caption = QString(), const QUrl &dir = QUrl(), const QString &filter = QString(), QString *selectedFilter = Q_NULLPTR, Options options = Options(), const QStringList &supportedSchemes = QStringList()) 上面的两个成员函数使用方式一样。 区别就在一个仅能选一个文件,一个可以进行文件的多选。 这里以仅能选择单文件的函数进行演示: QString strPath = QFileDialog::getOpenFileName(this, "getOpenFileName", "D:\\Snipaste\\L", "(*.txt)"); qDebug("strPath = [%s]", strPath.toUtf8().data());

参数介绍:

QWidget *parent = Q_NULLPTR:父窗口对象,通常是this。 const QString &caption = QString():文件对话框标题。 const QString &dir = QString():默认打开文件目录。 const QString &filter = QString():文件类型筛选器。 QString *selectedFilter = Q_NULLPTR:默认的过滤器类型,当有多个过滤器类型时可以使用。 Options options = Options():路径下文件或者目录的展示方式,详细参数值,参见附录。 const QStringList &supportedSchemes : 允许限制用户能够选择的URL类型(是本地还是网络)。

演示结果: 在这里插入图片描述 在这里插入图片描述

3、附录:

flags QFileDialog::Options

ConstantValueDescriptionQFileDialog::ShowDirsOnly0x00000001仅在文件对话框中显示目录。 默认情况下,同时显示文件和目录。 (仅在目录文件模式下有效。)QFileDialog::DontResolveSymlinks0x00000002不要在文件对话框中解析符号链接。 默认情况下,符号链接被解析。QFileDialog::DontConfirmOverwrite0x00000004不要求确认是否选择了现有文件。 默认情况下,要求确认。QFileDialog::DontUseNativeDialog0x00000010不要使用本机文件对话框。 默认情况下,除非使用包含Q_OBJECT宏的QFileDialog的子类,否则平台将使用本机文件对话框,或者平台没有所需类型的本机对话框。QFileDialog::ReadOnly0x00000020表示只读模式QFileDialog::HideNameFilterDetails0x00000040表示是否隐藏文件名过滤器详细信息。QFileDialog::DontUseSheet0x00000008在早期版本的Qt中,如果为静态函数指定了父级,则默认情况下静态函数会创建一个工作表。 在Qt 4.5中不再支持此功能,并且不执行任何操作。静态函数将始终是应用程序模式对话框。 如果要使用图纸,请改用QFileDialog :: open()。QFileDialog::DontUseCustomDirectoryIcons0x00000080始终使用默认目录图标。 某些平台允许用户设置其他图标。 自定义图标查找会对网络或可移动驱动器产生很大的性能影响。 设置此选项将启用图标提供程序中的QFileIconProvider :: DontUseCustomDirectoryIcons选项。 这个枚举值是在Qt 5.2中添加的。


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3