1

I need to add some features in c++. But I struggle how to properly create my own QML window type. I have tried to subclass QQuickWindow and register my new type and use it in My QML project. But when starting it show error, that I can not set opacity

mywindow.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

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

main.cpp

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

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. I believe I do not know how to properly subclass the QML Window type. I use it besides the main ApplicationWindow When I use it without opacity, it works properly

  • I guess that `QQuickWindow` has no property named `opacity`, you have to implement it by yourself. I think the better way is to extend the Window in QML, of course it depends on your real tergets. – folibis May 13 '21 at 08:24
  • I was thinking that QML Window type is the same as C++ QQuickWindow. Am I wrong? – Andrej Fogelton May 13 '21 at 11:14

1 Answers1

0

Add this to your MyWindow class declaration:

QML_FOREIGN(QQuickWindow)

The opacity property is declared on QWindow (inherited by QQuickWindow) like this:

    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.

The QML_FOREIGN documentation states:

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. The enclosing type still needs to be registered with the meta object system using a Q_GADGET or Q_OBJECT macro.

So that tells the QML machinery to not validate the version restrictions against your module but against the one that QQuickWindow is provided in.

More discussion in these bug reports similar to yours:

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

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

David K. Hess
  • 16,632
  • 2
  • 49
  • 73
  • thank you for explaining, the error persist. "MyWindow.opacity" is not available in com.organization.my 2.15. Interesting part is when I use setOpacity in cpp file, It works. I think now, it is a bug. I will mark your answer as correct – Andrej Fogelton May 13 '21 at 14:23
  • How about if you try registering it in "QtQuick.Window" instead of your custom one? – David K. Hess May 13 '21 at 14:29
  • Actually see this existing bug report: https://bugreports.qt.io/browse/QTBUG-91706 – edited to match. – David K. Hess May 13 '21 at 15:46
  • How to access the original Window object? Please provide more details. – Jimmy Chen Dec 23 '22 at 03:58
  • @JimmyChen if you have a new question not answered here, you should probably open your own new question. Make sure and include background information and your code to make it possible for people to help you. – David K. Hess Dec 23 '22 at 14:10