繁体   English   中英

QML-将信号从C ++连接到QML

[英]QML - connect a signal from c++ to qml

我有一堆嵌套列表,链中最深的列表的高度为0。 因此,当用户单击一个按钮时,该列表将展开。

我的问题是,我无法向列表的代表发出信号,以便它知道何时扩展。 而且我尝试了很多东西。

我已经成功使用allready的一种方法使我失败,因为它找不到正确的对象

view->setSource(QUrl(QStringLiteral("qrc:/content/CommentNumberDelegate.qml")));
QObject *obj3 = view->rootObject()->findChild<QObject*>("commentNumberDelegate");
QObject::connect(&gather, SIGNAL(showCommentsButtonClicked()), obj3, SIGNAL(showCommentsButtonClicked()));

这给了我错误:

QObject::connect: Cannot connect DataGather::showCommentsButtonClicked() to (null)::showCommentsButtonClicked()

是的,我已经正确设置了我正在访问的文件的ObjectName属性(CommentNumberDelegate.qml)。

CommentNumberDelegate.qml:小版本

Rectangle {
    id: root
    objectName: "commentNumberDelegate"
    //width: parent.width + 20
    height: 0 //col1.childrenRect.height //Screen.height * 0.1 //300 //col1.childrenRect.height
    clip: true

    property alias delegateState: root.state
    signal showCommentsButtonClicked()
}

即使我以前已经成功连接了这样的信号,也无法使它正常工作。 此代码有效:

view->setSource(QUrl(QStringLiteral("qrc:/LoginPage.qml")));
QObject *obj = view->rootObject()->findChild<QObject*>("loginRectID");
QObject::connect(&gather, SIGNAL(loginSequenceFinished(QString)), obj, SIGNAL(loginSequenceFinished(QString)));

QObject *obj2 = view->rootObject()->findChild<QObject*>("mainRectID");
QObject::connect(&gather, SIGNAL(xmlFileCreationFinished()), obj, SIGNAL(xmlFileCreationFinished()));

当我使用调试器时,我可以看到QObject * obj3没有正确分配,因为我无法访问它。 在obj和ob2(工作代码)上使用调试器时,我可以清楚地看到该对象已正确分配,因为在分配给对象之后,我可以使用调试器对其进行访问。

我的问题是:我在这里可能做错了什么?

我尝试使对象失败的任何方式,都保持为空。

因此,我尝试查看在QML本身中,每次委托完成后该对象是否可用。 所以我做了:

delegate: CommentNumberDelegate {
    objectName: "commentNumberDelegateID"
    id: commentNumberDelegateID

    Component.onCompleted: {
    console.log("delegate completed = ", commentNumberDelegateID)
    gatherer.test123(commentNumberDelegateID) // this function passes the address to c++
}

输出:

qml: delegate completed =  CommentNumberDelegate_QMLTYPE_2(0x2bfa5748, "commentNumberDelegateID")
qml: delegate completed =  CommentNumberDelegate_QMLTYPE_2(0x2bf78d38, "commentNumberDelegateID")

因此,您可以看到组件完成时该对象确实可用。 所以我所做的就是将地址传递给一个名为test123的函数,该函数将QObject指针作为参数。

我曾经使用该指针将委托内部的信号连接到C ++中的信号

gatherer.h

class DataGather : public QObject
{
    ....
    Q_INVOKABLE void showCommentsButton() { emit showCommentsButtonClicked(); }
    Q_INVOKABLE void test123(QObject *obj);
    ....

    signals:
    void showCommentsButtonClicked();
}

gatherer.cpp

void DataGather::test123(QObject* obj)
{
    QObject::connect(this, SIGNAL(showCommentsButtonClicked()), obj, SIGNAL(showCommentsButtonClicked()));
}

因此,基本上,您需要做的是:

从QML调用showcomments按钮。 这将发出信号。 从QML您可以像这样捕获它:

commentNumberDelegate.qml:

Rectangle {
    id: commentNumberDelegate
    objectName: "commentNumberDelegate"
    width: parent.width + 20
    height: col1.childrenRect.height 
    clip: true

    signal showCommentsButtonClicked()

    onShowCommentsButtonClicked: console.log("showCommentsButtonClicked signal has been caught")

现在,我不确定这是否是正确的方法,但是它对我有用。

暂无
暂无

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

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