QML-ListView


1、例子1

import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0
import QtQuick.Window 2.2

Window {
    visible: true
    width: 300
    height: 450
    title: qsTr("Hello World")

    ListView{
        id:listView
        anchors.fill: parent
        model: 10
        snapMode: ListView.SnapOneItem
        orientation:ListView.Horizontal
        delegate: Rectangle{
              width: listView.width
              height: listView.height
              color: index%2 ? "red":"yellow"
              Label{
                  anchors.centerIn: parent
                  font.pointSize: 100
                  text: index
              }
        }
    }
}

 

 

2、例子2

import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0
import QtQuick.Window 2.2
ListView{
    id:listView
    anchors.fill: parent
    model: 10
    headerPositioning: ListView.PullBackHeader

    header: Rectangle{
        width: listView.width
        height: 70
        color: "green"
        Label{
            anchors.centerIn: parent
            text: "this is header"
        }
    }

    onCurrentIndexChanged: {
        console.log("current index = ",currentIndex)
    }

    delegate: Rectangle{
        width: listView.width
        height: 280
        color: index%2 ? "red":"yellow"
        Label{
            id:txt
            anchors.centerIn: parent
            font.pointSize: 100
            text: index
        }
        Label{
            anchors.top: txt.bottom
            font.pointSize: 30
            text: "currentIndex = " + listView.currentIndex
            anchors.horizontalCenter: parent.horizontalCenter
        }
    }
}

 3、例子3

 

 ListView {
            id: listView
            clip: true
            width: parent.width-1;
            height: parent.height;
            spacing :0
            highlight: Rectangle {
                width: listView.width
                height: 40
                color: "lightsteelblue"
                radius: 5
                Behavior on y {
                    SpringAnimation {
                        spring: 3
                        damping: 0.2
                    }
                }
            }
            highlightFollowsCurrentItem: true
            focus: true
            delegate: Rectangle {
                property color tempcolor: "transparent"
                color: tempcolor
                width: listView.width
                height: 40
                Row {
                    id: row1
                    anchors.fill: parent
                    spacing: 10
                    anchors.leftMargin: 10
                    Rectangle {
                        width: 30
                        height: 30
                        color: colorCode
                        radius: width/2;
                        anchors.verticalCenter: parent.verticalCenter

                    }
                    Text {
                        text: name
                        anchors.verticalCenter: parent.verticalCenter
                        font.bold: true
                        font.pointSize: listView.currentIndex == index ? 14 : 11
                        color: "white"
                    }
                }
                MouseArea{
                    anchors.fill: parent
                    hoverEnabled: true
                    onClicked: {
                        listView.currentIndex = index;
                    }
                    onEntered: {
                        tempcolor = "#a7e094"
                    }

                    onExited: {
                        tempcolor = "transparent"
                    }
                }
            }
            model: ListModel {
                ListElement {
                    name: "綜合方艙門"
                    colorCode: "green"
                }

                ListElement {
                    name: "網絡交換機"
                    colorCode: "red"
                }

                ListElement {
                    name: "設備"
                    colorCode: "green"
                }

                ListElement {
                    name: "設備"
                    colorCode: "green"
                }
                ListElement {
                    name: "設備"
                    colorCode: "green"
                }

                ListElement {
                    name: "設備"
                    colorCode: "green"
                }

                ListElement {
                    name: "設備"
                    colorCode: "green"
                }

                ListElement {
                    name: "設備"
                    colorCode: "green"
                }
                ListElement {
                    name: "設備"
                    colorCode: "green"
                }

                ListElement {
                    name: "設備"
                    colorCode: "green"
                }

                ListElement {
                    name: "設備"
                    colorCode: "green"
                }

                ListElement {
                    name: "設備"
                    colorCode: "green"
                }
                ListElement {
                    name: "設備"
                    colorCode: "green"
                }

                ListElement {
                    name: "設備"
                    colorCode: "green"
                }

                ListElement {
                    name: "設備"
                    colorCode: "green"
                }

                ListElement {
                    name: "設備"
                    colorCode: "green"
                }

            }
        }
        Rectangle {
            width: 1;
            height: parent.height;
            color: "gray";
            anchors.right: parent.right;
        }
    }

 

4、例子4

動態生成一個界面,控制器顏色和文字內容【不能動態增加減少】

 

 

 ①、創建QAbstractListModel子類

paralistmodel.h

#ifndef PARALISTMODEL_H
#define PARALISTMODEL_H

#include <QAbstractListModel>

struct ParaModel
{
    ParaModel()
    {
        paraName = "";
        crState = "";
    }
    //核心屬性
 QString paraName; QString crState;
};

