c++ - Qt:如何继承QQuickPath类

标签 c++ qt qml

我想创建继承QQuickPath 的新类MyPath。据我所知,QQuickPath 是私有(private)类。有什么“干净”的方法吗?

我的意图是创建自定义 QML 类型 MyPath,它使用 QQuickPathQPainterPath 属性(根据 this link)具有一些扩展功能。

感谢您的帮助。

编辑:

Qt Creator 现在可以建议 QQuickPath 但编译失败。

我按照@michał-w-urbańczyk 发布的说明创建了此类标题

#ifndef C_H
#define C_H

#include <QtQuick/private/qquickpath_p.h>
#include <QObject>

class C : public QQuickPath
{
    Q_OBJECT
public:
    C(QObject *parent = 0);
    ~C();

signals:

public slots:
};

#endif // C_H

和源文件

 #include "c.h"

 C::C(QObject *parent)
  : QQuickPath(parent)
 {
 }

 C::~C()
 {
 }

我得到了这些错误:

c.o: In function `C::C(QObject*)':
../app/gui/c.cpp:4: undefined reference to `QQuickPath::QQuickPath(QObject*)'
c.o: In function `C::~C()':
../app/gui/c.cpp:8: undefined reference to `QQuickPath::~QQuickPath()'
moc_c.o: In function `C::qt_metacast(char const*)':
../app/moc_c.cpp:79: undefined reference to `QQuickPath::qt_metacast(char const*)'
moc_c.o: In function `C::qt_metacall(QMetaObject::Call, int, void**)':
../app/moc_c.cpp:84: undefined reference to `QQuickPath::qt_metacall(QMetaObject::Call, int, void**)'
moc_c.o:(.data.rel.ro+0x0): undefined reference to `QQuickPath::staticMetaObject'
moc_c.o:(.data.rel.ro._ZTV1C[_ZTV1C]+0x70): undefined reference to `QQuickPath::componentComplete()'
moc_c.o:(.data.rel.ro._ZTV1C[_ZTV1C]+0x78): undefined reference to `QQuickPath::classBegin()'
moc_c.o:(.data.rel.ro._ZTV1C[_ZTV1C]+0xa0): undefined reference to `non-virtual thunk to QQuickPath::classBegin()'
moc_c.o:(.data.rel.ro._ZTV1C[_ZTV1C]+0xa8): undefined reference to `non-virtual thunk to QQuickPath::componentComplete()'
moc_c.o:(.data.rel.ro._ZTI1C[_ZTI1C]+0x10): undefined reference to `typeinfo for QQuickPath'

谢谢

最佳答案

简短回答:Qt 不支持这种情况,因此没有“干净”的方法来做到这一点。如果你真的有心,用Qt的developer build时是可以实现的。

长答案: Qt 确实提供了对访问其私有(private)头文件的支持(其中定义了像`QQuickPath1 这样的私有(private)类),但这还不够。显然,使用官方 Qt 二进制发行版时不可能继承私有(private) Qt 类,因为它似乎没有必要的 QQuickPath 相关方法可用。

然而,我能够通过从官方源代码包构建 Qt 来实现它。根据配置选项,可能会访问必要的方法。我已经使用传递给 configure-developer-build 选项构建了 Qt — 它显然定义了 QT_BUILD_INTERNAL 宏,该宏导出非Qt代码能够继承Qt的私有(private)类。

qglobal.h 中的以下评论似乎相关:

/*
   No, this is not an evil backdoor. QT_BUILD_INTERNAL just exports more symbols
   for Qt's internal unit tests. If you want slower loading times and more
   symbols that can vanish from version to version, feel free to define QT_BUILD_INTERNAL.
*/

The whole thing is possible but obviously not recommended, as your application will require custom Qt binaries build. If you're ready to accept such drawback (along the other drawbacks of using Qt private headers like loosing binary compatibility with future Qt 5 releases), build the Qt on your own with the said flag defined and follow the instructions below.

First you need to add appropriate info to the .pro file, so the qmake will add private include directories to the includes path. Along with quick build option you need to provide quick-private. Because of the QQuickPath include dependencies, you'll need to add qml-private and gui-private as well. In the end, you'll end with something like this in your .pro file:

QT += qml quick quick-private qml-private gui-private

If you are not using qmake to build your project, then you'll need to add the path to the folder with priovate includes manually. The private includes are in the locations like: <QtDir>/include/<ModuleName>/<QtVersion>/<ModuleName>/private

Then simply add the include with QQuickPath to one of your files and define the derived type:

#include <QtQuick/private/qquickpath_p.h>

class MyPath : public QQuickPath
{
    Q_OBJECT
public:
    // any additional code
};

关于c++ - Qt:如何继承QQuickPath类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28900894/

相关文章:

c++ - 字符数组被分解为 3 个整型变量

c++ - 使用默认构造函数从类中调用对象

c++ - 如何在 Cascades、Blackberry 10 中使用 Qt/QML/C++ 从另一个 qml 文件更改一个 qml 文件中的标签文本?

qt - 调整pyqt tableview大小时如何裁剪列单元格的左侧

python - QtQuick GeometryChanged 方法仅适用于加载组件的一个方向

c++ - 如何管理关于子 QML TextEdit 的 QML ScrollView 导航?

c++ - 使用 Xcode 的综合工具包

C++ 转换运算符重载、枚举、整数和字符

c++ - 将十六进制转换为带符号的十进制

c++ - 如何从 qml Keys.onPressed 调用 qt keyPressEvent(QKeyEvent *event)