Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. WebView with QQuickWidget and QQuikView
Qt 6.11 is out! See what's new in the release blog

WebView with QQuickWidget and QQuikView

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 1 Posters 976 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    yagabey
    wrote on last edited by
    #1

    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.

    Here is a minimal runnable code.

    https://www.dropbox.com/s/iepydx40rr6ni0f/maptest.zip?dl=0

    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;
    }
    
    Y 1 Reply Last reply
    0
    • Y yagabey

      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.

      Here is a minimal runnable code.

      https://www.dropbox.com/s/iepydx40rr6ni0f/maptest.zip?dl=0

      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;
      }
      
      Y Offline
      Y Offline
      yagabey
      wrote on last edited by
      #2

      @yagabey said in WebView with QQuickWidget and QQuikView:

      weblink.qml

      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 cource 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

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved