There is a Qt Quick application which allows the user to open some type of a file. The folder property of the file dialog is bound to the property of the application class (in my case it is faveFolder) and the application may change the value of this property. Thus I would expect the FileDialog to be positioned on the new folder. In fact, it respects only the initial value of the property.
My QML code is shown below:
import QtQuick 2.7
import QtQuick.Window 2.2
import QtQuick.Dialogs 1.2
Window
{
visible: true
MouseArea
{
anchors.fill: parent
FileDialog
{
id: fileDialog
title: "Please choose a file"
nameFilters: "NZB Files (*.nzb)"
folder: app.faveFolder // binding to the property of Application
onAccepted:
{
//nzbFileDialog.folder = "file:///Z:/";
app.openFile(fileDialog.fileUrl);
print("new folder: " + fileDialog.folder);
// ! this line magically solves the issue :)
//fileDialog.folder = fileDialog.folder;
}
} // FileDialog
onClicked:
{
print("app.faveFolder = " + app.faveFolder + " fileDialog.folder = " + fileDialog.folder);
fileDialog.visible = true;
//fileDialog.open();
}
}
Text
{
text: qsTr("Click to open")
anchors.centerIn: parent
}
}
import QtQuick 2.7
import QtQuick.Window 2.2
import QtQuick.Dialogs 1.2
Window
{
visible: true
MouseArea
{
anchors.fill: parent
FileDialog
{
id: fileDialog
title: "Please choose a file"
nameFilters: "NZB Files (*.nzb)"
folder: app.faveFolder // binding to the property of Application
onAccepted:
{
//nzbFileDialog.folder = "file:///Z:/";
app.openFile(fileDialog.fileUrl);
print("new folder: " + fileDialog.folder);
// ! this line magically solves the issue :)
//fileDialog.folder = fileDialog.folder;
}
} // FileDialog
onClicked:
{
print("app.faveFolder = " + app.faveFolder + " fileDialog.folder = " + fileDialog.folder);
fileDialog.visible = true;
//fileDialog.open();
}
}
Text
{
text: qsTr("Click to open")
anchors.centerIn: parent
}
}
To copy to clipboard, switch view to plain text mode
The Application instance (app) is passed to QML in main.cpp:
#include "application.h"
//#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
int main(int argc, char *argv[])
{
Application app(argc, argv);
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("app", &app);
engine.
load(QUrl(QStringLiteral
("qrc:/main.qml")));
return app.exec();
}
#include "application.h"
//#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
int main(int argc, char *argv[])
{
Application app(argc, argv);
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("app", &app);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Application.h:
#ifndef APPLICATION_H
#define APPLICATION_H
#include <QGuiApplication>
class Application: public QGuiApplication
{
Q_OBJECT
//Q_PROPERTY(QString nzbFolder MEMBER nzbDir NOTIFY nzbFolderChanged)
Q_PROPERTY(QString faveFolder MEMBER faveDir NOTIFY faveFolderChanged
)
public:
Application(int argc, char * argv[]);
Q_INVOKABLE
void openFile
(const QString &filePath
);
signals:
void faveFolderChanged();
//void faveFolderChanged(const QString &newPath);
private:
};
#endif // APPLICATION_H
#ifndef APPLICATION_H
#define APPLICATION_H
#include <QGuiApplication>
class Application: public QGuiApplication
{
Q_OBJECT
//Q_PROPERTY(QString nzbFolder MEMBER nzbDir NOTIFY nzbFolderChanged)
Q_PROPERTY(QString faveFolder MEMBER faveDir NOTIFY faveFolderChanged)
public:
Application(int argc, char * argv[]);
Q_INVOKABLE void openFile(const QString &filePath);
signals:
void faveFolderChanged();
//void faveFolderChanged(const QString &newPath);
private:
QString faveDir;
};
#endif // APPLICATION_H
To copy to clipboard, switch view to plain text mode
#include "application.h"
#include <QUrl>
#include <QFileInfo>
#include <QDebug>
Application::Application(int argc, char * argv[]): QGuiApplication(argc, argv)
{
this->faveDir = "file:///D:/temp"; // set the initial value
}
void Application
::openFile(const QString &filePath
) {
qDebug() << filePath;
this->faveDir = "file:///Z:/"; // set the new value
//this->faveDir = QUrl::fromLocalFile(QFileInfo(QUrl(filePath).toLocalFile()).absolutePath()).toString();
qDebug() << this->faveDir;
emit faveFolderChanged();
//emit faveFolderChanged(this->faveDir);
}
#include "application.h"
#include <QUrl>
#include <QFileInfo>
#include <QDebug>
Application::Application(int argc, char * argv[]): QGuiApplication(argc, argv)
{
this->faveDir = "file:///D:/temp"; // set the initial value
}
void Application::openFile(const QString &filePath)
{
qDebug() << filePath;
this->faveDir = "file:///Z:/"; // set the new value
//this->faveDir = QUrl::fromLocalFile(QFileInfo(QUrl(filePath).toLocalFile()).absolutePath()).toString();
qDebug() << this->faveDir;
emit faveFolderChanged();
//emit faveFolderChanged(this->faveDir);
}
To copy to clipboard, switch view to plain text mode
This is what was printed to the output window:
qml: app.faveFolder = file:///D:/temp fileDialog.folder = file:///D:/temp
"file:///Z:/crash_part29.nzb"
"file:///Z:/"
qml: new folder: file:///Z:/
qml: app.faveFolder = file:///Z:/ fileDialog.folder = file:///Z:/
As a one can see from the last line, which is printed right before showing the file dialog, it does have the updated value. In spite of this, when opened FileDialog is still positioned to the old folder. And the interesting thing here is that uncommenting the line "fileDialog.folder = fileDialog.folder" somehow solves the issue.
Bookmarks