Results 1 to 14 of 14

Thread: How to know wich item is clicked QTreeWidget

  1. #1
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default How to know wich item is clicked QTreeWidget

    Hi,

    I have a QTreeWidget that I insert some items at column 0. This items have other items,...

    What I want is to know wich item is pressed. I'm reading about QModelIndex but for a tree similar of this:
    - Item1
    -SubItem1
    -SubSubItem1

    The row and column of these three items are always (0,0).
    I have a SLOT called every time an item is clicked and I recive the clicked item there.

    Any idea how to do it?

    Thanks,
    Òscar Llarch i Galán

  2. #2
    Join Date
    Oct 2007
    Location
    Quebec, Canada
    Posts
    40
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to know wich item is clicked QTreeWidget

    Check model index's parent's ModelIndex.

  3. #3
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to know wich item is clicked QTreeWidget

    Hi,

    Could you be more explicit?

    Thanks,
    Òscar Llarch i Galán

  4. #4
    Join Date
    Oct 2007
    Location
    Quebec, Canada
    Posts
    40
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to know wich item is clicked QTreeWidget

    Yeah sorry I was searching for a better answer. Here's a link:

    http://doc.trolltech.com/4.3/model-v...rents-of-items

    I guess you're receiving a model index in your slot.

    You can recursively call parent() on model index to retrieve the higher level model index to get the full path of the selection.

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to know wich item is clicked QTreeWidget

    With QTreeWidget there shouldn't be need to use QModelIndexes, at least for that. QTreeWidget offers a signal QTreeWidget::itemClicked(QTreeWidgetItem* item, int column). What do you want to actually do when a certain item is clicked?
    J-P Nurmi

  6. #6
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to know wich item is clicked QTreeWidget

    Hi,

    Ok, will try to use it.

    Thanks,
    Òscar Llarch i Galán

  7. #7
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to know wich item is clicked QTreeWidget

    Hi,

    I'm not getting it work yet.

    I have the tree populated like the attached image.

    Thanks,
    Attached Images Attached Images
    Òscar Llarch i Galán

  8. #8
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to know wich item is clicked QTreeWidget

    But why don't you use what jpn suggested? It is as clear as that, even simpler that using QModelIndex.

  9. #9
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to know wich item is clicked QTreeWidget

    Hi,

    Quote Originally Posted by marcel View Post
    But why don't you use what jpn suggested? It is as clear as that, even simpler that using QModelIndex.
    Sorry, but I didn't see jpn reply (I supose I was writting too).

    Quote Originally Posted by jpn View Post
    What do you want to actually do when a certain item is clicked?
    What I want is to know wich item is clicked.
    I have an internal structure with some LinkedLists. When I add, remove, rename, ... an item, it represents one object of the internal data, so I have to know wich object is represented by the item that is selected.

    Thanks,
    Òscar Llarch i Galán

  10. #10
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to know wich item is clicked QTreeWidget

    Quote Originally Posted by ^NyAw^ View Post
    I have an internal structure with some LinkedLists. When I add, remove, rename, ... an item, it represents one object of the internal data, so I have to know wich object is represented by the item that is selected.
    Then you should seriously consider saying goodbye to QTreeWidget and switching to model based approach. You should wrap the internal structure into QAbstractItemModel instead of replicating it to another structure.
    J-P Nurmi

  11. #11
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to know wich item is clicked QTreeWidget

    By the way, does the internal structure of yours have any kind of identifier numbers or something like that? Or how did you plan to match corresponding items anyway? One possibility for solving this in a quick and dirty way is to abuse QTreeWidgetItem::setData() with Qt::UserRole to identify which item corresponds which part of the internal structure.
    J-P Nurmi

  12. #12
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to know wich item is clicked QTreeWidget

    Hi,

    Quote Originally Posted by jpn View Post
    Then you should seriously consider saying goodbye to QTreeWidget and switching to model based approach. You should wrap the internal structure into QAbstractItemModel instead of replicating it to another structure.
    Wrap the internal structure to a model is to get a model and my structure in parallel?

    Quote Originally Posted by jpn View Post
    By the way, does the internal structure of yours have any kind of identifier numbers or something like that?
    Yes, I have LinkedLists that have internal nodes with uniques ID for each node.

    Really I don't know wich is the best solution because my internal structure is handcoded (I had coded linkedLists, nodes, ... manually because it was done before I discovered Qt).

    Thanks,
    Òscar Llarch i Galán

  13. #13
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to know wich item is clicked QTreeWidget

    Quote Originally Posted by ^NyAw^ View Post
    Wrap the internal structure to a model is to get a model and my structure in parallel?
    QAbstractItemModel subclass would itself keep no data at all. It would be a plain interface for to your internal data. That's how Qt's view classes would be able to represent your internal data without need of copying the internal data to another structure, QTreeWidgetItems in your case.

    Quote Originally Posted by ^NyAw^ View Post
    Yes, I have LinkedLists that have internal nodes with uniques ID for each node.

    Really I don't know wich is the best solution because my internal structure is handcoded (I had coded linkedLists, nodes, ... manually because it was done before I discovered Qt).
    So basically you could mark each QTreeWidgetItem with corresponding identifier:
    Qt Code:
    1. int id = ... // id of the corresponding item in the internal data structure
    2. item->setData(0, Qt::UserRole, id);
    To copy to clipboard, switch view to plain text mode 
    And once you react to clicked-signal you could ask for the identifier:
    Qt Code:
    1. int id = item->data(0, Qt::UserRole).toInt();
    2. ... // find the corresponding item in the internal data structure with help of id
    To copy to clipboard, switch view to plain text mode 
    Roles starting from Qt::UserRole and upwards can be used for application specific things. QTreeWidgetItem will be able to store them, but will of course use them for nothing by itself.
    J-P Nurmi

  14. The following user says thank you to jpn for this useful post:

    ^NyAw^ (6th November 2007)

  15. #14
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to know wich item is clicked QTreeWidget

    Hi,

    Hey jpn! It's so easy now!

    I just tell the item wich kind of item is, and when one is clicked, if it represents a node into a linkedList, I can tell the item parent wich position the item stays.

    Thanks for all that have replied and helped me, maybe I will ask some other questions,
    Òscar Llarch i Galán

Similar Threads

  1. QTreeWidget clicked signal
    By ^NyAw^ in forum Qt Programming
    Replies: 41
    Last Post: 30th January 2010, 11:42
  2. QTreeWidget select item
    By ^NyAw^ in forum Qt Programming
    Replies: 7
    Last Post: 26th October 2007, 14:26
  3. Inserting Item to a QTreeWidget from Thread?
    By vishal.chauhan in forum Qt Programming
    Replies: 3
    Last Post: 23rd June 2007, 11:23
  4. Removing items properly from qtreewidget item
    By Djony in forum Qt Programming
    Replies: 6
    Last Post: 21st November 2006, 12:20
  5. Replies: 17
    Last Post: 31st March 2006, 05:57

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.