使用QWebPage获取运行时错误

Getting runtime errors with QWebPage

本文关键字:运行时错误 获取 QWebPage 使用      更新时间:2023-10-16

我已经创建了一个Qt GUI应用程序,但我还没有涉及任何有关GUI的内容。我已经修改了mainwindow.cpp和项目文件。

mainwindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QWebPage>
#include <QWebFrame>
QWebPage page;
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(page.mainFrame(), SIGNAL(loadFinished(bool)), this, SLOT(pageLoaded1(bool)));
    QUrl router("http://192.168.1.1");
    page.mainFrame()->load(router);
}
MainWindow::~MainWindow()
{
    delete ui;
}

untitled.pr:

#-------------------------------------------------
#
# Project created by QtCreator 2013-05-01T23:48:00
#
#-------------------------------------------------
QT       += core gui webkit webkitwidgets
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = untitled
TEMPLATE = app

SOURCES += main.cpp
        mainwindow.cpp
HEADERS  += mainwindow.h
FORMS    += mainwindow.ui

main.cpp:

#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

错误:

---------------------------
Microsoft Visual C++ Debug Library
---------------------------
Debug Error!
Program: ...tled-Desktop_Qt_5_0_2_MSVC2010_32bit-Debugdebuguntitled.exe
Module: 5.0.2
File: globalqglobal.cpp
Line: 1977
ASSERT: "!"No style available without QApplication!"" in file kernelqapplication.cpp, line 962
(Press Retry to debug the application)
---------------------------
Abort   Retry   Ignore   
---------------------------

此处插入额外字符以绕过字符要求。

main.cpp中,确保创建一个应用程序对象,即使不直接使用:

QApplication app;
// Below you can then create the window

编辑

问题是,在创建QApplication之前,您正在将QWebPage创建为全局对象。若要解决此问题,请使页面成为MainWindow类的成员。还要使页面成为指针,否则您将遇到其他问题。

即在mainwindow.h:中

private:
    QWebPage* page;

mainwindow.cpp:中

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QWebPage>
#include <QWebFrame>
// Remove this!!
// QWebPage page;
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    // Create the page here:
    page = new QWebPage(this);
    connect(page.mainFrame(), SIGNAL(loadFinished(bool)), this, SLOT(pageLoaded1(bool)));
    QUrl router("http://192.168.1.1");
    page.mainFrame()->load(router);
}
MainWindow::~MainWindow()
{
    delete ui;
}