Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Changing downloadDirectory property of WebEngineDownloadItem in QML QtQuick 2.2 and QtWebEngine 1.10



  • Hi ,
    I am running the webengine\quicknanobrowser example from qt installer for
    5.14.1 version.
    Here I want to change the download directory of downloadItem received in
    signal handler onDownloadRequested.
    If I hard code it to some folder then it works but I want to use a folder
    dialog to browse the folder and then set it to download.downloadDirectory.
    Here is what I tried but not working.

    FolderDialog {
    id: folderDialog
    onAccepted: {
    console.log("\n\nfolder picked");
    }
    }
    function onDownloadRequested(download) {
    folderDialog.open();
    if(folderDialog.result === Dialog.Accepted)
    {
    download.downloadDirectory = folderDialog.folder;
    console.log("$$\nnew dir: " +
    download.downloadDirectory);
    downloadView.append(download);
    download.accept();
    }
    }
    

    So it seems execution of the signal handler function continues after
    folderDialog.open() and once it is complete then it opens the dialog for
    browsing.
    How can execution of onDownloadRequested wait till the user browses folder?

    I tried using timer too like below but again not working:

    
    Timer {
    id: delaytimer;
    interval: 10000;
    running: false;
    repeat: false;
    onTriggered:
    {
    console.log("%%%\n\onTriggered");
    if(folderDialog.result === Dialog.Accepted)
    {
    console.log("%%%\n\done picking");
    folderPicked = true;
    delaytimer.running = false;//stop();
    }
    }
    }
    function onDownloadRequested(download) {
    folderDialog.open();
    delaytimer.running = true;
    while(delaytimer.running == true)
    {
    if(folderPicked == true)
    {
    download.downloadDirectory = folderDialog.folder;
    downloadView.visible = true;
    downloadView.append(download);
    download.accept();
    }
    }
    

    So we want to browse folder and select that folder as download
    directory(override default download directory).

    Can anyone please suggest if we can achieve this and how?

    Thanks in advance.
    Sheetal



  • If anyone comes here for solution, please read below reply from QT support team which I got.

    You need to block the execution in the handler until the
    download location has been set.
    Like below:
    In BrowserWindow.qml

    import Test 1.0
    
        FileDialog {
            id: fd
            selectFolder: true
            property bool done: false
            onAccepted: {
                done = true
            }
            onRejected: done = true
        }
    
        function onDownloadRequested(download) {
            fd.done = false
            fd.open()
            while(!fd.done) {
                App.process()
            }
            download.downloadDirectory = App.urlToDir(fd.folder)
            downloadView.visible = true;
            downloadView.append(download);
            download.accept();
        }
    

    In cpp files:

    class App : public QObject
    {
        Q_OBJECT
    public:
        Q_INVOKABLE QString urlToDir(QUrl url)
        {
            return url.toLocalFile();
    
        }
        Q_INVOKABLE void process()
        {
            qApp->processEvents();
        }
    };
    
    int main(int argc, char **argv)
    {
    ///all initialization code and including QApplication creation
        qmlRegisterSingletonInstance("Test", 1, 0, "App", new App);
    }
    

    Regards,
    Sheetal



  • If anyone comes here for solution, please read below reply from QT support team which I got.

    You need to block the execution in the handler until the
    download location has been set.
    Like below:
    In BrowserWindow.qml

    import Test 1.0
    
        FileDialog {
            id: fd
            selectFolder: true
            property bool done: false
            onAccepted: {
                done = true
            }
            onRejected: done = true
        }
    
        function onDownloadRequested(download) {
            fd.done = false
            fd.open()
            while(!fd.done) {
                App.process()
            }
            download.downloadDirectory = App.urlToDir(fd.folder)
            downloadView.visible = true;
            downloadView.append(download);
            download.accept();
        }
    

    In cpp files:

    class App : public QObject
    {
        Q_OBJECT
    public:
        Q_INVOKABLE QString urlToDir(QUrl url)
        {
            return url.toLocalFile();
    
        }
        Q_INVOKABLE void process()
        {
            qApp->processEvents();
        }
    };
    
    int main(int argc, char **argv)
    {
    ///all initialization code and including QApplication creation
        qmlRegisterSingletonInstance("Test", 1, 0, "App", new App);
    }
    

    Regards,
    Sheetal


Log in to reply