Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to add Item to ListView with C++?
Forum Updated to NodeBB v4.3 + New Features

How to add Item to ListView with C++?

Scheduled Pinned Locked Moved QML and Qt Quick
1 Posts 1 Posters 1.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BBFAG3
    wrote on 14 Dec 2013, 14:33 last edited by
    #1

    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! :)

    1 Reply Last reply
    0

    1/1

    14 Dec 2013, 14:33

    • Login

    • Login or register to search.
    1 out of 1
    • First post
      1/1
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved