B
B
Burgul2014-06-08 06:03:56
Qt
Burgul, 2014-06-08 06:03:56

How to connect Webkit in Qt 5.3.0?

I'm trying to display a website using a webView:

void MainWindow::on_pushButton_clicked()
{
    ui->webView->load(QUrl("http://www.trolltech.com/"));

}

At the output I get errors:
13:53:45: Starting: "/usr/bin/make" 
g++ -Wl,-rpath,Qt5.3.0/5.3/gcc_64 -Wl,-rpath,/home/fenixsar/Qt5.3.0/5.3/gcc_64/lib -Wl,-rpath-link,Qt5.3.0/5.3/gcc_64/lib -o brouser main.o mainwindow.o moc_mainwindow.o   -L./Qt5.3.0/5.3/gcc_64/lib -lQt5WebKit -lQt5Widgets -lQt5Network -lQt5Gui -lQt5Core -lGL -lpthread 
mainwindow.o: In function `MainWindow::on_pushButton_clicked()':
/Qt5.3.0/Tools/QtCreator/bin/build-brouser-Desktop_Qt_5_3_0_GCC_64bit-Debug/../brouser/mainwindow.cpp:27: undefined reference to `QWebView::load(QUrl const&)'
mainwindow.o: In function `Ui_MainWindow::setupUi(QMainWindow*)':
/Qt5.3.0/Tools/QtCreator/bin/build-brouser-Desktop_Qt_5_3_0_GCC_64bit-Debug/./ui_mainwindow.h:40: undefined reference to `QWebView::QWebView(QWidget*)'
/Qt5.3.0/Tools/QtCreator/bin/build-brouser-Desktop_Qt_5_3_0_GCC_64bit-Debug/./ui_mainwindow.h:43: undefined reference to `QWebView::setUrl(QUrl const&)'
collect2: error: ld returned 1 exit status
make: *** [brouser] Error 1
13:53:45: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project brouser (kit: Desktop Qt 5.3.0 GCC 64bit)
When executing step 'Make'
13:53:45: Elapsed time: 00:00.

Project file:
QT       += core gui webkit network

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = brouser
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui

Tried to include different libraries:
#include <QtWebKitWidgets/QWebView>
#include <QtWebKit>

By the way, he cannot find this one: Although the certificate says that she should be present.
#include <QWebView>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
EXL, 2014-06-08
@EXL

Tried to include different libraries:
#include
#include
Firstly, who told you that libraries in C ++ are connected in this way? So only declarations of classes and their methods are connected. You will not connect libraries to the project in this way, and therefore you will get a natural linking error " undefined reference to ... ". This is completely logical, since you included declarations, but their implementation did not. By the way, you do not need these lines at all, delete them, they will not have the desired effect, since they are already in "ui_MainWindow.h", open this file and examine its header.
Secondly, you are using documentation that is too old, apparently for Qt 4, although you are using Qt 5. This is noticeable by:
and by:
no prefix. Update your documentation to the latest one or install Qt 4. Qt 5 has been split into many parts, Qt 4 is more monolithic. Therefore, WebView was moved to a separate webkitwidgets library in Qt 5.
Third, www.trolltech.com is long dead.
In your case, to get a fully functional application, you should define in the * .pro file:
and remove:
#include <QtWebKitWidgets/QWebView>
#include <QtWebKit>

From where you put it yourself.
A fully working application compiled on Qt 5 will look something like this:
project.pro:
#-------------------------------------------------
#
# Project created by QtCreator 2014-06-09T02:41:10
#
#-------------------------------------------------

QT       += core gui webkit webkitwidgets
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = untitled3
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();
}

mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;

private slots:
    void on_pushButton_clicked();
};

#endif // MAINWINDOW_H

mainwindow.cpp:
#include "MainWindow.h"
#include "ui_MainWindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    ui->webView->setUrl(QUrl("http://www.habrahabr.ru"));
}

Good luck!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question