How to load html source from string code to QWebView[SOLVED]
-
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") ) ^
@ -
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);
@ -
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?
-
Qt version 5.2.1. Second parameter is not default, because i get error
"Error...":http://yadi.sk/d/gHOnhJLXRjywE
-
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. -
Do you have QT += webkitwidgets in the Formula.pro file?