Why Qt signal will not work after use class as property in qml page?

ⅰ亾dé卋堺 提交于 2019-12-08 06:06:59

问题


I have a c++ class like below and i used this class after registering that in an qml page and that signals works well in qml page

class1.cpp

QString Class1::message(){
    return m_Message;
}
void Class1::setMessage(QString m){
    m_Message=m ;
    emit messageChanged();
}
void Class1::invokeEvent(){
    setMessage("The Second Message");
}
Class2* Class1::getClass2Obj(){
    class2obj.plusplusClickCount();
    return &class2obj;
}

class1.h

class Class1 : public QObject
{
    Q_OBJECT
    QString m_Message;
    Class2 class2obj;
public:
    explicit Class1(QObject *parent = nullptr);

    Q_PROPERTY(QString message READ message WRITE setMessage NOTIFY messageChanged)

    void setMessage(QString m);
    QString message();
    Q_INVOKABLE  void invokeEvent();
    Q_INVOKABLE Class2 *getClass2Obj();
signals:
    void messageChanged();
public slots:
};

I used this class in Page1.qml and signal work well and with getClass2Obj method i will return an object of Class2( that like Class1) and send it to Page2.qml as you see in below:

Page1.qml

Rectangle {
Class1{
    id:cl
    onMessageChanged: {
        txt.text=cl.message;
    }
}    
Button{
    onClicked: {
        cl.invokeEvent();//this will raise onMessageChanged and work well
    }
}
Button{
    onClicked: {
        mainWindow.stackView.push("/Page2.qml",{cl2:cl.getClass2Obj()})//here i send an object of class2 to page2.qml
    }
}
}

in the page2.qml now object of class2 has stord in cls2.and all are fine but just onMessageChanged not raisd.

Page2.qml

Rectangle {
property Class2 cl2: Class2{
    onMessageChanged: {
        console.log("On Message Changed Event From Class2")//will not work ! why?
        txt.text=message;//will not work ! why?
    }
}
Text {
    id: txt     
}
Button{
    onClicked: {
        cl2.invokeMessage("The Second Message From Page 2")
    }
}    
}

In page2.qml when i change the property Class2 cl2: Class2{} to Class2{} and get object with an alias property onMessageChanged will raise but i have another things in class2 object (like properties ans so on) that i need it.now i want to know how do this work well and why signal not work

来源:https://stackoverflow.com/questions/49024891/why-qt-signal-will-not-work-after-use-class-as-property-in-qml-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!