Results 1 to 8 of 8

Thread: [PySide] -# AttributeError: 'PySide.QtGui.QTreeView' object has no attribute 'topLeve

  1. #1
    Join Date
    Jul 2015
    Posts
    8
    Qt products
    Platforms
    Windows

    Default [PySide] -# AttributeError: 'PySide.QtGui.QTreeView' object has no attribute 'topLeve

    Hey Guys,

    I don't understand this problem here.
    I am quiet new to python and pyside in Maya to be specific and I am maintaining & extending existing code.
    Now I need a solution to list all items inside a READ CAREFUL NOW 'QTreeView'.

    When I look in the internet about QTreeView lots of people say there is no way to get all items as strings with their index with one command.
    Only by surfing thru all topLevelItems and all childItems or so.

    ok, so I tried:
    Qt Code:
    1. print self.lightListerTree.topLevelItemCount()
    To copy to clipboard, switch view to plain text mode 

    and I get the following error:
    Qt Code:
    1. # AttributeError: 'PySide.QtGui.QTreeView' object has no attribute 'topLevelItemCount'
    To copy to clipboard, switch view to plain text mode 

    Does anyone have an idea what I was doing wrong?
    And if anyone every written already a functioning class to just loop thru all top level and child nodes, very welcome.

    what I am trying to do: the user has an item selected (I know it's name as string) and I need to select the same item in the QTreeView.


    btw. no matter what I try I always get this error:
    Qt Code:
    1. # AttributeError: 'PySide.QtGui.QTreeView' object has no attribute 'findItems'
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. # AttributeError: 'PySide.QtGui.QTreeView' object has no attribute 'MatchRecursive'
    To copy to clipboard, switch view to plain text mode 

    there's something fishy right? I have to get the name of the QTreeView aswell maybe?

  2. #2
    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: [PySide] -# AttributeError: 'PySide.QtGui.QTreeView' object has no attribute 'top

    there's something fishy right?
    There's nothing fishy at all. Have you read the QTreeView documentation? It has none of the methods you list. On the other hand, if you look at the documentation for QTreeWidget, you might find something there...

    And no class in all of Qt has a method that starts with a capital letter.

  3. #3
    Join Date
    Jul 2015
    Posts
    8
    Qt products
    Platforms
    Windows

    Default Re: [PySide] -# AttributeError: 'PySide.QtGui.QTreeView' object has no attribute 'top

    aha, I guess next time I should just read the documentation more carefully, and don't rely on what people in the internet say.

    Anyway according to the documentation there is no way to get what is in the QTreeView?
    Who programs a nightmare like this?

    Or do I need to retreive my value from somewhere else?

  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: [PySide] -# AttributeError: 'PySide.QtGui.QTreeView' object has no attribute 'top

    A QTreeView always gets its data from a model, an instance of a QAbstractItemModel subclass.

    If you need to iterate over all entries, you can do that with just the model.
    If that is a custom model you could even implement that inside that class, operating directly on the underlying data.

    For finding an entry by string, see QAbstracItemModel::match()

    Cheers,
    _

  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: [PySide] -# AttributeError: 'PySide.QtGui.QTreeView' object has no attribute 'top

    The QTreeView uses an external model (derived from QAbstractItemModel). QTreeWidget builds its own internal model from the QTreeWidgetItem instances that are inserted into it. That's why QTreeWidget has methods for traversing the tree and retrieving the contents - it knows the structure of its model, so can retrieve the content.

    To do this with a QTreeView, you have to call the base class QAbstractItemView::model() method to retrieve the pointer to the QAbstractItemModel that the view uses. Once you have the model, you can call QAbstractItemModel::index() to traverse through it (C++, you'll have to translate to Python):

    Qt Code:
    1. QAbstractItemModel * pModel = lightListerTree->model();
    2. if ( pModel )
    3. {
    4. // rowCount is the number of top level items in the model
    5. int nRows = pModel->rowCount( QModelIndex() );
    6. for ( int nRow = 0; nRow < nRows; ++nRow )
    7. {
    8. // You retrieve the model index instances for the first column in each row
    9. QModelIndex rowIndex = pModel->index( nRow, 0, QModelIndex() );
    10. if ( rowIndex.isValid() )
    11. {
    12. QString text = rowIndex.data().toString();
    13. // etc.
    14. }
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

    This code just gets the top level items in the model. You can turn that into a recursive function to drill down to all levels of the model - once you get a model index for the first column in a row, you pass it in the the call to QAbstractItemModel::hasChildren() (or to a call to rowCount()). If hasChildren() returns true, then you call QAbstractItemModel::index() again with 0, 0 row and column indexes, but this time using the model index you have for the current row as the parent argument (instead of an invalid QModelIndex()).

  6. #6
    Join Date
    Jul 2015
    Posts
    8
    Qt products
    Platforms
    Windows

    Default Re: [PySide] -# AttributeError: 'PySide.QtGui.QTreeView' object has no attribute 'top

    brilliant, thank you so much!
    Works like a charm! And I understand the whole concept a little better now

    just created a pointer from the qAbstractItemModel and then used pointer.match() to identify the object, and setCurrentIndex() to set selected.

  7. #7
    Join Date
    Jul 2015
    Posts
    8
    Qt products
    Platforms
    Windows

    Default Re: [PySide] -# AttributeError: 'PySide.QtGui.QTreeView' object has no attribute 'top

    Hi,

    I am facing another problem similar topic, so let's put it here.
    I need to query the current selected item in my QAbstractItemModel or QTreeView, but I seem to be not able to find a way to query it and return it's item value as string.

    any ideas?

    thanks.


    Added after 26 minutes:


    nevermind, found it after hours of searching.
    it can be received from the QAbstractItemModel data field.
    Last edited by Sorath; 24th August 2015 at 12:44.

  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: [PySide] -# AttributeError: 'PySide.QtGui.QTreeView' object has no attribute 'top

    For future reference: the view's item selection model can tell you which items are selected.

    Cheers,
    _

Similar Threads

  1. Replies: 3
    Last Post: 19th June 2014, 14:59
  2. pyside question pls help
    By sujan.dasmahapatra in forum Qt Programming
    Replies: 0
    Last Post: 12th July 2013, 11:44
  3. Using QML Components for the Desktop with PySide
    By hansotronic in forum Qt Quick
    Replies: 2
    Last Post: 30th October 2012, 01:49
  4. Qt Designer and PySide
    By moabi in forum Newbie
    Replies: 0
    Last Post: 25th August 2011, 09:40
  5. Pyside needs QT?
    By junichiro in forum Installation and Deployment
    Replies: 0
    Last Post: 30th July 2011, 16:13

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.