库问题:如何设置QtWebKit来解析HTML?

库问题:如何设置QtWebKit来解析HTML?,html,qt,html-parsing,qtwebkit,qwebelement,Html,Qt,Html Parsing,Qtwebkit,Qwebelement,Nick Presta在这里展示了您可以使用qt解析HTML: 但是,当我尝试构建此文件时,在“QWebFrame*frame=page.mainFrame();”行中出现访问冲突 我做错了什么 #include <QtWebKit\QWebElement> #include <QtWebKit\QWebView> #include <QtWebKit\QWebFrame> #include <QtWebKit\QWebPage> #include

Nick Presta在这里展示了您可以使用qt解析HTML:

但是,当我尝试构建此文件时,在“QWebFrame*frame=page.mainFrame();”行中出现访问冲突

我做错了什么

#include <QtWebKit\QWebElement>
#include <QtWebKit\QWebView>
#include <QtWebKit\QWebFrame>
#include <QtWebKit\QWebPage>
#include <iostream>

int main() {
 QWebPage page;
 QWebFrame* frame = page.mainFrame();

 frame->setHtml( "<html><head></head><body></body></html>" );
 QWebElement document = frame->documentElement();

 return 0;
}
#包括
#包括
#包括
#包括
#包括
int main(){
QQ网页;
QWebFrame*frame=page.mainFrame();
框架->设置HTML(“”);
QWebElement文档=框架->文档元素();
返回0;
}

在Qt中执行任何有用的操作之前,通常需要一个
QApplication
(对于GUI,对于其他用户,请使用
qcoreApplicationon
)对象

尝试在main的顶部声明一个:

int main(int argc, char* argv[]) 
{
    QApplication a(argc, argv);

    ...

    return a.exec(); // start event handling (if you have some UI or networking that is event based)
}
返回
a.exec()
而不是
0
(正如编辑之前我的原始代码一样)是可以的,如果您有事件处理功能。如果您只想解析文档并使用它,那么可能不需要事件循环


OTOH,WebKit是异步的,因此运行exec循环并等待结果本身不是一个坏主意,只是不是必需的。

您可以像previewer一样搜索Qt示例,类似于以下代码:

QString text = plainTextEdit->toPlainText();
webView->setHtml(text, baseUrl);

任何涉及
QtGui
的内容都需要
QApplication
,对于其他内容,您需要
QCoreApplication