Results 1 to 8 of 8

Thread: How can I get data from the hidden column in a QTreeView?

  1. #1
    Join Date
    Apr 2015
    Posts
    6
    Thanks
    1
    Qt products
    PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default How can I get data from the hidden column in a QTreeView?

    Hi, everyone! I have a qtreeview, that displays some information from the sqlite database. When I click on the item in the tree, I go to the DB and trying to find details about selected item... The trouble is that i need to search information in the DB by ID, not by displayed name.. So, I decided to draw additional column in the tree that will hold the ID. When I click on the row, I take element's ID with help of self.ui.treeView.selectedIndexes()[1] (0 column is a name)
    But. When I am trying to hide column using hideColumn(), i get an "index is out of bounds" error... In the description of selectedIndexes() I read that "This convenience function returns a list of all selected and non-hidden item indexes in the view."... So, is there another way to tackle with my problem?

    Thanks!

  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: How can I get data from the hidden column in a QTreeView?

    You take the index for the first column and then ask the model for the index of the second column, using the parent index and row of the index you have.

    Cheers,
    _

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How can I get data from the hidden column in a QTreeView?

    Alternatively, you can store the ID as Qt::UserRole data for the model index: QAbstractItemModel::setData(). When you get notified of the selected index, you ask it for the UserRole data, which is returned as a QVariant.

  4. #4
    Join Date
    Apr 2015
    Posts
    6
    Thanks
    1
    Qt products
    PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: How can I get data from the hidden column in a QTreeView?

    Oh, I tried to do what you described.. It works only while my column is visible...

    hm... I'll try it, thanks!

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How can I get data from the hidden column in a QTreeView?

    Are you using a proxy model to convert the DB result into something to display in the tree?

  6. #6
    Join Date
    Apr 2015
    Posts
    6
    Thanks
    1
    Qt products
    PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: How can I get data from the hidden column in a QTreeView?

    I am creating QStandardItem and recursively appending rows..

    child_node = QtGui.QStandardItem("ITEM TEXT")
    root.appendRow(child_node)

    "ITEM TEXT" - is getting directly from sqlite...for each iteration...

  7. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How can I get data from the hidden column in a QTreeView?

    Well then it is easy - when you create the QStandardItem, just call
    Qt Code:
    1. child_node.setData( QVariant( ID ), Qt::UserRole )
    To copy to clipboard, switch view to plain text mode 
    When you get the selected item, then it is just
    Qt Code:
    1. ID = selected_node.data( Qt::UserRole )
    To copy to clipboard, switch view to plain text mode 
    (or whatever the appropriate syntax is - I'm not a python programmer).

  8. The following user says thank you to d_stranz for this useful post:

    serj (4th April 2015)

  9. #8
    Join Date
    Apr 2015
    Posts
    6
    Thanks
    1
    Qt products
    PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: How can I get data from the hidden column in a QTreeView?

    It works! Thanks! This is exactly what I need..

    child_node.setData(QtCore.QVariant(str(chld["ID"]))) #setting data

    selected_id = str(index.model().itemFromIndex(index).data().toSt ring()) #getting data

Similar Threads

  1. Replies: 4
    Last Post: 27th February 2014, 10:42
  2. Showing a Hidden QTableView Column
    By dchow in forum Qt Programming
    Replies: 1
    Last Post: 16th June 2011, 01:11
  3. Replies: 1
    Last Post: 23rd January 2010, 23:04
  4. Replies: 2
    Last Post: 21st October 2009, 09:13
  5. hidden QListView column suddenly visible
    By edb in forum Qt Programming
    Replies: 10
    Last Post: 27th January 2006, 09:00

Tags for this Thread

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.