简体   繁体   English

如何继承 QML Window?

[英]How to subclass QML Window?

I need to add some features in c++.我需要在 c++ 中添加一些功能。 But I struggle how to properly create my own QML window type.但是我很难正确创建自己的 QML window 类型。 I have tried to subclass QQuickWindow and register my new type and use it in My QML project.我试图继承 QQuickWindow 并注册我的新类型并在我的 QML 项目中使用它。 But when starting it show error, that I can not set opacity但是当启动它时显示错误,我无法设置不透明度

mywindow.h我的窗口.h

#include <QQuickItem>
#include <QQuickWindow>
#include <QWindow>
#include <QApplication>
#include <QObject> 
class MyWindow : public QQuickWindow {
   Q_OBJECT
public:
   MyWindow(QQuickWindow *parent=nullptr); 
public slots:
   Q_INVOKABLE void mycppFeature();

mywindow.cpp我的窗口.cpp

#include "reminderwindow.h"
MyWindow::MyWindow(QQuickWindow *parent):QQuickWindow(parent){
}

main.cpp主文件

qmlRegisterType<MyWindow>("com.organization.my", 1, 0, "MyWindow");

SplashWindow.qml SplashWindow.qml

import QtQuick 2.0
import QtQuick.Controls 2.5
import QtQuick.Window 2.15
import com.organization.my 1.0
MyWindow{
   opacity: 0.8

MyWindow is find but the error is "MyWindow.opacity" is not available in com.organizatino.my 1.0. MyWindow 已找到,但错误是“MyWindow.opacity”在 com.organizatino.my 1.0 中不可用。 I believe I do not know how to properly subclass the QML Window type.我相信我不知道如何正确地继承 QML Window 类型。 I use it besides the main ApplicationWindow When I use it without opacity, it works properly我在主 ApplicationWindow 之外使用它当我在没有不透明度的情况下使用它时,它可以正常工作

Add this to your MyWindow class declaration:将此添加到您的 MyWindow class 声明中:

QML_FOREIGN(QQuickWindow)

The opacity property is declared on QWindow (inherited by QQuickWindow) like this: opacity 属性在 QWindow(由 QQuickWindow 继承)上声明,如下所示:

    Q_PROPERTY(qreal opacity READ opacity WRITE setOpacity NOTIFY opacityChanged REVISION(2, 1))

Meaning it was added in version 2.1 of the QtQuick.Window QML module.这意味着它是在QtQuick.Window QML 模块的 2.1 版中添加的。

The QML_FOREIGN documentation states: QML_FOREIGN 文档指出:

Declares that any QML_ELEMENT, QML_NAMED_ELEMENT(), QML_ANONYMOUS, QML_INTERFACE, QML_UNCREATABLE(), QML_SINGLETON, QML_ADDED_IN_MINOR_VERSION(), QML_REMOVED_IN_MINOR_VERSION(), QML_ATTACHED(), or QML_EXTENDED() macros in the enclosing C++ type do not apply to the enclosing type but instead to FOREIGN_TYPE. Declares that any QML_ELEMENT, QML_NAMED_ELEMENT(), QML_ANONYMOUS, QML_INTERFACE, QML_UNCREATABLE(), QML_SINGLETON, QML_ADDED_IN_MINOR_VERSION(), QML_REMOVED_IN_MINOR_VERSION(), QML_ATTACHED(), or QML_EXTENDED() macros in the enclosing C++ type do not apply to the enclosing type but instead到 FOREIGN_TYPE。 The enclosing type still needs to be registered with the meta object system using a Q_GADGET or Q_OBJECT macro.封闭类型仍然需要使用 Q_GADGET 或 Q_OBJECT 宏向 meta object 系统注册。

So that tells the QML machinery to not validate the version restrictions against your module but against the one that QQuickWindow is provided in.因此,这告诉 QML 机器不要验证针对您的模块的版本限制,而是针对提供 QQuickWindow 的模块。

More discussion in these bug reports similar to yours:这些错误报告中的更多讨论与您的类似:

https://bugreports.qt.io/browse/QTBUG-91706 https://bugreports.qt.io/browse/QTBUG-91706

https://bugreports.qt.io/browse/QTBUG-72986 https://bugreports.qt.io/browse/QTBUG-72986

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

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