我綁定了一個ListView,其值從cpp傳遞。信號發出問題 - Listview未顯示完整列表
問題:Listview只顯示一行,意味着第一個值,其餘的行沒有出現。
選中: 我在main.qml作爲測試創建的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綁定到DataTable或XML。在這裏我跟着創建了一個名爲Details [Details.h]的類,並創建了Model.h/Model.cpp,並從那裏獲取值並綁定到ListView。我做對了嗎,還是我們有其他的流程。任何教程/ Codesnippet /項目鏈接高度讚賞。
您是如何得出與信號有關的結論的? 據我所知你的描述它工作正常,否則視圖將是完全空的。 我的猜測是你的模型工作不正常。你真的測試過它嗎? –
你能告訴我們對'get_detailsModel()'的定義嗎? –
親愛的Ansh,我將上面的代碼作爲流發佈。 –