PDA

View Full Version : WebView with QQuickWidget and QQuikView



yagabey
11th August 2017, 13:35
Hi,

I am trying to view an html file(google maps) with WebView on QQuickWidget. The html loads without an error(console log says that) ;but nothing is shown on QQuickWidget, just a white screen..

When I replace QQuickWidget with QQuickView everything works fine. But since there are some issues with QQuickView on mobile platforms, I dont want to use it.

I have attached a minimal runnable code.

Any idea?

Thanks in advance..

Code looks like that:




MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

#ifdef WITH_QUICKWIDGET
view = new QQuickWidget();
ui->verticalLayout->addWidget(view);
#else
view = new QQuickView();
QWidget* container = QWidget::createWindowContainer(view);
ui->verticalLayout->addWidget(container);
#endif
view->setSource(QUrl::fromLocalFile(QLatin1String(":/web/weblink.qml")));
mapApiKey = QString::fromLatin1("AIzaSyAOeIxkqfAs9HQrz_PY7QPz7ZzKiML7hDE");
m_webDir = QDir::homePath()+QString::fromLatin1("/") ;
loadHtml();
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::loadHtml()
{

QString htmlData = loadFileData(QString::fromLatin1(":/web/map.html"));
htmlData.replace(QLatin1String("$$API_KEY"),mapApiKey);
htmlData.replace(QLatin1String("$$PORT"),QLatin1String("1000"));
htmlData.replace(QLatin1String("$$WEB_DIR"),QUrl::fromLocalFile(m_webDir).toString());

QString htmlfname = QString::fromLatin1("map_%1.html").arg(QDateTime::currentMSecsSinceEpoch());
htmlData.replace(QLatin1String("$$MAP_HTML"),htmlfname);

m_filePath = saveFile( htmlData, htmlfname);

if(!QFileInfo::exists(m_webDir + QString::fromLatin1("jquery.min.js")))
QFile::copy(QLatin1String(":/web/jquery.min.js") , m_webDir+QLatin1String("jquery.min.js"));

if(!QFileInfo::exists(m_webDir + QString::fromLatin1("label.js")))
QFile::copy(QLatin1String(":/web/label.js") , m_webDir+QLatin1String("label.js"));

QObject *object = view->rootObject();
QQmlProperty::write(object, QString::fromLatin1("url"), QUrl::fromLocalFile(m_filePath).toString());
}

QString MainWindow::loadFileData(QString path)
{
QString data;
QFile readfile(path);
if(readfile.open(QIODevice::ReadOnly)){
QTextStream txt(&readfile);
data = txt.readAll();
readfile.close();
}
return data;
}

QString MainWindow::saveFile(QString htmldata, QString fname)
{
QString mpath= m_webDir+fname;
QFile writefile(mpath);
if(writefile.open(QIODevice::WriteOnly)){
QTextStream txt(&writefile);
txt << htmldata;
writefile.close();
}
return mpath;
}

yagabey
11th August 2017, 23:54
I have detected a problem. In the weblink.qml, when I replace :


WebView {
id: webView
anchors.fill: parent
with:

WebView {
id: webView
width : 1920
height: 1080

The web page shows up on QQuickWidget correctly. But of course size of the webview is not changing dynamically in that case. So it looks like "anchors.fill: parent" is not working as expected with WebView and QQuickWidget combination.

Is this a known bug and is there any workaround for this ? Thanks