PDA

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



Sorath
13th August 2015, 16:58
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:

print self.lightListerTree.topLevelItemCount()

and I get the following error:

# AttributeError: 'PySide.QtGui.QTreeView' object has no attribute 'topLevelItemCount'

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:

# AttributeError: 'PySide.QtGui.QTreeView' object has no attribute 'findItems'

# AttributeError: 'PySide.QtGui.QTreeView' object has no attribute 'MatchRecursive'

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

d_stranz
13th August 2015, 17:08
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.

Sorath
13th August 2015, 17:17
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?

anda_skoa
13th August 2015, 17:31
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,
_

d_stranz
13th August 2015, 17:50
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):



QAbstractItemModel * pModel = lightListerTree->model();
if ( pModel )
{
// rowCount is the number of top level items in the model
int nRows = pModel->rowCount( QModelIndex() );
for ( int nRow = 0; nRow < nRows; ++nRow )
{
// You retrieve the model index instances for the first column in each row
QModelIndex rowIndex = pModel->index( nRow, 0, QModelIndex() );
if ( rowIndex.isValid() )
{
QString text = rowIndex.data().toString();
// etc.
}
}
}


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()).

Sorath
14th August 2015, 13:30
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.

Sorath
24th August 2015, 11:44
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.

anda_skoa
25th August 2015, 07:39
For future reference: the view's item selection model can tell you which items are selected.

Cheers,
_