Results 1 to 4 of 4

Thread: Binding FileDialog folder to property

  1. #1
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Binding FileDialog folder to property

    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:
    Qt Code:
    1. import QtQuick 2.7
    2. import QtQuick.Window 2.2
    3. import QtQuick.Dialogs 1.2
    4.  
    5. Window
    6. {
    7. visible: true
    8.  
    9. MouseArea
    10. {
    11. anchors.fill: parent
    12.  
    13. FileDialog
    14. {
    15. id: fileDialog
    16. title: "Please choose a file"
    17. nameFilters: "NZB Files (*.nzb)"
    18. folder: app.faveFolder // binding to the property of Application
    19.  
    20. onAccepted:
    21. {
    22. //nzbFileDialog.folder = "file:///Z:/";
    23. app.openFile(fileDialog.fileUrl);
    24. print("new folder: " + fileDialog.folder);
    25.  
    26. // ! this line magically solves the issue :)
    27. //fileDialog.folder = fileDialog.folder;
    28. }
    29. } // FileDialog
    30.  
    31. onClicked:
    32. {
    33. print("app.faveFolder = " + app.faveFolder + " fileDialog.folder = " + fileDialog.folder);
    34. fileDialog.visible = true;
    35. //fileDialog.open();
    36. }
    37. }
    38.  
    39. Text
    40. {
    41. text: qsTr("Click to open")
    42. anchors.centerIn: parent
    43. }
    44. }
    To copy to clipboard, switch view to plain text mode 

    The Application instance (app) is passed to QML in main.cpp:
    Qt Code:
    1. #include "application.h"
    2.  
    3. //#include <QGuiApplication>
    4. #include <QQmlApplicationEngine>
    5. #include <QQmlContext>
    6.  
    7. int main(int argc, char *argv[])
    8. {
    9. Application app(argc, argv);
    10.  
    11. QQmlApplicationEngine engine;
    12. engine.rootContext()->setContextProperty("app", &app);
    13. engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    14.  
    15. return app.exec();
    16. }
    To copy to clipboard, switch view to plain text mode 

    Application.h:
    Qt Code:
    1. #ifndef APPLICATION_H
    2. #define APPLICATION_H
    3.  
    4. #include <QGuiApplication>
    5.  
    6. class Application: public QGuiApplication
    7. {
    8. Q_OBJECT
    9.  
    10. //Q_PROPERTY(QString nzbFolder MEMBER nzbDir NOTIFY nzbFolderChanged)
    11. Q_PROPERTY(QString faveFolder MEMBER faveDir NOTIFY faveFolderChanged)
    12.  
    13. public:
    14. Application(int argc, char * argv[]);
    15.  
    16. Q_INVOKABLE void openFile(const QString &filePath);
    17.  
    18. signals:
    19. void faveFolderChanged();
    20. //void faveFolderChanged(const QString &newPath);
    21.  
    22. private:
    23. QString faveDir;
    24. };
    25.  
    26. #endif // APPLICATION_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "application.h"
    2.  
    3. #include <QUrl>
    4. #include <QFileInfo>
    5. #include <QDebug>
    6.  
    7. Application::Application(int argc, char * argv[]): QGuiApplication(argc, argv)
    8. {
    9. this->faveDir = "file:///D:/temp"; // set the initial value
    10. }
    11.  
    12. void Application::openFile(const QString &filePath)
    13. {
    14. qDebug() << filePath;
    15. this->faveDir = "file:///Z:/"; // set the new value
    16. //this->faveDir = QUrl::fromLocalFile(QFileInfo(QUrl(filePath).toLocalFile()).absolutePath()).toString();
    17. qDebug() << this->faveDir;
    18. emit faveFolderChanged();
    19. //emit faveFolderChanged(this->faveDir);
    20. }
    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.
    Magicians do not exist

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Binding FileDialog folder to property

    The "folder" property is very likely set by the dialog if you change the folder, which overwrites the initial binding.

    Why the commented line fixes that I don't know, usually it would be required to reset the binding, e.g. using Qt.binding().

    Cheers,
    _

  3. #3
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: Binding FileDialog folder to property

    The "folder" property is very likely set by the dialog if you change the folder, which overwrites the initial binding.
    Not sure I understand you correctly. The thing is the dialog uses the initial value of the folder property regardless of what directory I navigated to and regardless of the new value of this property. For example, in my case the folder property is initialized with the value "file:///d:/temp". Then I open a file from the directory "z:/", the property is updated to "file:///z:/", but when I open the dialog again it still points to "d:/temp". So I can't understand what the dialog sets and why.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Binding FileDialog folder to property

    Ah, interesting.

    I guess it is an "input only" property then, assuming that the developer changes it if necessary.

    Cheers,
    _

Similar Threads

  1. Replies: 3
    Last Post: 2nd April 2016, 12:46
  2. property binding in a repeater
    By sedi in forum Qt Quick
    Replies: 6
    Last Post: 2nd April 2015, 08:45
  3. Replies: 1
    Last Post: 22nd August 2013, 00:23
  4. Dynamic QML Property Binding
    By EMCEE in forum Qt Quick
    Replies: 1
    Last Post: 30th August 2012, 12:51
  5. QML Property Binding update does not propagate
    By bluestreak in forum Qt Quick
    Replies: 0
    Last Post: 21st August 2012, 20:24

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.