QML之ListView基本使用

以下实现在qml中显示listview,数据由c++提供,切qml可以修改list某项的值。

关键需要实现3个函数:

int rowCount(const QModelIndex &parent) const;
QVariant data(const QModelIndex &index, int role) const;
QHash<int, QByteArray> roleNames() const;

main.cpp

#include <QApplication>
#include "qtquick1applicationviewer.h"
#include <qdeclarativecontext.h>
#include "medialistmodel.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QtQuick1ApplicationViewer viewer;
    viewer.addImportPath(QLatin1String("modules"));
    viewer.setOrientation(QtQuick1ApplicationViewer::ScreenOrientationAuto);
    MediaListModel* pMedialist = new MediaListModel();
    viewer.rootContext()->setContextProperty("myListModel", pMedialist);

    viewer.setMainQmlFile(QLatin1String("qml/listview/main.qml"));
    viewer.showExpanded();

    return app.exec();
}

main.qml

import QtQuick 1.1

Rectangle {
    width: 800
    height: 480

    Component {
        id: listDelegate;

        Text {
            id: listname;
            x:0
            width: 400;
            height: 32;
            font.pointSize: 15;
            verticalAlignment: Text.AlignVCenter;
            horizontalAlignment: Text.AlignHCenter;
            text: name;
            color: ListView.view.currentIndex == index ? "red" : "blue";
            MouseArea {
                anchors.fill: parent;
                onClicked: {
                    console.log("click index ", index)
                }
            }
        }

    }

    ListView {
        id: dynamicList;
        width: 800
        height: 300
        anchors.margins: 10;

        delegate: listDelegate;
        model: myListModel;

        onFlickStarted: {

        }

        onFlickEnded: {

        }
    }

    Rectangle{
        x:30
        y:360
        width: 100
        height: 50
        color: "blue"

        MouseArea {
            anchors.fill: parent;
            onClicked: {
                myListModel.changeName(1)
            }
        }
    }

    Rectangle{
        x:230
        y:360
        width: 100
        height: 50
        color: "red"

        MouseArea {
            anchors.fill: parent;
            onClicked: {
                myListModel.changeName(2)
            }
        }
    }

}

medialistmodel.h

#ifndef MEDIALISTMODEL_H
#define MEDIALISTMODEL_H

#include <QAbstractListModel>
#include <QList>
#include <QString>
#include <QHash>
#include <QByteArray>

class Element{
public:

    Element(QString name_, int id_, int age_){
        name = name_;
        id = id_;
        age = age_;
    }

    QString name;
    int id;
    int age;
};

class MediaListModel : public QAbstractListModel
{
    Q_OBJECT
public:
    enum LineInfo_Roles{
        RoleId =Qt::UserRole + 1,
        RoleName,
        RoleAge
    };
public:
    explicit MediaListModel(QObject *parent = 0);

    int rowCount(const QModelIndex &parent) const;
    QVariant data(const QModelIndex &index, int role) const;
    QHash<int, QByteArray> roleNames() const;

    Q_INVOKABLE void changeName(int index);

signals:

public slots:

private:
    QList<Element> m_display_list;
    QHash<int, QByteArray> m_roleNames;
};

#endif // MEDIALISTMODEL_H

medialistmodel.cpp

#include "medialistmodel.h"

MediaListModel::MediaListModel(QObject *parent) :
    QAbstractListModel(parent)
{
    m_display_list.clear();
    m_roleNames.insert(RoleId, "id");
    m_roleNames.insert(RoleName, "name");
    m_roleNames.insert(RoleAge, "age");

    m_display_list.push_back(Element("wang", 1, 20));
    m_display_list.push_back(Element("liu", 2, 20));
    m_display_list.push_back(Element("zhang", 3, 20));
    m_display_list.push_back(Element("weo", 4, 20));
    m_display_list.push_back(Element("say", 5, 20));
    m_display_list.push_back(Element("bye", 6, 20));
    m_display_list.push_back(Element("why", 7, 20));

}


int MediaListModel::rowCount(const QModelIndex &parent) const
{
    return m_display_list.size();
}

QVariant MediaListModel::data(const QModelIndex &index, int role) const
{
    Element ele = m_display_list.at(index.row());
    QVariant var;
    switch (role) {
    case RoleId:
        var = ele.id;
        break;
    case RoleName:
        var = ele.name;
        break;
    case RoleAge:
        var = ele.age;
        break;
    default:
        break;
    }

    return var;
}

QHash<int, QByteArray> MediaListModel::roleNames() const
{
    return m_roleNames;
}

void MediaListModel::changeName(int index)
{
    if(index == 1){
        Element ele1("change1", 1, 2);
        m_display_list[index] = ele1;
    }

    if(index == 2){
        Element ele2("change2", 2, 3);
        m_display_list[index] = ele2;
    }

    emit dataChanged(createIndex(index,0), createIndex(index,0));
}

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值