【问题标题】:QQuickWindow subclass, visibilityChanged and closing signalsQQuickWindow 子类,visibilityChanged 和关闭信号
【发布时间】:2017-12-05 08:57:24
【问题描述】:

我正在尝试创建自己的窗口类并在 QML 中使用它。在第一步中,我尝试以这种方式对 QQuickWindow 进行子类化:

class TestWindow : public QQuickWindow {
Q_OBJECT
...
};

然后在 main.cpp 文件中:

qmlRegisterType<TestWindow>("test.components", 1, 0, "TestWindow");

现在在 QML 中我可以简单地以这种方式使用它:

import test.components 1.0
TestWindow {
    //onClosing: console.log("closing")
    //onVisibilityChanged: console.log(visibility)
}

当我尝试取消注释其中一行时,就会出现问题。 QML 引擎说:““TestWindow.onVisibilityChanged”在 test.components 1.0 中不可用。”在“onClosing”的情况下也会发生类似的事情。我应该继承自 QWindow/QQuickWindow 但我还是尝试创建自己的。

  //connect(this, QQuickWindow::visibilityChanged, this, TestWindow::visibilityChanged);

并声明适当的 Q_PROPERTY 并定义 READ/WRITE 和 NOTIFY 部分。我用“closeEvent”替换了关闭信号并做了这样的事情:

//connect(this, SIGNAL(closing(QQuickCloseEvent*)), this, SIGNAL(closingEvent(QQuickCloseEvent*)));

这也很奇怪,因为当我尝试使用新的信号/插槽语法时,编译器会抱怨不完整的 QQuickCloseEvent,它没有在任何地方描述,似乎来自 Qt 内部。所以我不得不坚持使用旧的代码版本。

在这些处理之后,来自导入 QtQuick.Window 2.2 的另一个“窗口”(不是 TestWindow)说“x”和“y”属性未定义。我尝试修复的警告/错误越多,其他事情就越无法正常工作。

有人遇到过类似的问题吗?

编辑:我发现它与用于 QWindow 和 QQuickWindow 的某些信号/插槽/属性的 Q_REVISION 宏有关。我什至尝试使用这些行:

//qmlRegisterType<TestWindow>("test.components", 1, 0, "TestWindow");
//qmlRegisterType<TestWindow, 1>("test.components", 1, 0, "TestWindow");
qmlRegisterType<TestWindow, 2>("test.components", 1, 0, "TestWindow");

仍然没有成功。

【问题讨论】:

  • 为什么在 C++ 中继承 QQuickWindow?为什么不在 qml 中使用 QtQuick.Window?
  • 我需要提供一些可以在 C++ 中更有效地完成的附加功能。我只是在寻找好的解决方案(不仅仅是一个工作)子类化 QQuickWindow 似乎是个好主意。

标签: c++ qt qml


【解决方案1】:

我找到了解决办法。

  qmlRegisterType<TestWindow>("test.components", 1, 0, "TestWindow");
  qmlRegisterRevision<QWindow, 1>("test.components", 1, 0);
  qmlRegisterRevision<QQuickWindow, 1>("test.components", 1, 0);

我还必须注册基类的修订版。这里描述的解释:https://bugreports.qt.io/browse/QTBUG-22688在评论中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-19
    • 1970-01-01
    • 1970-01-01
    • 2016-07-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多