PDA

View Full Version : QStandardItemModel



neda
30th April 2016, 10:12
Hi,

I want to bind QStandardItemModel to a table(TableView or GridView or ...) with several rows and columns.

Table must have not any scroll and height of table is equal of count of row.

I can show QStandardItemModel in tableview but height of it more than from count of row.

I can show QStandardItemModel in GridView but it can show just one record.

Please gide me.


TableView {
id: tablemodel
anchors.fill: parent

model: myXML.newMyModel


MyTableViewColumn {
role: "field1"
}
MyTableViewColumn {
role: "field2
}
MyTableViewColumn {
role: "field3
}
MyTableViewColumn {
role: "field4
}
}



GridView {
anchors.fill: parent
model: myXML.newMyModel

delegate: Rectangle {
Text {
text: field1
}
Text {
text: field2
}
Text {
text: field3
}
Text {
text: field4
}
Text {
text: field5
}
Text {
text: field6
}
}
}

anda_skoa
1st May 2016, 09:32
Well, this obviously isn't the code you are using.

Can you show the roleNames() implementation of your QStandardItemModel subclass and how you create a "row" item?

Cheers,
_

neda
1st May 2016, 09:51
Thank you.



Can you show the roleNames() implementation of your QStandardItemModel subclass and how you create a "row" item?
_





QHash<int, QByteArray> roleNames() const{
QHash<int, QByteArray> roles;
roles[index] = "index";
roles[first] = "first";
roles[avg] = "avg";
roles[time] = "time";
roles[niro] = "force";
roles[method] = "method";
roles[zoom] = "zoom";
roles[image] = "image";
return roles;
}

QString MyXML::readXMLFile(QString fileName)
{
QFile file(fileName);
if (!file.open(QFile::ReadOnly | QFile::Text))//Error: Cannot read file
return "";

QXmlStreamReader reader(file.readAll());
file.close();

newMyModel->clear();

while(!reader.atEnd()) {
reader.readNext();
if (reader.isStartElement()) {

QXmlStreamAttributes attributes = reader.attributes();

QStandardItem* it = new QStandardItem();

it->setData(attributes.value("index").toString(), MyModel::index);
it->setData(attributes.value("first").toString(), MyModel::first);
it->setData(attributes.value("avg").toString(), MyModel::avg);
it->setData(attributes.value("time").toString(), MyModel::time);
it->setData(attributes.value("force").toString(), MyModel::force);
it->setData(attributes.value("method").toString(), MyModel::method);
it->setData(attributes.value("zoom").toString(), MyModel::zoom);
it->setData(attributes.value("image").toString(), MyModel::image);

newMyModel->appendRow(it);
}
}

return fileName;
}
TableView {
id: tablemodel
anchors.fill: parent

model: myXML.newMyModel

MyTableViewColumn {
role: "index"
title: "index"
}
MyTableViewColumn {
role: "zoom"
title: "zoom"
}

MyTableViewColumn {
role: "avg"
title: "avg"
}

MyTableViewColumn {
role: "first"
title: "first"

}

}

anda_skoa
1st May 2016, 11:34
That looks good.
So you are saying that if you do this


GridView {
anchors.fill: parent
model: myXML.newMyModel

delegate: Text {
text: "index"
}
}

you are only getting one item in the grid?

Cheers,
_

neda
1st May 2016, 12:01
Thank you




GridView {
anchors.fill: parent
model: myXML.newMyModel

delegate: Text {
text: "index"
}
}

you are only getting one item in the grid?

_


Yes

anda_skoa
1st May 2016, 12:58
Very strange.
What's the value of the GridView's count property?

Have you tried with a ListView just to check that you can access all model data?

Cheers,
_