PDA

View Full Version : set XML tag to QTableView



mythili
6th May 2013, 15:06
Hi,
I am having an XML file which containing titles and authors of books. I am reading the title tags alone and I want to display them in QTableView in rows. Please help me.

wysota
7th May 2013, 01:33
What is the problem you are having?

mythili
7th May 2013, 06:15
I am using QStandardItemModel and QModelIndex. When I am inserting rows, the rows should be appended to the existing rows not to be inserted before the existing rows

my code is
QStandardItemModel *model = new QStandardItemModel();
QModelIndex parentItem;
QModelIndex mindex;
After reading the 'title' tag from XML , I am assigning the text to a variable "text" of type QSting.

parentItem = model->index(row, 0, parentItem);
model->insertRows(0, 1, parentItem);
mindex=model->index(0,0);
model->setData(mindex,text);

Please help me.

ChrisW67
7th May 2013, 06:54
This problem has nothing to do with the view.

If you do not want to insert the row at the front of the model rows then do not call insertRow(0,...). You want to insert the new row before the (non-existent) row rowCount():


model->insertRow(model->rowCount(parentIndex), parentIndex);


There are other problems:

Your thread title implies you are expecting a table structure but you appear to be trying to build a tree.
You also set the text on the-most top-level item every time; not the item you just inserted.
Have you called insertColumn() or setColumnCount() at some stage? Otherwise your model has no columns.


I think, in general, you need to read the QStandardItemModel Detailed Description for examples of creating a table.

mythili
7th May 2013, 07:04
Yes, what u said is correct. I dont want tree structure. I want just table. I have set the Column count to 1. Only problem I am facing is its appending rows. Can u give me the lines of code for tableView without parentitem.

wysota
7th May 2013, 07:22
I dont want tree structure. I want just table. I have set the Column count to 1.
So you don't want a table but rather a list.

mythili
7th May 2013, 07:54
Can u give me the lines of code for listView or tableView using QStandardItremModel and QModelIndex

wysota
7th May 2013, 08:05
Docs for QStandardItemModel contain an example for building a tree or a table.

mythili
7th May 2013, 08:08
I want to use QModelIndex instead of QStandardItem. Can u help me

wysota
7th May 2013, 10:16
model.insertRow(model.rowCount());
QModelIndex idx = model.index(model.rowCount()-1);
model.setData(idx, "foo", Qt::DisplayRole);

mythili
7th May 2013, 13:43
Thank u very much..Its working fine...