简体   繁体   English

Qt:如何继承QQuickPath类

[英]Qt: How to inherit QQuickPath class

I would like to create new class MyPath which inherits QQuickPath . 我想创建继承QQuickPath新类MyPath As far as I found out QQuickPath is private class. 据我QQuickPath是私有类。 Is there any "clean" way of doing it? 这样做有“干净”的方法吗?

My intent is creating custom QML type MyPath which has some extended functionality using QPainterPath atribute of QQuickPath (according to this link ). 我的目的是创建自定义QML类型MyPath ,它使用QQuickPath QPainterPath QQuickPath (根据此链接 )具有一些扩展功能。

Thanks for help. 感谢帮助。

Edit: 编辑:

Qt Creator is now able to suggest QQuickPath but compilation fails. Qt Creator现在能够推荐QQuickPath但编译失败。

I followed instructions posted by @michał-w-urbańczyk and have created this class header 我按照@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

and source file 和源文件

 #include "c.h"

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

 C::~C()
 {
 }

and I got these errors: 我收到了这些错误:

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'

Thanks 谢谢

Short answer: such scenario is not supported by Qt so there is no "clean" way to do that. 简短的回答:Qt不支持这种情况,因此没有“干净”的方法来做到这一点。 If you are really determined, it is possible to achieve when using the developer build of Qt. 如果你真的有决心,可以在使用Qt的开发者版本时实现。

Long answer: Qt does provide a support for accessing its private headers (where are the private classes like `QQuickPath1 defined) but it is not enough. 答案很长:Qt确实提供了访问其私有标头的支持(私有类,如`QQuickPath1定义),但这还不够。 Apparently it is not possible to inherit a private Qt class when using the official Qt binary distribution, as it does not seem to have necessary QQuickPath-related methods available. 显然,在使用官方Qt二进制分发时,不可能继承私有Qt类,因为它似乎没有必要的QQuickPath相关方法。

I was however able to get this working by building the Qt from the official source code package. 然而,我能够通过从官方源代码包构建Qt来实现这一点。 Depending on the configure options, the necessary methods might become accessible. 根据配置选项,可以访问必要的方法。 I have built Qt with the -developer-build option passed to configure — it apparently defines the QT_BUILD_INTERNAL macro that exports symbols necessary for the non-Qt-code to be able to inherit Qt private classes. 我已经使用传递给configure-developer-build选项构建了Qt - 它显然定义了QT_BUILD_INTERNAL宏,它导出非Qt代码所需的符号,以便能够继承Qt私有类。

The following comment from qglobal.h seems relevant: 来自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. 整个事情是可能的,但显然不推荐,因为您的应用程序将需要自定义Qt二进制文件构建。 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. 如果您已经准备好接受这样的缺点(除了使用Qt私有标头的其他缺点,例如在将来的Qt 5版本中失去二进制兼容性),请自行构建Qt并定义所述标志,并按照以下说明操作。

First you need to add appropriate info to the .pro file, so the qmake will add private include directories to the includes path. 首先,您需要向.pro文件添加适当的信息,以便qmake将私有包含目录添加到包含路径。 Along with quick build option you need to provide quick-private . 除了quick构建选项,您还需要提供quick-private Because of the QQuickPath include dependencies, you'll need to add qml-private and gui-private as well. 由于QQuickPath包含依赖项,您还需要添加qml-privategui-private In the end, you'll end with something like this in your .pro file: 最后,您将在.pro文件中以这样的结尾结束:

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. 如果您没有使用qmake来构建项目,那么您需要手动添加priovate包含的文件夹路径。 The private includes are in the locations like: <QtDir>/include/<ModuleName>/<QtVersion>/<ModuleName>/private 私有包含位于以下位置: <QtDir>/include/<ModuleName>/<QtVersion>/<ModuleName>/private

Then simply add the include with QQuickPath to one of your files and define the derived type: 然后只需将包含QQuickPath的include添加到您的某个文件中并定义派生类型:

#include <QtQuick/private/qquickpath_p.h>

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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