How to load html source from string code to QWebView[SOLVED]
-
wrote on 30 May 2014, 16:59 last edited by
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") ) ^
@ -
wrote on 30 May 2014, 17:53 last edited by
"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 ?
-
wrote on 30 May 2014, 17:59 last edited by
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@ -
wrote on 30 May 2014, 18:07 last edited by
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);
@ -
wrote on 30 May 2014, 18:17 last edited by
andreyc, no, the ui->webView no have method setHtml() with one parameter
-
wrote on 30 May 2014, 18:23 last edited by
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?
-
wrote on 30 May 2014, 18:28 last edited by
Qt version 5.2.1. Second parameter is not default, because i get error
"Error...":http://yadi.sk/d/gHOnhJLXRjywE
-
wrote on 30 May 2014, 18:34 last edited by
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. -
wrote on 30 May 2014, 18:36 last edited by
Do you have QT += webkitwidgets in the Formula.pro file?
-
wrote on 30 May 2014, 18:42 last edited by
Haha, i am stupid!!! Thank, now works! And how can I enable JavaScript or he works by default?
-
wrote on 30 May 2014, 18:49 last edited by
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.
-
wrote on 30 May 2014, 18:53 last edited by
Okay
1/12