简体   繁体   中英

How to include webview in existing Qt project?

I am trying to include one of the following libraries:

#include <QtWebView>
#include <QWebView>
#include <QtWebEngineWidgets>
#include <WebEngineCore>
#include <QtWebEngine>

Each time I add one of its includes an error appears in my code. However, I use Qt 6.3.1 and I find files that correspond to the includes in my system installation folder under macOS. I use a cmake in my qt project and not a file.pro or qmake.

Ultimately, I want to display a web form in my UI.

Maybe you need to add WebEngineWidgets module to your cmake file

find_package(Qt6 REQUIRED COMPONENTS WebEngineWidgets)
target_link_libraries(mytarget PRIVATE Qt6::WebEngineWidgets)

then #include <QWebEngineView>

https://doc.qt.io/qt-6/qwebengineview.html

You need to make sure that you install the QtWebEngine module when installing Qt.

在此处输入图像描述

Then, in your CMakeLists.txt , you would write something like this below.

Please note that you should use versionless targets as recommended by the Qt Project , ie do not use Qt6::WebEngineWidgets as that would have portability issues.

find_package(Qt6 COMPONENTS WebEngineWidgets REQUIRED)
target_link_libraries(YourTarget Qt::WebEngineWidgets)
add_executable(YourTarget main.cpp)

Then, you can just write something like this in your main.cpp :

#include <QApplication>
#include <QWebEngineView>

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

    QWebEngineView view;
    view.setUrl(QUrl(QStringLiteral("https://www.qt.io")));
    view.show();

    return app.exec();
}

Please refer to the official examples for further details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM