QWebview not available in QT creator
-
Hi everyone,
I'm trying to do DOM traversal so that i can get information i want from HTML code. I've found a solution that says you need QWebView from QT creator. However, my Qt creator does not have QWebView in the design portion. I've googled but yet to find any solution can someone help me?
-
@GCDX
Independent of anything you might or might not have available in Qt Creator (I don't use it, so I don't know), what version of Qt are you using? BecauseQWebView
was available in Qt 4 plus Qt 5 up to 5.5. But at Qt 5.6 it was deprecated, as Qt moved from WebKit to WebEngine. WebKit/QWebView
is no longer part of the "standard" Qt build, you have to take special action to create it.Whichever one you choose, if you only want "to do DOM traversal so that i can get information i want from HTML code", you can always create an instance of whichever dynamically at run-time instead of statically at design-time and do your work. Especially no need to create it in Qt Creator if you don't intend to present it visually to the end-user.
-
Hi @JonB , and thank you your reply!
Could you elaborate on " you can always create an instance of whichever dynamically at run-time instead of statically at design-time and do your work. Especially no need to create it in Qt Creator if you don't intend to present it visually to the end-user."?
Sorry , i'm rather new to qt, so i don't really know how to solve this. I tried using webview in qt 5.0.2 but i got stuck. Here is what i tried!The result is that i can't seem to find out what's wrong with this code.
-
@GCDX
I don't really know what you're asking now.-
You are already creating it dynamically with your
QWebView *innerPage = new QWebView()
. So you are managing to create one without having had to use anything in Qt Creator to achieve/view it, aren't you? -
Are you asking about the underline on
body
in your code, or theLEAK
s on exit? -
Are you asking about why you get empty strings from your
qDebug()
statements? -
Or.... ?
-
-
Yes i created a WebView but in the design UI it doesn't have the function for me to put inside the QML code for design.
I am trying to parse out the strings HelloWorld and TESTCODE. However, when i try to store the strings using the toPlainText() and print it out as using QDebug, it prints an empty string, so i dont know what is wrong with the code.
YES! Why do i get empty strings??
-
Yes i created a WebView but in the design UI it doesn't have the function for me to put inside the QML code for design.
I know nothing about QML or the Qt Creator designer. I don't know what "it doesn't have the function for me to put inside the QML code for design" might mean.
YES! Why do i get empty strings??
The only thing you have done in your code is:
innerPage->setUrl(url);
You then assume you can immediately access the elements & their content. You can't. Setting the Url only starts to load the document. It does not finish loading, and therefore have full data available, until a signal has been received. See the solution & explanation at https://stackoverflow.com/a/10767844/489865 (
QWebView::loadFinished(bool ok)
signal). -
@JonB Thanks so much for your help, could you indulge me a little and explain a little more? I dont fully understand the link.
But i did this! It doesn't compile, but i really don't know how to do it.
void MainWindow::on_pushButton_clicked()
{QWebView *innerPage = new QWebView(); QUrl url("http://www.comp.nus.edu.sg/~geraldc/web/index.html"); /* Code on the webpage is * <html> * <head></head> * <body> * <p>HelloWorld</p> * <p>TESTCODE</p> * </body> * </html> */ innerPage->setUrl(url); connect(innerPage, SIGNAL(loadFinished(bool)), SLOT(parse(innerPage)));
}
void MainWindow::parse(QWebView *innerpage)
{
QWebFrame *frameInner = innerPage->page()->mainFrame();
QWebElement doc = frameInner->documentElement();
QWebElement body = doc.firstChild();
QWebElement firstParagraph =doc.firstChild();
QWebElement secondParagraph = firstParagraph.nextSibling();
QString storedText = firstParagraph.toPlainText();
QString text = secondParagraph.toPlainText();
qDebug()<<QString(storedText);
qDebug()<<secondParagraph.toPlainText();
} -
@GCDX said in QWebview not available in QT creator:
connect(innerPage, SIGNAL(loadFinished(bool)), SLOT(parse(innerPage)));
this is not how signals/slots work. You cannot pass parameters to a slot in a connect.
Change toconnect(innerPage, SIGNAL(loadFinished(bool)), SLOT(parse(bool)));
You can get the sender in the slot calling sender() function.
Take a look at http://doc.qt.io/qt-5.9/signalsandslots.html -
@jsulm @JonB Thanks so much for both your help!!! i managed to parse out the HelloWorld and TESTCODE. However, its a little odd.
The first paragraph that is supposed to parse out HelloWorld returns a "" but the second paragraph parsed out both " Hello World TESTCODE" Its a little strange. But thank you for your help!!!! -
The first paragraph that is supposed to parse out HelloWorld returns a "" but the second paragraph parsed out both " Hello World TESTCODE" Its a little strange. But thank you for your help!!!!
It's to do with
doc.firstChild()
anddoc.firstChild().nextSibling()
not being quite where you think they are in the parsed DOM. For example, try recursing down into them and you'll see what they are actually positioned on, if you know how to use debugger it's much easier that debug messages. Since you want to do DOM traversal you will end up needing to understand the structure to get the code right.