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") ) ^
@ -
"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 ?
-
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@ -
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);
@ -
andreyc, no, the ui->webView no have method setHtml() with one parameter
-
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?
-
Haha, i am stupid!!! Thank, now works! And how can I enable JavaScript or he works by default?
-
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.
-
Okay