【问题标题】:LIbrary issue: How do I set up QtWebKit to parse HTML?库问题:如何设置 QtWebKit 来解析 HTML?
【发布时间】:2011-01-04 00:56:17
【问题描述】:

Nick Presta 在这里展示了您可以使用 qt 解析 HTML:https://stackoverflow.com/questions/489522/library-recommendation-c-html-parser

但是,当我尝试构建它时,我在“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;
}

【问题讨论】:

    标签: html qt html-parsing qtwebkit qwebelement


    【解决方案1】:

    在 Qt 中做任何有用的事情之前,您通常需要一个 QApplication(对于 GUI,对于其他人,使用 QCoreApplicaiton)对象。

    尝试在 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 循环并等待结果本身并不是一个坏主意,只是不需要。

    【讨论】:

    • 任何涉及QtGui的都需要QApplication,其他的你需要QCoreApplication
    【解决方案2】:

    您可以像预览器一样搜索 Qt 示例,如下所示:

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

    【讨论】:

      猜你喜欢
      • 2015-02-24
      • 2015-06-06
      • 2013-04-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-02
      • 1970-01-01
      • 1970-01-01
      • 2011-10-17
      相关资源
      最近更新 更多