从 C++ 插件扩展现有的 QML 组件

从 C++ 插件扩展现有的 QML 组件

首先,我想说的是,我对 Qt 不是很有经验,但我想在现有的 QML WebView 组件中添加一个新功能,以设置请求新 URL 时要发送的标头值列表,包括 User Agent 标头。我认为执行此操作的方法是子类化 QWebView 类(例如“MyWebView”扩展 QWebView)并使用注册该类

qmlRegisterType<MyWebView>(uri, 1, 0, "MyWebView");

我创建了一个名为 MyApp 的“QML 扩展库 + Tabbed Touch UI”应用程序,用于在编码时使用预先创建的后端类来注册 MyWebView 类。到目前为止,我唯一做的事情就是通过以下方式创建第一个方法 .h 文件:

#ifndef MYWEBVIEW_H
#define MYWEBVIEW_H

#include <QtWebKitWidgets/QWebView>

class MyWebView : public QWebView
{
    Q_OBJECT
public:
    explicit MyWebView(QWebView *parent = 0);
};

#endif // MYWEBVIEW_H

使用这个.cpp

#include "mywebview.h"

MyWebView::MyWebView(QWebView *parent) :
    QWebView(parent)
{
}

我的笔记本电脑上没有任何设备,因此我仅使用 Raring Ringtail 桌面发行版进行开发。添加此额外文件后,编译可以进行,但当我尝试运行或调试项目(至少要查看默认的 MyApp.qml 是否正常工作)时,会显示以下错误:

正在启动 /usr/lib/x86_64-linux-gnu/qt5/bin/qmlscene...

file:///[DEBUG_PATH]/app/MyApp.qml:30 类型 HelloTab 不可用 file:///[DEBUG_PATH]/app/ui/HelloTab.qml:3 无法为模块“MyApp”加载插件:无法加载库 [DEBUG_PATH]/backend/modules/MyApp/libMyApp.so: ([DEBUG_PATH]/backend/modules/MyApp/libMyApp.so: 未定义符号:_ZN8QWebView16staticMetaObjectE)

所以我想...是不是配置不正确?你知道发生了什么吗?

我使用的 Ubuntu 版本可能是 64 位的吗?当我执行

ldd -r libMyApp.so

有很多行说 WebKit 类未定义:

    linux-vdso.so.1 =>  (0x00007ffffb5d1000)
    libQt5Qml.so.5 => /usr/lib/x86_64-linux-gnu/libQt5Qml.so.5 (0x00007ff9e1baa000)
    libQt5Core.so.5 => /usr/lib/x86_64-linux-gnu/libQt5Core.so.5 (0x00007ff9e1595000)
    libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007ff9e1291000)
    libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007ff9e107b000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007ff9e0cb3000)
    libQt5Network.so.5 => /usr/lib/x86_64-linux-gnu/libQt5Network.so.5 (0x00007ff9e097b000)
    libQt5V8.so.5 => /usr/lib/x86_64-linux-gnu/libQt5V8.so.5 (0x00007ff9e0337000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007ff9e0032000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007ff9dfe14000)
    libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007ff9dfbfd000)
    libicui18n.so.48 => /usr/lib/x86_64-linux-gnu/libicui18n.so.48 (0x00007ff9df837000)
    libicuuc.so.48 => /usr/lib/x86_64-linux-gnu/libicuuc.so.48 (0x00007ff9df4cb000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007ff9df2c7000)
    libglib-2.0.so.0 => /lib/x86_64-linux-gnu/libglib-2.0.so.0 (0x00007ff9defcb000)
    librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007ff9dedc2000)
    /lib64/ld-linux-x86-64.so.2 (0x00007ff9e2205000)
    libicudata.so.48 => /usr/lib/x86_64-linux-gnu/libicudata.so.48 (0x00007ff9dda52000)
    libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007ff9dd812000)
