信号发出问题-列表视图没有显示完整的列表

Signal emit Issue - Listview is not showing full list

本文关键字:列表 显示 视图 出问题 信号      更新时间:2023-10-16

我绑定了一个ListView与值从cpp传递。

问题:Listview只显示一行,平均第一个值,其余行不显示。

检查:我在main中创建了一个ListModel/ListElement。现在ListView工作正常,显示所有值

我怀疑信号发出后,错误发生了。

代码片段:

main.qml

ListView {
    id: idListView
    anchors {
        left: parent.left
        leftMargin: 10 * scaleFactor
        right: parent.right
        rightMargin: 10 * scaleFactor
        top: rectangleToolBar.bottom
        topMargin: 10 * scaleFactor
        bottom: rectangleStatusBar.top
        bottomMargin: 10 * scaleFactor
    }
    // model: objHomeController.detailsModel // Display only one row
    //model: idListmodel //Working fine
    delegate: comsearchDelegate
    spacing: 10 * scaleFactor
    clip: true
    highlight: Rectangle {
        color: 'grey'
        Text {
            anchors.centerIn: parent
            color: 'white'
        }
    }
    focus: true
}

Component {
    id: comsearchDelegate
    Row {
        spacing: 10 * scaleFactor
        Column {
            Layout.alignment: Qt.AlignTop
            Text { text: title; font { pixelSize: 14 * scaleFactor; bold: true } }
            Text { text: description; font { pixelSize: 14 * scaleFactor; bold: true } }
        }
    }
}
ListModel {
    id: idListModel
    ListElement{
        title : "sdfsdf";
        description:"sdfsdfs";
    }
    ListElement {
        title : "sdfsdf";
        description:"sdfsdfs";
    }
    ListElement {
        title : "sdfsdf";
        description:"sdfsdfs";
    }
    ListElement {
        title : "sdfsdf";
        description:"sdfsdfs";
    }
}

HomeController.h

Q_PROPERTY(Model* detailsModel READ get_detailsModel WRITE set_detailsModel NOTIFY detailsModelChanged )

HomeController.cpp

void HomeController::set_detailsModel(Model* value)
{
    m_detailsModel = value;
    //value has correct values - checked.
    emit detailsModelChanged(value);
}
Model* HomeController::get_detailsModel(void)
{
    return m_detailsModel;
}
void HomeController::getAllData()
{
    m_detailsModel->clear();
    m_detailsModel->updateModel(eveReadXML());
    set_detailsModel(m_detailsModel);
}

Model.cpp

void Model::updateModel(const QList<Details> & details)
{
    if(this->rowCount() > 0) {
        this->clear();
    }
    beginInsertRows(QModelIndex(),rowCount(),rowCount());
    m_modelData.append(details);
    endInsertRows();
}

因为我来自。net背景,我想了解绑定Listview/GridView到一个数据表或XML。在这里,我遵循,创建类名为Details [Details.h]和创建Model.h/Model.cpp和获取值从那里和绑定到ListView。我做得对吗,还是我们有其他流程。任何教程/代码片段/项目链接,高度赞赏

要从c++中定义ListModel,您需要子类化QAbstractListModel

https://doc.qt.io/qt-5/qabstractlistmodel.html

您可以在这个项目中以QQmlObjectListModel为例:http://gitlab.unique-conception.org/qt-qml-tricks/qt-qml-models

或者克隆它并在你的项目中使用,如下所示:

Q_PROPERTY(QQmlObjectListModel<Details>* detailsModel READ get_detailsModel WRITE set_detailsModel NOTIFY detailsModelChanged)