0


I use QWebEngine to view a website, there is a download popup window show up and i need to download it a folder i set i use this code,
this to get any signal of a download file

ui->widget->load(QUrl(ui->lineEdit->text().trimmed()));
QWebEnginePage *page = ui->widget->page();
QWebEngineProfile *profile = page->profile();
connect(profile, SIGNAL(downloadRequested(QWebEngineDownloadItem*)), this, SLOT(DownloadItem(QWebEngineDownloadItem*)));

then I do this to start accept and download the file in the slot

void MainWindow::DownloadItem(QWebEngineDownloadItem *item)
{
    item->setPath("D:/amr.pdf");
    connect(item, SIGNAL(finished()), this, SLOT(DownloadFinish()));
    connect(item, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downloadProgress(qint64,qint64)));
    item->accept();
    qDebug() << "URL to download = " << item->url().toString();
}

The trick here is after the I download the file, there is a javascript file shows up and ask me to name the file, so the question here how can i get the file name wrote in this javascript dialog, here is an image of how it looks like enter image description here so I need a way to get the file name in the slot or anything so i can use this to take this name and name the file before i press ok and the download start.

Thanks.

user7179690
  • 1,051
  • 3
  • 17
  • 40
  • The path is in the C++ side and you want to use it in JS side? – Simon Jun 18 '19 at 12:51
  • this javascript prompt show in my QWebEngineView, I just want the value inside it to get it in a QString for example so i can use it and name the file which is downloaded, this dialog generated by the QWebEngineView I just used the javascript dialog as it's written and I don't know what is it exactly. – user7179690 Jun 18 '19 at 13:15

1 Answers1

1

Javascript Prompt window is implemented in QWebEnginePage using static QInputDialog::getText. If you want to customize this dialog or do any manipulations with text before it will be returned back to JS, you need to subclass QWebEnginePage and override QWebEnginePage::javaScriptPrompt function.

Here is a simple example:

mywebpage.h

#ifndef MYWEBPAGE_H
#define MYWEBPAGE_H

#include <QObject>
#include <QWebEnginePage>
#include <QWebEngineProfile>

class MyWebPage : public QWebEnginePage
{
public:
    explicit MyWebPage(QWebEngineProfile *profile, QObject *parent = Q_NULLPTR):QWebEnginePage(profile, parent){}

protected:
    bool javaScriptPrompt(const QUrl &securityOrigin, const QString& msg, const QString& defaultValue, QString* result) override;

};

#endif // MYWEBPAGE_H

mywebpage.cpp

#include "mywebpage.h"
#include <QDebug>
#include <QInputDialog>

bool MyWebPage::javaScriptPrompt(const QUrl &securityOrigin, const QString& msg, const QString& defaultValue, QString* result)
{
    bool ok = false;
    QString save_me = QInputDialog::getText(this->view(), tr("MyJavaScript Prompt"), msg, QLineEdit::Normal, defaultValue, &ok);

    //do any manipulations with save_me
    qDebug() << "User entered this string: " << save_me;

    //... and copy it to result
    result->append(save_me);

    return ok;
}

And here is example how you can set your WebPage subclass to WebView instance:

auto webview = new QWebEngineView(this);
webview->setPage(new MyWebPage(QWebEngineProfile::defaultProfile(), webview));

//you can test your Prompt here
webview->load(QUrl::fromUserInput("https://www.w3schools.com/Jsref/tryit.asp?filename=tryjsref_prompt"));
Xplatforms
  • 2,102
  • 17
  • 26