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. Implementation of displaying a dynamic data in listview on click of a button
Forum Updated to NodeBB v4.3 + New Features

Implementation of displaying a dynamic data in listview on click of a button

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
1 Posts 1 Posters 258 Views
  • 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.
  • P Offline
    P Offline
    Praveen.Illa
    wrote on last edited by
    #1

    Hi Team,

    I am new to Qt QML world, and am struggling to implement a requirement of displaying a dynamic data in listview on click of a button. Can any one help me on this ?

    Requirement: On button click, the parameters are passed to C++ code, fetches the dynamic data from backend or middleware and displays it on qml listview.

    main.cpp
    int main(int argc, char *argv[])
    {
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);
    
    QQmlApplicationEngine engine;
    const QUrl url(QStringLiteral("qrc:/main.qml"));
    /*QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);*/
    
    engine.rootContext()->setContextProperty("xyzModel", new xyzModel());
    engine.load(url);
    
    return app.exec();
    

    }

    xyzModel.h
    class xyzModel : public QAbstractListModel
    {
    Q_OBJECT
    public:
    explicit xyzModel();

    enum PARAMS {
        DESCRIPTION = 0,
        EDITABLE,
    };
    
    // QAbstractItemModel interface
    int rowCount(const QModelIndex &parent) const override;
    QVariant data(const QModelIndex &index, int role) const override;
    QHash<int, QByteArray> roleNames() const override;
    
    // QtableName and tableNo will be sent from the QML
    Q_INVOKABLE void xyzTableParams(QString QtableName, int tableNo);
    

    public slots:

    private:
    QList <xyzTable *> m_list;

    };

    xyzModel.cpp
    void xyzModel::xyzTableParams(QString QtableName, int tableNo)
    {
    // get_table_data returns a structure from backend or middleware
    int status = get_table_data(QtableName.toLatin1().data(), tableNo);

    //get_item_count returns number of items from backend or middleware
    int mTotalObjectCount = get_item_count();
    
    for(int iLoop = 0; iLoop < mTotalObjectCount; iLoop++)
    {
        // get_item_data_address returns the structure from backend or middleware
        xyz_item_data* p_item_data =  get_item_data_address(iLoop);
    
        xyzTable *xyztableObj = new xyzTable;
        xyztableObj->setdescription(QString((char*)p_item_data->description));
        xyztableObj->seteditable(p_item_data->editable);
        m_list.append(xyztableObj);
    }
    

    }

    xyzTable.h
    class xyzTable
    {
    public:
    xyzTable();

    QString description() const;
    void setdescription(const QString &description);
    
    unsigned char editable() const;
    void seteditable(unsigned char editable);
    

    private:
    QString m_description;
    unsigned char m_editable;
    };

    xyzTable.cpp
    xyzTable::xyzTable()
    {

    }

    QString xyzTable::description() const
    {
    return m_description;
    }

    void xyzTable::setdescription(const QString &description)
    {
    m_description = description;
    }

    unsigned char xyzTable::editable() const
    {
    return m_editable;
    }

    void xyzTable::seteditable(unsigned char editable)
    {
    m_editable = editable;
    }

    main.qml
    Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")

    Button {
        id: btnId
        text: "TST_CGF1"
        onClicked: ccnModel.ccnTableParams("TST_CGF1", 2)
    }
    
    ListView {
        id: lv
        width: parent.width
        height: 400
        anchors.left: parent.left
        anchors.top: btnId.bottom
        model: ccnModel
        delegate: del
        focus: true
    }
    
    Component {
        id: del
    
        Rectangle {
            width: 300; height: 50; color: "black"
            Row {
                spacing: 10
                anchors.centerIn: parent
    
                Text {
                    color: "white"
                    text: Description
                    font.pixelSize: 20
                }
                Text {
                    color: "white"
                    text: Editable
                    font.pixelSize: 20
                }
            }
        }
    }
    

    Can anyone help me in understanding where am doing wrong or can provide some sudo code how to be shown the dynamic data on list view on click of a button

    1 Reply Last reply
    0

    • Login

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