undefined symbol: _ZN8QWebView16staticMetaObjectE   (./libMyApp.so)
undefined symbol: _ZN8QWebView5eventEP6QEvent   (./libMyApp.so)
undefined symbol: _ZNK7QWidget7devTypeEv    (./libMyApp.so)
undefined symbol: _ZN7QWidget10setVisibleEb (./libMyApp.so)
undefined symbol: _ZNK8QWebView8sizeHintEv  (./libMyApp.so)
undefined symbol: _ZNK7QWidget15minimumSizeHintEv   (./libMyApp.so)
undefined symbol: _ZNK7QWidget14heightForWidthEi    (./libMyApp.so)
undefined symbol: _ZNK7QWidget17hasHeightForWidthEv (./libMyApp.so)
undefined symbol: _ZNK7QWidget11paintEngineEv   (./libMyApp.so)
undefined symbol: _ZN8QWebView15mousePressEventEP11QMouseEvent  (./libMyApp.so)
undefined symbol: _ZN8QWebView17mouseReleaseEventEP11QMouseEvent    (./libMyApp.so)
undefined symbol: _ZN8QWebView21mouseDoubleClickEventEP11QMouseEvent    (./libMyApp.so)
undefined symbol: _ZN8QWebView14mouseMoveEventEP11QMouseEvent   (./libMyApp.so)
undefined symbol: _ZN8QWebView10wheelEventEP11QWheelEvent   (./libMyApp.so)
undefined symbol: _ZN8QWebView13keyPressEventEP9QKeyEvent   (./libMyApp.so)
undefined symbol: _ZN8QWebView15keyReleaseEventEP9QKeyEvent (./libMyApp.so)
undefined symbol: _ZN8QWebView12focusInEventEP11QFocusEvent (./libMyApp.so)
undefined symbol: _ZN8QWebView13focusOutEventEP11QFocusEvent    (./libMyApp.so)
undefined symbol: _ZN7QWidget10enterEventEP6QEvent  (./libMyApp.so)
undefined symbol: _ZN7QWidget10leaveEventEP6QEvent  (./libMyApp.so)
undefined symbol: _ZN8QWebView10paintEventEP11QPaintEvent   (./libMyApp.so)
undefined symbol: _ZN7QWidget9moveEventEP10QMoveEvent   (./libMyApp.so)
undefined symbol: _ZN8QWebView11resizeEventEP12QResizeEvent (./libMyApp.so)
undefined symbol: _ZN7QWidget10closeEventEP11QCloseEvent    (./libMyApp.so)
undefined symbol: _ZN8QWebView16contextMenuEventEP17QContextMenuEvent   (./libMyApp.so)
undefined symbol: _ZN7QWidget11tabletEventEP12QTabletEvent  (./libMyApp.so)
undefined symbol: _ZN7QWidget11actionEventEP12QActionEvent  (./libMyApp.so)
undefined symbol: _ZN8QWebView14dragEnterEventEP15QDragEnterEvent   (./libMyApp.so)
undefined symbol: _ZN8QWebView13dragMoveEventEP14QDragMoveEvent (./libMyApp.so)
undefined symbol: _ZN8QWebView14dragLeaveEventEP15QDragLeaveEvent   (./libMyApp.so)
undefined symbol: _ZN8QWebView9dropEventEP10QDropEvent  (./libMyApp.so)
undefined symbol: _ZN7QWidget9showEventEP10QShowEvent   (./libMyApp.so)
undefined symbol: _ZN7QWidget9hideEventEP10QHideEvent   (./libMyApp.so)
undefined symbol: _ZN7QWidget11nativeEventERK10QByteArrayPvPl   (./libMyApp.so)
undefined symbol: _ZN8QWebView11changeEventEP6QEvent    (./libMyApp.so)
undefined symbol: _ZNK7QWidget6metricEN12QPaintDevice17PaintDeviceMetricE   (./libMyApp.so)
undefined symbol: _ZNK7QWidget11initPainterEP8QPainter  (./libMyApp.so)
undefined symbol: _ZNK7QWidget10redirectedEP6QPoint (./libMyApp.so)
undefined symbol: _ZNK7QWidget13sharedPainterEv (./libMyApp.so)
undefined symbol: _ZN8QWebView16inputMethodEventEP17QInputMethodEvent   (./libMyApp.so)
undefined symbol: _ZNK8QWebView16inputMethodQueryEN2Qt16InputMethodQueryE   (./libMyApp.so)
undefined symbol: _ZN8QWebView18focusNextPrevChildEb    (./libMyApp.so)
undefined symbol: _ZN8QWebView12createWindowEN8QWebPage13WebWindowTypeE (./libMyApp.so)
undefined symbol: _ZThn16_NK7QWidget7devTypeEv  (./libMyApp.so)
undefined symbol: _ZThn16_NK7QWidget11paintEngineEv (./libMyApp.so)
undefined symbol: _ZThn16_NK7QWidget6metricEN12QPaintDevice17PaintDeviceMetricE (./libMyApp.so)
undefined symbol: _ZThn16_NK7QWidget11initPainterEP8QPainter    (./libMyApp.so)
undefined symbol: _ZThn16_NK7QWidget10redirectedEP6QPoint   (./libMyApp.so)
undefined symbol: _ZThn16_NK7QWidget13sharedPainterEv   (./libMyApp.so)
undefined symbol: _ZTI8QWebView (./libMyApp.so)
undefined symbol: _ZN8QWebPage16staticMetaObjectE   (./libMyApp.so)
undefined symbol: _ZN8QWebPage5eventEP6QEvent   (./libMyApp.so)
undefined symbol: _ZN8QWebPage13triggerActionENS_9WebActionEb   (./libMyApp.so)
undefined symbol: _ZN8QWebPage9extensionENS_9ExtensionEPKNS_15ExtensionOptionEPNS_15ExtensionReturnE    (./libMyApp.so)
undefined symbol: _ZNK8QWebPage17supportsExtensionENS_9ExtensionE   (./libMyApp.so)
undefined symbol: _ZN8QWebPage25shouldInterruptJavaScriptEv (./libMyApp.so)
undefined symbol: _ZN8QWebPage12createWindowENS_13WebWindowTypeE    (./libMyApp.so)
undefined symbol: _ZN8QWebPage12createPluginERK7QStringRK4QUrlRK11QStringListS8_    (./libMyApp.so)
undefined symbol: _ZN8QWebPage23acceptNavigationRequestEP9QWebFrameRK15QNetworkRequestNS_14NavigationTypeE  (./libMyApp.so)
undefined symbol: _ZN8QWebPage10chooseFileEP9QWebFrameRK7QString    (./libMyApp.so)
undefined symbol: _ZN8QWebPage15javaScriptAlertEP9QWebFrameRK7QString   (./libMyApp.so)
undefined symbol: _ZN8QWebPage17javaScriptConfirmEP9QWebFrameRK7QString (./libMyApp.so)
undefined symbol: _ZN8QWebPage16javaScriptPromptEP9QWebFrameRK7QStringS4_PS2_   (./libMyApp.so)
undefined symbol: _ZN8QWebPage24javaScriptConsoleMessageERK7QStringiS2_ (./libMyApp.so)
undefined symbol: _ZNK8QWebPage15userAgentForUrlERK4QUrl    (./libMyApp.so)
undefined symbol: _ZTI8QWebPage (./libMyApp.so)
undefined symbol: _ZN8QWebView11qt_metacastEPKc (./libMyApp.so)
undefined symbol: _ZN8QWebViewD2Ev  (./libMyApp.so)
undefined symbol: _ZN8QWebView11qt_metacallEN11QMetaObject4CallEiPPv    (./libMyApp.so)
undefined symbol: _ZN8QWebPage11qt_metacallEN11QMetaObject4CallEiPPv    (./libMyApp.so)
undefined symbol: _ZN8QWebViewC2EP7QWidget  (./libMyApp.so)
undefined symbol: _ZN8QWebPageD2Ev  (./libMyApp.so)
undefined symbol: _ZN8QWebPage11qt_metacastEPKc (./libMyApp.so)
undefined symbol: _ZN8QWebPageC2EP7QObject  (./libMyApp.so)

答案1

解决方案是将此行添加MyApp.pro到后端模块的文件中

QT += webkit webkitwidgets

我以前试过这个,但直到现在才起作用。为什么?因为我libMyApp.so每次尝试构建项目时都没有建议不要生成后端模块文件...

更改后MyApp.pro需要删除此文件:

build-MyApp-Desktop-Debug/backend/modules/MyApp/libMyApp.so

这样,文件就会再次生成,但 webkit 依赖关系已经得到解决。

相关内容