PDA

View Full Version : Bind rows of a standarditemmodel



canavaroski90
21st January 2014, 10:36
Hi all,

I'm building a gui application with Qt 5.2.0 . In the app, user can add an item to a QListView and at the background all items are representing by a class (lets say itemClass). I mean when you add a new item then app is going to create a new object and add a new row to the Qtreeview.

Here is the problem,
itemClass object is a thread and it may change values of some members in runtime. I want to update corresponding row to the item to be updated when a itemClass updated its own member value.

I'll try to explain step by step;
ADDING
- Write name into lineEdit & Click AddItemButton
- Qtreeview -> add new row with text from lineEdit
- Create a new itemClass, set the value of the "name" member to text from lineEdit, run thread

RUN-TIME
- in itemClass thread set the value of the "name" member to text which is coming from web
- now update corresponding row to the itemClass.

is it possible?
sorry for bad english

anda_skoa
21st January 2014, 12:14
ADDING
- Write name into lineEdit & Click AddItemButton
- Qtreeview -> add new row with text from lineEdit
- Create a new itemClass, set the value of the "name" member to text from lineEdit, run thread

This sounds like the wrong order.
The "add" should create the item and then the model should create the respective row.

Do you have a list of objects or a tree?

In case of list it should be easy enough to create a list model, i.e. derive from QAbstractListModel.
Tree is trickier, but your own model will still be the best choice.

Cheers,
_

canavaroski90
22nd January 2014, 15:58
Hi,
thanks for the answer. I noticed that the order is wrong, as you said. now ;

- creating the root itemmodel
- creating rootRow
- creating a new object and passing the parameters to it
- new object is creating required standartitems with incoming parameters

it works perfect.

Now I've got another problem; I wonder how to bind row address (or index) to the appropriate object? So when I delete object, corresponding row (and all columns in this row) will be deleted automatically.

do you know how can it be accomplished?

anda_skoa
22nd January 2014, 16:53
You will need a lookup structure, e.g. a QHash.

Something like



QHash<ItemClass*, int> rowForItem;


Or you store the corresponding row in the ItemClass instances, etc.

Cheers,
_

canavaroski90
22nd January 2014, 17:26
Or you store the corresponding row in the ItemClass instances, etc.

Cheers,
_

At this point, i do not know how to get row handler (i.e. pointer,address,key or anything else that gives me row).

here is my mainclass


QStandartItemModel tmpModel;
ui->treeView->setModel(tmpModel);
QStandartItem rootRow = tmpModel->invisibleRootItem();

for(;;)
{
MyObj *newObj = new MyObj(rootRow);
}


And here is MyObj implementation


Myobj(QStandartItem* incItem)
{
QstandartItem newItem;
//some code with newItem
incItem->appendRow(newItem)
// at this point i need to know the address of newly created row. So i can do this;
rowPointer = newRowPointer;

}

~MyObj()
{
delete rowPointer;
}

And when I call destructor of MyObj corresponding row to the MyObj will be deleted automatically and it'll disappear from the treeView.

How can i get the handler of the row?

anda_skoa
23rd January 2014, 08:17
I am not sure I understand.

You create a QStandardItem for your object and then add it to the model as a new row. What other row do you need?

Cheers,
_

canavaroski90
23rd January 2014, 09:21
Thanks for your answer.

I'm trying to build a tree as below:


-rootRow
-branch1 (parentRow)
-item1 -item2 -item3 (these subRows are being created by MyObj class)
-item1 -item2 -item3
...
-branch2
-item1 -item2 -item3
...

So as I mentioned on my last post, I'm creating a new MyObject instance and passing required parameters and parentRow to it. And then the new instance is creating a new row with the given parameters and append it to parentRow. But I want to get the handler of new subRow, which is created by new MyObj, and assign its value to a member of MyObj. So when destructor of MyObj is called, the row will be deleted from tree automatically.

How can i get the new subRow handler? I mean MyObj instance must know which subRow has itself created and so it can use subRow handler later.

anda_skoa
23rd January 2014, 10:23
And then the new instance is creating a new row with the given parameters

Which other row do you need?

Cheers,
_

canavaroski90
25th January 2014, 17:52
Which other row do you need?

