Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to load html source from string code to QWebView[SOLVED]
Forum Updated to NodeBB v4.3 + New Features

How to load html source from string code to QWebView[SOLVED]

Scheduled Pinned Locked Moved General and Desktop
12 Posts 2 Posters 9.6k Views 1 Watching
  • 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.
  • W Offline
    W Offline
    WebSofter
    wrote on 30 May 2014, 16:59 last edited by
    #1

    I try to use QWebView with setHtml() method, but I didn't realize that the second argument:

    @
    //---------Scripts-----------
    QString textScript ="[removed]alert('Hello!')[removed]";
    //---------------------------
    //---------Styles------------
    QString textStyle="<style>#title{font-size:20px;background-color:red;text-align:center;}</style>";
    //---------------------------
    QString startText = "<html><head>"+textStyle+textScript+"</head><body><div id='title'>Hello!</div><br/>";
    QString endText = "</body></html>";
    QUrl baseUrl = QUrl::fromLocalFile( QDir::current().absoluteFilePath("mybinaryfile") );
    QString htmlText = startText + mDescription[buttonIndex] + endText;
    ui->webView->setHtml(htmlText, baseUrl);//does not work
    @

    Errors:
    @
    C:\Qt\Tools\QtCreator\bin\Formula\mainwindow.cpp:154: ошибка: incomplete type 'QDir' used in nested name specifier
    QUrl baseUrl = QUrl::fromLocalFile( QDir::current().absoluteFilePath("mybinaryfile") ) ^
    @

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andreyc
      wrote on 30 May 2014, 17:53 last edited by
      #2

      "Incomplete type" usually means that you did not provide header file and compiler can not find a class declaration.

      "Does not work" - does not compile or complied but has errors when app is running ?

      1 Reply Last reply
      0
      • W Offline
        W Offline
        WebSofter
        wrote on 30 May 2014, 17:59 last edited by
        #3

        andreyc, I need to put html code in the browser window, I can not do so? May need to use

        @ui->webView->page()->mainFrame()->setHtml(htmlText);//too does not work@
        instead
        @ui->webView->setHtml(htmlText, baseUrl);//does not work@

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andreyc
          wrote on 30 May 2014, 18:07 last edited by
          #4

          Does it compile ?

          If your htmlText does not contain any external data then you don't need to provide QUrl. See "here":https://qt-project.org/doc/qt-5/qwebview.html#setHtml for more details.

          Provide only htmlText
          @
          ui->webView->setHtml(htmlText);
          @

          1 Reply Last reply
          0
          • W Offline
            W Offline
            WebSofter
            wrote on 30 May 2014, 18:17 last edited by
            #5

            andreyc, no, the ui->webView no have method setHtml() with one parameter

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andreyc
              wrote on 30 May 2014, 18:23 last edited by
              #6

              The "doc for Qt 5.3":https://qt-project.org/doc/qt-5/qwebview.html#setHtml says that it has two parameters with default second parameter.
              @
              void QWebView::setHtml(const QString & html, const QUrl & baseUrl = QUrl())
              @
              It means that you don't have to provide the second parameter if the default one is good enough for your needs.

              BTW, what version of Qt do you use?

              Do you have compile error or run-time error when you try to use one parameter?

              1 Reply Last reply
              0
              • W Offline
                W Offline
                WebSofter
                wrote on 30 May 2014, 18:28 last edited by
                #7

                Qt version 5.2.1. Second parameter is not default, because i get error

                "Error...":http://yadi.sk/d/gHOnhJLXRjywE

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andreyc
                  wrote on 30 May 2014, 18:34 last edited by
                  #8

                  This is different class.
                  We were talking about "QWebView":https://qt-project.org/doc/qt-5/qwebview.html ::setHtml() and you have error in "QWebPage":http://qt-project.org/doc/qt-5/qwebpage.html
                  QWebPage does not have such function.

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andreyc
                    wrote on 30 May 2014, 18:36 last edited by
                    #9

                    Do you have QT += webkitwidgets in the Formula.pro file?

                    1 Reply Last reply
                    0
                    • W Offline
                      W Offline
                      WebSofter
                      wrote on 30 May 2014, 18:42 last edited by
                      #10

                      Haha, i am stupid!!! Thank, now works! And how can I enable JavaScript or he works by default?

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        andreyc
                        wrote on 30 May 2014, 18:49 last edited by
                        #11

                        You are welcome. Glad it works for you.

                        I never worked with QtWebKit, so I don't know.
                        I would suggest to start new thread this this question or look into the examples.

                        Please add "[SOLVED]" to the title of your post.

                        1 Reply Last reply
                        0
                        • W Offline
                          W Offline
                          WebSofter
                          wrote on 30 May 2014, 18:53 last edited by
                          #12

                          Okay

                          1 Reply Last reply
                          0

                          1/12

                          30 May 2014, 16:59

                          • Login

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