我想创建继承QQuickPath
的新类MyPath
。据我所知,QQuickPath
是私有(private)类。有什么“干净”的方法吗?
我的意图是创建自定义 QML 类型 MyPath
,它使用 QQuickPath
的 QPainterPath
属性(根据 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/