How to add Item to ListView with C++?



  • I tried to write follow the example,but I have a factastic error here...

    #include "father.h"
    #include"QDebug"
    #include"QVariant"
    father::father(QObject *parent) :
    QObject(parent)
    {
    ctxt=viewer.rootContext();
    msgList.append("asdf");
    msgList.append("asdf");
    msgList.append("asdf");//add item to QStringList
    }

    void father::run()
    {
    ctxt->setContextProperty("msglist",QVariant::fromValue(msgList));//It works, 4*"asdf" display in ListView
    viewer.rootContext()->setContextProperty("sendMsg",new father);
    viewer.setMainQmlFile(QStringLiteral("qml/KnightLegend/main.qml"));
    viewer.showFullScreen();
    }

    void father::addmsg()
    {
    msgList.append("asd");
    ctxt->setContextProperty("msglist",QVariant::fromValue(msgList));//But This doesn't work,I'm sure is function has been called.
    return;
    }

    My QML:
    ListView
    {
    id:chat
    y:0
    width: (fwidth/3)*2
    height: cw.height-50

        model:msglist
        delegate: Text { text: modelData }
    
        Image//Background
        {
            anchors.fill:parent
            source:"qrc:Penguins.jpg"
            z:-2
        }
        MouseArea
        {
            anchors.fill: parent
        }
    }
    

    Thanks a lot! :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.