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

标签 c++ qt signals qml

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

我的问题是,我无法向列表的委托(delegate)发送信号,以便它知道何时展开。我已经尝试了很多东西。

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

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)完成时对象是否可用。所以我做了:

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 指针作为参数。

我用来将委托(delegate)内的信号连接到 C++ 中的信号的指针

收集器.h

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

    signals:
    void showCommentsButtonClicked();
}

收集器.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")

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

关于c++ - QML - 将信号从 C++ 连接到 qml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29535722/

相关文章:

c++ - 如何确定 VMT 指针的计数

c++ - 使用 Phonon 播放 mp4 视频

c++ - 计算装配错误率的工具

c++ - 如何阻止 Qt 的 setStyleSheet 重置我的表单和按钮上的指定颜色?

c++ - qt 背景样式表不起作用

c - 让进程等待它的 'brothers' 进程

process - VHDL : Multiple rising_edge detections inside a process block

c++ - 世界语言联盟。 Visual Studio 2015 中的空输出文件

c++ - 库在 Qt 中链接失败

c - 在 C 中使用 SIGALRM 强制函数结束