- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在体验新的 QQuickWidget。我如何在 QQuickWidget 和 C++ 之间进行交互?
C++
QQuickWidget *view = new QQuickWidget();
view->setSource(QUrl::fromLocalFile("myqml.qml"));
view->setProperty("test", 0);
myLayout->addWidget(view);
QML
import QtQuick 2.1
Rectangle {
id: mainWindow
width: parent.width
height: parent.height
Text {
id: text
width: mainWindow.width
font.pixelSize: 20
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
text: test
}
}
text: test
不起作用:ReferenceError: test is not defined
如何通过 C++ 为我的 QML 文件提供一些属性?
是否也可以在 C++ 中获取 Text 对象并更新其文本?
最佳答案
试一试:
view->rootContext()->setContextProperty("test", "some random text");
代替
view->setProperty("test", 0);
setProperty(name, val)
如果对象具有定义为 Q_PROPERTY
的属性 name
,则有效。
可以将 QObject
派生对象作为 view
的上下文属性传递:
class Controller : public QObject
{
Q_OBJECT
QString m_test;
public:
explicit Controller(QObject *parent = 0);
Q_PROPERTY(QString test READ test WRITE setTest NOTIFY testChanged)
QDate test() const
{
return m_test;
}
signals:
void testChanged(QString arg);
public slots:
void setTest(QDate arg)
{
if (m_test != arg) {
m_test = arg;
emit testChanged(arg);
}
}
};
Controller c;
view->rootContext()->setContextProperty("controller", &c);
Text {
id: text
width: mainWindow.width
font.pixelSize: 20
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
text: controller.test
}
Is it also possible to get the Text object in C++ and update its text?
一般来说,好像不是best approach -- 如果 c++
代码遵循模型- View 模式,则它不应该知道表示。
然而,如描述的那样是可能的here .
关于c++ - QQuickWidget与C++交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23912633/
我在使 QQuickWidget 背景透明时遇到问题。 我想在 QWidget 下放置一个 QQuickWidget。 QQuickWidget 使用源 qml 文件。当我使用 Qt 4.8 时,我使
我正在尝试使 QtVirtualKeyboard 示例与 QQuickWidget 而不是 QQuickView 一起使用。对于 QuickView,我使用以下 main.cpp 代码,这对我来说很好
我的应用程序中出现了 2 天无法解决的问题,所以我在这里发帖。我有用 pyqt5 (Qt 设计器)编写的 UI,主窗口的一部分由 QML 对象(在 QQuickWidget 内部)组成。 我的问题是刷
当用户在使用 QQuickWidget 时单击窗口上的其他位置时,ToolTip 和 ComboBox 组件不会关闭,但在使用 QQuickView 时它们会关闭(ToolTip 消失并且组合框的弹出
使用 QQuickWidget 和 qml 文件创建了一个 MapWidget,以放大给定的位置坐标。但是,只要坐标发生变化, map 就不会刷新。我正在尝试连接一个可以单击以更新 map 的按钮,但
我在我的 QT5 gui 中使用 QQuickwidget。我通过相应地添加 qml 文件在其上加载了 openstreet map 。通过在qml文件中添加一个函数,我使用QMetaObject::
我在我的程序中使用来自新 Qt 5.3 的 QQuickWidget。我想从 C++ 调用 QML 中的函数。QML 文件非常简单: import QtQuick 2.2 Rectangle {
我有一个应用程序,我想为它制作一个小动画。我做了一个 qml 文件并使用 QQuickWidget 打开并在我的显示器中显示它。现在我想在 C++ 和 QML 之间进行迭代。例如,当调用 c++ 中的
我使用Python和Pyside2,我尝试在Qwidget内或布局内插入QQuickWidget,但我还没有找到解决方案。我尝试使用以下代码: view = QQuickWidget() view.s
我是一名优秀的程序员,十分优秀!