hey,

sorry for late answer. i'm just trying to get the handler of newly created subrow. i do not need handler of any other rows. i can assign it to a member which is created in MyObj.

Actually I'm appending these Myclass objects to a Qmap as a value and the keys should be handler of subrows. so when somebody click a row and try to delete it, on the background i'll search the index of the row in the map. so i need a unique handler for each of these subrows.

how can it be done?

anda_skoa
26th January 2014, 10:32
The question remains: what other row do you need?

You have the row that you are inserting, yet you seem to need a pointer to something else. What is the something else you need?

Earlier you posted this



Myobj(QStandartItem* incItem)
{
QstandartItem *newItem new QStandardItem;
//some code with newItem
incItem->appendRow(newItem)
// at this point i need to know the address of newly created row. So i can do this;
rowPointer = newRowPointer;

}

inCitem->appendRow(newItem) creates a new row. That pointer you obviously have (newItem).
What does that "newRowPointer" point to?

Cheers,
_

canavaroski90
27th January 2014, 16:05
The question remains: what other row do you need?

You have the row that you are inserting, yet you seem to need a pointer to something else. What is the something else you need?

Earlier you posted this



Myobj(QStandartItem* incItem)
{
QstandartItem *newItem new QStandardItem;
//some code with newItem
incItem->appendRow(newItem)
// at this point i need to know the address of newly created row. So i can do this;
rowPointer = newRowPointer;

}

inCitem->appendRow(newItem) creates a new row. That pointer you obviously have (newItem).
What does that "newRowPointer" point to?

Cheers,
_

Wow I've made a mistake while exampling the code. it is actually like that;


Myobj(QStringList* incItemList, QStandartItem* parentRow)
{
// creatinf new items to build a new row
QstandartItem *newItem_1 = new QStandardItem(incItemList->at(0));
QstandartItem *newItem_2 = new QStandardItem(incItemList->at(1));
QstandartItem *newItem_3 = new QStandardItem(incItemList->at(2));

// now creating a new subRow and filling it with the newly created items
Qlist<QStandartItem*> newRow;
newrow.append (newItem_1);
newrow.append (newItem_2);
newrow.append (newItem_3);

// now append new subRow to the parent row.
// I do not know what is the pointer or handler of the subrow I've added. I'm just adding it to the parent row, and the items are
// being appended to the row with index of like (parentRowIndex,1) , (parentRowIndex,2) , (parentRowIndex,3) respect to newItem_1 newItem_2 newItem_3.
parentRow->appendRow(newRow);

// at this point i need to know the address of newly created row. So i can do this;
rowPointer = newRowPointer;
}

sorry for misexplaining, did this reply your question?

anda_skoa
27th January 2014, 19:19
So you save newRow as a member. It contains pointers to each column's cell in that row.

Cheers,
_

canavaroski90
27th January 2014, 21:10
So you save newRow as a member. It contains pointers to each column's cell in that row.

Cheers,
_

Yes you're right, it contains pointers to each column's cell in that subRow. But i need to know the pointer of (or sth to get the whole subrow) subrow, because after creating a new object in maincontroller class, I'll insert all these new MyObject instances to a Qmap with a key of subrow pointer. so when someone clicked a subrow on a treeview, I can get the subrow pointer, and i can find the right MyObject instance, corresponding to subrow pointer, in the map.

I can do that with appending a new hided column in a subrow, and setting value of this cell as a unique id of the subrow, so i can insert MyObj instances to the map with this unique id. But in this way i have to track unique id order and i do not want to do that. I would like to design a systems is complete diynamic. If keys for the map can be gathered from subRow (like pointer of it) it'll be great.

anda_skoa
28th January 2014, 08:33
Since you don't want to store the list of row items, how about newItem_1?

Cheers,
_

P.S.: at some point you should really look into creating your model directly. That way you don't have to synchronize two trees (yours and the item tree)

canavaroski90
28th January 2014, 11:58
Since you don't want to store the list of row items, how about newItem_1?

Cheers,
_

P.S.: at some point you should really look into creating your model directly. That way you don't have to synchronize two trees (yours and the item tree)

I've tried something similar and there was problems again but now i've got another idea. I'll try and share the results.
thank you