2013-05-02 107 views
3

我已经创建了一个Qt GUI应用程序,但我没有触及任何关于GUI的东西。我修改了mainwindow.cpp和项目文件。使用QWebPage获取运行时错误

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.pro:

#------------------------------------------------- 
# 
# 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-Debug\debug\untitled.exe 
Module: 5.0.2 
File: global\qglobal.cpp 
Line: 1977 

ASSERT: "!"No style available without QApplication!"" in file kernel\qapplication.cpp, line 962 

(Press Retry to debug the application) 
--------------------------- 
Abort Retry Ignore 
--------------------------- 

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

回答

1

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

QApplication app; 

// Below you can then create the window 

编辑

的问题是,你正在创建一个QWebPage作为全球对象,并且在创建QApplication之前。要解决该问题,请将该页面设为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; 
} 
+0

我有一个已经。我会发布main.cpp – user2341549 2013-05-02 03:56:48

+0

@ user2341549,我已经编辑了我的答案。 – 2013-05-02 06:30:16

+0

谢谢!非常感激。 – user2341549 2013-05-02 22:39:44