- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
让我们在 A.qml 中定义一个以下组件:
Item {
function test() { console.log("this is a test") }
}
在 B.qml 中,我们执行以下操作:
A {
function test2() { console.log("this is a second test") }
}
在 C++ 中,我创建了一个 QQuickView 并将源设置为 B.qml。现在我尝试调用“测试”,但它失败了,并显示一条错误消息,指出“测试在 B 中不存在”。如果我调用“test2”,一切都会按预期进行。如果我为以下内容更改 test2:
A {
function test2() { test() }
}
调用 test() 成功。
问题是,当它被定义为 A.qml 组件时,如何直接从 C++ 调用 test() 函数?
注意:我使用的是 Qt 5.7.1
最佳答案
我已尝试在 Windows 7 上使用 Qt 5.9.1 重现您描述的问题,但无法重现。我可以从 C++ 调用 test() 和 test2()。
因此,如果您需要更多帮助,您需要发布更多代码来帮助重现问题。
我的代码如下...
import QtQuick 2.7
Item {
function test() { console.log("this is a test") }
}
import QtQuick 2.7
import QtQuick.Controls 2.0
A {
id: root
function test2() { console.log("this is a second test") }
Button {
text: "click me"
onClicked: {
helper.invokeMethod(root, "test");
helper.invokeMethod(root, "test2");
}
}
}
#ifndef HELPER_H
#define HELPER_H
#include <QObject>
class Helper : public QObject
{
Q_OBJECT
public:
explicit Helper() { }
Q_INVOKABLE void invokeMethod(QObject* obj, QString method){
QMetaObject::invokeMethod(obj, method.toLatin1().constData());
}
signals:
public slots:
};
#endif // HELPER_H
#include <QGuiApplication>
#include <QQmlContext>
#include <QQuickView>
#include "helper.h"
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
Helper helper;
QQuickView *view = new QQuickView;
view->rootContext()->setContextProperty("helper", &helper);
view->setSource(QUrl("qrc:/B.qml"));
view->show();
return app.exec();
}
关于c++ - 从 C++ 调用组件中定义的 QML 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46635892/
如果下一个元素的宽度超过指定布局的宽度,是否有 QML 布局或某些配置会自动将 QML 项目包装到下一行? 当我使用 QML GridLayout ,项目刚好离开窗口的边缘并被剪裁: GridLayo
如果下一个元素的宽度超过指定布局的宽度,是否有 QML 布局或某些配置会自动将 QML 项目包装到下一行? 当我使用 QML GridLayout ,项目刚好离开窗口的边缘并被剪裁: GridLayo
如何在 qml 文件之间发送变量或信号? http://i.stack.imgur.com/MChCG.png Mainwindow -> 创建组件Item2.qml MainWindow -> 创建
我正在做一些事情,我有一个名为“FloatingMenu”的类(它应该在 C++ 中管理菜单)及其在文件 FloatingMenu.qml 中用于 GUI 的 QML alter-ego。我有一个文件
我正在尝试做一些看似简单的事情,但失败了:定义一个简单的内联文本格式组件,然后用不同的文本多次实例化它。这是代码 Item { . . . Component { id: favButtonL
我可以在页面中使用 InvokeActionItem 轻松共享项目,但我需要能够在 ListView 项目中调用它。我设法触发了一个调用,但我不知道如何在触发它时添加数据。我不断收到错误消息 Invo
我如何在 QML 中检测 Window {} 之外的点击? Rectangle { id: topLevel height: 400; width: 400 Window {
我试过 : var child = grid.children[slot1]; grid.children[slot1] = grid.children[slot2]; grid.children[s
例如,我希望创建一个包含 100 个文本编辑器的 qml 窗口,如何在循环中创建它?那可能吗? 最佳答案 循环是命令式代码,所以它不是 QML,而是 Javascript 或 C++。所以当然,你可以
这是我的 QML 文件,其中包含一个文本组件: import QtQuick 2.0 Item { id: idItmWrapText Text { id: idTxt
我正在寻找一种方法来显示一个文本提示,说明预期的输入作为对用户的建议。以谷歌搜索栏为例: 是否有我缺少的属性,或者这是必须通过脚本来实现的? 最佳答案 Qt Quick 输入项上不存在该属性。您可以为
为 qml 项设置背景的最简单方法是让子矩形的 anchor 完全填满父项: Item { width: 320 height: 240 Rectangle {
我想将属性动态添加到 QML 元素: Item { id: dynamicProperty; property int first; Component.onCompleted: { /*
我用 PySide 和 QML 编写了某种安装程序。按照设计,它必须是多页的。而且我想将要从 QML 表单调用的插槽划分为不同的对象(理想情况下,划分为模块,但据我了解,带有插槽的对象必须继承 QOb
QML 中有没有办法用 opacity: 0 创建一个矩形?仍然有可见的边界?如果没有,有关如何解决它的任何建议? 谢谢 最佳答案 不,不透明度适用于项目的完整视觉方面(并且不透明度:0 使项目完全不
属性变体 a:{}似乎不起作用。 a 最终未定义,而不是一个空字典。 我对 Javascript 不是很有经验...初始化属性以保存空字典的正确方法是什么? 以下 qml 在控制台上打印“qrc:/m
我在 SO 上查看了大量关于 QML 内容边距的问题,但所有答案都指向缺少 spacing: 0 属性。我已经完成了所有这些,但仍然有一些我无法消除的奇怪空间。任何人都可以解释为什么这个 QML 代码
我有一个用于样式定义的 QML 单例,定义如下: pragma Singleton import QtQuick 2.2 import QtQuick.Controls 1.1 import QtQu
这是以下代码的结果: 主.qml import QtQuick 2.8 Item { Reusable { index: 1234 // reusable with
属性变体 a:{}似乎不起作用。 a 最终未定义,而不是一个空字典。 我对 Javascript 不是很有经验...初始化属性以保存空字典的正确方法是什么? 以下 qml 在控制台上打印“qrc:/m
我是一名优秀的程序员,十分优秀!