QFileDialog 默认会存储上一次选择的路径,如果下次打开没设置 dir ,则默认以保存的路径打开。Qt5.15.2 源码相关片段,lastVisited 字段:
QFileDialogArgs::QFileDialogArgs(const QUrl &url)
{
// default case, re-use QFileInfo to avoid stat'ing
const QFileInfo local(url.toLocalFile());
// Get the initial directory URL
if (!url.isEmpty())
directory = _qt_get_directory(url, local);
if (directory.isEmpty()) {
const QUrl lastVisited = *lastVisitedDir();
if (lastVisited != url)
directory = _qt_get_directory(lastVisited, QFileInfo());
}
if (directory.isEmpty())
directory = QUrl::fromLocalFile(QDir::currentPath());
/*
The initial directory can contain both the initial directory
and initial selection, e.g. /home/user/foo.txt
*/
if (selection.isEmpty() && !url.isEmpty()) {
if (url.isLocalFile()) {
if (!local.isDir())
selection = local.fileName();
} else {
// With remote URLs we can only assume.
selection = url.fileName();
}
}
}
#if QT_CONFIG(settings)
void QFileDialogPrivate::saveSettings()
{
Q_Q(QFileDialog);
QSettings settings(QSettings::UserScope, QLatin1String("QtProject"));
settings.beginGroup(QLatin1String("FileDialog"));
//... ...
settings.setValue(QLatin1String("lastVisited"), lastVisitedDir()->toString());
//... ...
}
bool QFileDialogPrivate::restoreFromSettings()
{
Q_Q(QFileDialog);
QSettings settings(QSettings::UserScope, QLatin1String("QtProject"));
if (!settings.childGroups().contains(QLatin1String("FileDialog")))
return false;
settings.beginGroup(QLatin1String("FileDialog"));
q->setDirectoryUrl(lastVisitedDir()->isEmpty() ? settings.value(QLatin1String("lastVisited")).toUrl() : *lastVisitedDir());
//... ...
return restoreWidgetState(history, settings.value(QLatin1String("sidebarWidth"), -1).toInt());
}
#endif // settings
注册表对应位置:计算机\HKEY_CURRENT_USER\SOFTWARE\QtProject\OrganizationDefaults\FileDialog
如果设置了 Organization 应该会放到对应的路径下。
二、Qt Quick 问题交流 1.QtQuick.Dialogs 的 FileDialog 切换 nameFilters 等属性如果是在 FileDialog 的 visibleChanged 中修改 nameFilters 属性,会报警告,且设置不生效。可以在调用 open() 之前设置好 nameFilters。(当前,也别绑定属性来动态切换,因为在弹出之后你没法重置这个属性)
Button {
text: 'dialog'
onClicked: {
dialog.flag=!dialog.flag
if(dialog.flag){
dialog.nameFilters=["(*.txt)"]
}else{
dialog.nameFilters=["(*.h *.cpp)"]
}
dialog.open()
}
}
FileDialog {
id: dialog
title: "import"
visible: false
folder: shortcuts.desktop
selectExisting: true
selectFolder: false
selectMultiple: true
modality: Qt.ApplicationModal
nameFilters: ["(*.txt)","(*.h *.cpp)"]
property bool flag: true
//不能在visible中设置,不会生效
//会有警告:QWindowsNativeFileDialogBase::selectNameFilter: Invalid parameter 'xxx' not found in 'xxx'.
onVisibleChanged: {
/*if(visible){
dialog.flag=!dialog.flag
if(dialog.flag){
dialog.nameFilters=["(*.txt)"]
}else{
dialog.nameFilters=["(*.h *.cpp)"]
}
}*/
}
}
三、其他
1.Qt5 及以下 QByteArray 不能超过 2G
参照文档:https://doc.qt.io/qt-5/qbytearray.html
Maximum size and out-of-memory conditionsThe current version of QByteArray is limited to just under 2 GB (2^31 bytes) in size. The exact value is architecture-dependent, since it depends on the overhead required for managing the data block, but is no more than 32 bytes. Raw data blocks are also limited by the use of int
type in the current version to 2 GB minus 1 byte.
In case memory allocation fails, QByteArray will throw a std::bad_alloc
exception. Out of memory conditions in the Qt containers are the only case where Qt will throw exceptions.
Note that the operating system may impose further limits on applications holding a lot of allocated memory, especially large, contiguous blocks. Such considerations, the configuration of such behavior or any mitigation are outside the scope of the QByteArray API.
机翻一下:
当前版本的 QByteArray 大小限制在 2 GB(2^31 字节)以下。确切的值取决于体系结构,因为它取决于管理数据块所需的开销,但不超过 32 字节。原始数据块也受int
当前版本中使用类型的限制为 2 GB 减 1 个字节。
如果内存分配失败,QByteArray 将抛出std::bad_alloc
异常。Qt 容器中的内存不足情况是 Qt 会抛出异常的唯一情况。
请注意,操作系统可能会对持有大量已分配内存的应用程序施加进一步限制,尤其是大的连续块。此类考虑、此类行为的配置或任何缓解措施超出了 QByteArray API 的范围。
Qt5 可以自己封装一个字节数据容器,以支持更大的数据存储。 Qt6 的时候,Qt 容器使用 qsizetype 来表示 size() ,在 64 位编译时支持更大的数值,不过在 32 位编译时依然受限在 2GB 内(https://doc.qt.io/qt-6/qbytearray.html)。
2.main 参数带空格会被分割int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
QStringList arglist = app.arguments();
}
代码如上,一般 argv[0] 为程序的完整路径,如果启动参数填aabb中间加个空格 >>app.exe aa bb,那么就会被识别成 aa 和 bb 两个单独得参数,arglist 取出来也是分开的。可以将参数加上双引号括起来,>>app.exe "aa bb",那么 arglist[1] 就是 aa bb 字符串了。
3.QDate/QTime.toString() 输出 h/m/s 等格式化用到的原字符参照文档:https://doc.qt.io/qt-5/qtime.html#toString
可以使用单引号将字符串括起来,就不会检索其中的格式化字符,两个连续的单引号输出为一个单引号字符。
QDateTime d = QDateTime::currentDateTime();
//h会被识别为格式化字符,所以得加单引号来输出原字符
qDebug()
关注
打赏
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?