class ParaListModel : public QAbstractListModel
{
    Q_OBJECT
public:
    ParaListModel(QObject* parent = 0);
    enum Roles//qml用來識別別名的規則
    {
        paraNameRole = Qt::UserRole + 1,
        colorRole
    };
    void addModel(const ParaModel &deviceList);//C++設置值
    void update(int index, const ParaModel &paraModel);//C++更新
    void clear();//清空

protected:
    int  rowCount(const QModelIndex &parent = QModelIndex()) const;//qml內部調用,不用多管直接重寫即可
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;//qml內部調用,不用多管直接重寫即可
    QHash<int, QByteArray> roleNames() const;//qml內部調用,不用多管直接重寫即可

private:
    QList<ParaModel> m_data;
};

#endif // PARALISTMODEL_H

paralistmodel.cpp

#include "paralistmodel.h"

ParaListModel::ParaListModel(QObject *parent)
    : QAbstractListModel(parent)
{

}

void ParaListModel::addModel(const ParaModel &paraModel)
{
    beginInsertRows(QModelIndex(), rowCount(), rowCount());
    m_data << paraModel;
    endInsertRows();
}

int ParaListModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return m_data.count();
}

QVariant ParaListModel::data(const QModelIndex &index, int role) const
{
    if (index.row() < 0 || index.row() >= m_data.count())
        return QVariant();

    const ParaModel &paraModel = m_data[index.row()];
    switch (role)
    {
    case paraNameRole:
        return paraModel.paraName;
        break;
    case colorRole:
        return paraModel.crState;
        break;
    default:
        break;
    }
    return QVariant();
}

void ParaListModel::update(int index, const ParaModel &paraModel)
{
    if (index < 0 || index >= m_data.count())
        return;
    ParaModel &srcModel = m_data[index];
    if(paraModel.paraName != "")
    {
        srcModel.paraName = paraModel.paraName;
    }
    if(paraModel.crState != "")
    {
        srcModel.crState = paraModel.crState;
    }
}

void ParaListModel::clear()
{
    m_data.clear();
}

//qml通過這里的QByteArray來訪問數據
//首先qml輸入"value"知道其role為roles[valueRole],然后把valueRole輸入到data函數返回真實值
QHash<int, QByteArray> ParaListModel::roleNames() const
{
    QHash<int, QByteArray> roles;
    roles[paraNameRole] = "paraName";
    roles[colorRole] = "crState";
    return roles;
}

 

②、創建PredictPara.qml,用於對listview的顯示

import QtQuick 2.0

Rectangle {
    id: root;
    width: 300;
    height: 400;
//    color: "#222648";//背景色
    color: Qt.rgba(0,0,0,0)
    ListView {
        anchors.fill: parent;
        model: ParaListModel;
        delegate: delegate_list
        spacing: 0;
    }
    //委托
    Component {
        id: delegate_list;
        Rectangle {
            id: rect;
            width: root.width;
            height: 40;
            color: Qt.rgba(0,0,0,0)
            Rectangle {
                id: round;
                width: 30;
                height: width;
                radius: width/2;
                color: crState;
                anchors.verticalCenter: parent.verticalCenter;
                anchors.left: parent.left;
                anchors.leftMargin: 40;
            }

            Text {
                id: para_name;
                text: paraName;
                font.pixelSize: 20;
                anchors.centerIn: parent;
                color: crState;
                horizontalAlignment: Text.AlignLeft;
            }
            Rectangle {
                width: parent.width;
                height: 1;
                color: "green";
                anchors.bottom: parent.bottom;
            }
        }
    }

}

標紅的兩個屬性就是來自於c++類里的屬性

③、main.cpp里設置上下文屬性

void main_fun()
{  
    QUrl source("qrc:/qml/PredictPara.qml");
    ui->quickWidget_predict_para->setResizeMode(QQuickWidget::SizeRootObjectToView);
    ui->quickWidget_predict_para->setSource(source);
    ui->quickWidget_predict_para->setAttribute(Qt::WA_AlwaysStackOnTop);
    ui->quickWidget_predict_para->setClearColor(QColor(Qt::transparent));
    
    int paraCount = paras.size();
    QStringList paraInfoList;
    QString paraName;
    BIT_State_Type cv;
    std::pair<QString, QString> paraStatePair;
    ParaListModel modelList;
    for(int i=0;i<3;++i){
        ParaModel model;
        model.paraName =  "name_1";
        model.crState = "#ff0000";
        modelList.addModel(model);
    }
    ui->quickWidget_predict_para->rootContext()->setContextProperty("ParaListModel", &modelList);
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM