qml ListView结合cpp的model使用

14 篇文章 0 订阅
//.h
class Model : public QAbstractListModel
{
    Q_OBJECT

    struct ModelItem {
        QString name;
        int age;
    };
    enum {NAME=Qt::UserRole+1, AGE};

public:
    explicit Model(QObject *parent = 0);

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

private:
    QList<ModelItem> list;
};

//.cpp
Model::Model(QObject *parent) :
    QAbstractListModel(parent)
{
    ModelItem item;
    item.name = "aaaa";
    item.age = 34;
    list.push_back(item);

    item.name = "bbbbb";
    item.age = 33;
    list.push_back(item);

    item.name = "ccccc";
    item.age = 55;
    list.push_back(item);

    for(int i=0; i<100; i++)
    {
        item.name = QString::number(i);
        item.age = i;
        list.push_back(item);
    }
}

int Model::rowCount(const QModelIndex &) const
{
    return list.size();
}

QVariant Model::data(const QModelIndex &index, int role) const
{
    int i = index.row();
    if (i < 0 || i >= list.size())
        return QVariant();

    if (role == NAME)
        return list[i].name;

    if (role==AGE)
        return list[i].age;

    //if (role==Qt::DisplayRole)
    //    return list[i].name + " ("+ QString::number(list[i].age) +")";

    return QVariant();
}

QHash<int, QByteArray> Model::roleNames() const
{
    QHash<int, QByteArray> r =QAbstractListModel::roleNames();
    r[NAME] = "name";
    r[AGE] = "age";
    return r;
}

//qml
Window {
    visible: true
    width: 480
    height: 360
    title: qsTr("Hello World")
    id : root
    objectName: "root"
    property int currentIndex : 0


    ListView {
        id : listview
        anchors.fill: parent
        model: modcpp
        delegate: id_delegate
        currentIndex: root.currentIndex
    }

    Component{
        id : id_delegate
        Item {
            id : wrapper
            width: parent.width
            height: 57

            Column{
                width: parent.width
                height: parent.height

                Row{
                    width: parent.width
                    height: parent.height - 1

                    Rectangle{
                        id :id_icon
                        width : parent.width / 2
                        height: parent.height
                        color: "transparent"
                        Text {
                            anchors.fill: parent
                            text: "名字:" + name

                            color: currentIndex===index ? "red" : "black"
                            font.pixelSize: currentIndex===index ? 22 : 18

                            horizontalAlignment: Text.AlignLeft
                            verticalAlignment: Text.AlignVCenter
                        }
                        MouseArea{
                            anchors.fill: parent
                            onClicked: {
                                root.currentIndex = index
                            }
                        }
                    }

                    Rectangle{
                        width : parent.width / 2
                        height: parent.height
                        color: "transparent"
                        Text {
                            anchors.fill: parent
                            text: "年龄:" + age

                            color: currentIndex===index ? "red" : "black"
                            font.pixelSize: currentIndex===index ? 22 : 18

                            horizontalAlignment: Text.AlignRight
                            verticalAlignment: Text.AlignVCenter
                        }


                        MouseArea{
                            anchors.fill: parent
                            onClicked: {
                                root.currentIndex = index
                            }
                        }
                    }
                }

                Rectangle{
                    width: parent.width
                    height: 1
                    color: "#6E7489"
                }
            }
        }
    }
}

运行测试,效果如下:
在这里插入图片描述
资源链接:点击跳转

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值