Results 1 to 15 of 15

Thread: Bind rows of a standarditemmodel

  1. #1
    Join Date
    Jan 2014
    Posts
    12
    Thanks
    3
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Bind rows of a standarditemmodel

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Bind rows of a standarditemmodel

    Quote Originally Posted by canavaroski90 View Post
    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,
    _

  3. #3
    Join Date
    Jan 2014
    Posts
    12
    Thanks
    3
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Bind rows of a standarditemmodel

    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?

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Bind rows of a standarditemmodel

    You will need a lookup structure, e.g. a QHash.

    Something like

    Qt Code:
    1. QHash<ItemClass*, int> rowForItem;
    To copy to clipboard, switch view to plain text mode 

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

    Cheers,
    _

  5. #5
    Join Date
    Jan 2014
    Posts
    12
    Thanks
    3
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Bind rows of a standarditemmodel

    Quote Originally Posted by anda_skoa View Post

    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

    Qt Code:
    1. QStandartItemModel tmpModel;
    2. ui->treeView->setModel(tmpModel);
    3. QStandartItem rootRow = tmpModel->invisibleRootItem();
    4.  
    5. for(;;)
    6. {
    7. MyObj *newObj = new MyObj(rootRow);
    8. }
    To copy to clipboard, switch view to plain text mode 


    And here is MyObj implementation

    Qt Code:
    1. Myobj(QStandartItem* incItem)
    2. {
    3. QstandartItem newItem;
    4. //some code with newItem
    5. incItem->appendRow(newItem)
    6. // at this point i need to know the address of newly created row. So i can do this;
    7. rowPointer = newRowPointer;
    8.  
    9. }
    10.  
    11. ~MyObj()
    12. {
    13. delete rowPointer;
    14. }
    To copy to clipboard, switch view to plain text mode 

    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?

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Bind rows of a standarditemmodel

    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,
    _

  7. #7
    Join Date
    Jan 2014
    Posts
    12
    Thanks
    3
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Bind rows of a standarditemmodel

    Thanks for your answer.

    I'm trying to build a tree as below:

    Qt Code:
    1. -rootRow
    2. -branch1 (parentRow)
    3. -item1 -item2 -item3 (these subRows are being created by MyObj class)
    4. -item1 -item2 -item3
    5. ...
    6. -branch2
    7. -item1 -item2 -item3
    8. ...
    To copy to clipboard, switch view to plain text mode 

    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.

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Bind rows of a standarditemmodel

    Quote Originally Posted by canavaroski90 View Post
    And then the new instance is creating a new row with the given parameters
    Which other row do you need?

    Cheers,
    _

  9. #9
    Join Date
    Jan 2014
    Posts
    12
    Thanks
    3
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Bind rows of a standarditemmodel

    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?

  10. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Bind rows of a standarditemmodel

    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
    Qt Code:
    1. Myobj(QStandartItem* incItem)
    2. {
    3. QstandartItem *newItem new QStandardItem;
    4. //some code with newItem
    5. incItem->appendRow(newItem)
    6. // at this point i need to know the address of newly created row. So i can do this;
    7. rowPointer = newRowPointer;
    8.  
    9. }
    To copy to clipboard, switch view to plain text mode 
    inCitem->appendRow(newItem) creates a new row. That pointer you obviously have (newItem).
    What does that "newRowPointer" point to?

    Cheers,
    _

  11. #11
    Join Date
    Jan 2014
    Posts
    12
    Thanks
    3
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Bind rows of a standarditemmodel

    Quote Originally Posted by anda_skoa View Post
    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
    Qt Code:
    1. Myobj(QStandartItem* incItem)
    2. {
    3. QstandartItem *newItem new QStandardItem;
    4. //some code with newItem
    5. incItem->appendRow(newItem)
    6. // at this point i need to know the address of newly created row. So i can do this;
    7. rowPointer = newRowPointer;
    8.  
    9. }
    To copy to clipboard, switch view to plain text mode 
    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;

    Qt Code:
    1. Myobj(QStringList* incItemList, QStandartItem* parentRow)
    2. {
    3. // creatinf new items to build a new row
    4. QstandartItem *newItem_1 = new QStandardItem(incItemList->at(0));
    5. QstandartItem *newItem_2 = new QStandardItem(incItemList->at(1));
    6. QstandartItem *newItem_3 = new QStandardItem(incItemList->at(2));
    7.  
    8. // now creating a new subRow and filling it with the newly created items
    9. Qlist<QStandartItem*> newRow;
    10. newrow.append (newItem_1);
    11. newrow.append (newItem_2);
    12. newrow.append (newItem_3);
    13.  
    14. // now append new subRow to the parent row.
    15. // 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
    16. // being appended to the row with index of like (parentRowIndex,1) , (parentRowIndex,2) , (parentRowIndex,3) respect to newItem_1 newItem_2 newItem_3.
    17. parentRow->appendRow(newRow);
    18.  
    19. // at this point i need to know the address of newly created row. So i can do this;
    20. rowPointer = newRowPointer;
    21. }
    To copy to clipboard, switch view to plain text mode 

    sorry for misexplaining, did this reply your question?

  12. #12
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Bind rows of a standarditemmodel

    So you save newRow as a member. It contains pointers to each column's cell in that row.

    Cheers,
    _

  13. #13
    Join Date
    Jan 2014
    Posts
    12
    Thanks
    3
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Bind rows of a standarditemmodel

    Quote Originally Posted by anda_skoa View Post
    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.

  14. #14
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Bind rows of a standarditemmodel

    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)

  15. #15
    Join Date
    Jan 2014
    Posts
    12
    Thanks
    3
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Bind rows of a standarditemmodel

    Quote Originally Posted by anda_skoa View Post
    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

Similar Threads

  1. bind MFC Library
    By rock1411 in forum Qt Programming
    Replies: 3
    Last Post: 28th October 2012, 17:50
  2. Read Excel Rows by Rows
    By abgokbayrak in forum Qt Programming
    Replies: 1
    Last Post: 6th December 2011, 22:43
  3. Replies: 2
    Last Post: 15th August 2011, 00:26
  4. Export StandardItemModel to XML
    By homerun4711 in forum Newbie
    Replies: 2
    Last Post: 23rd December 2010, 08:10
  5. Workaround for StandardItemModel drag drop bug
    By onamatic in forum Qt Programming
    Replies: 4
    Last Post: 9th November 2008, 21:50

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.