Problems getting selected Indexes(pyqt)

788 views
Skip to first unread message

Bay

unread,
Oct 22, 2013, 2:12:58 PM10/22/13
to python_in...@googlegroups.com
Hi all, 
          a problem I'm having right now with my script is that when my gui takes in a series of selections decided by the user, I want to be able to select a row/rows from the GUI and remove it. Currently I'm just trying to get it to print out the information from the selection but it crashes everytime I do so. After some attempts to debug it, I noticed that everytime I send the information into the itemFromIndex method in my model I'm getting some huge number like 65536 which is the reason it has been crashing

           Could anyone tell me the likely reason that this may be so? Or possible mistakes I've made? Attached are the files. 

Thank you
Gann Boon Bay
listbuild.py
selectorGUI.py

Justin Israel

unread,
Oct 23, 2013, 4:29:58 AM10/23/13
to python_in...@googlegroups.com
Hey Bay,

While its definitely helpful to have your full code in the event that someone wants to review all of it,it would be ideal to include some concise snippets in the actual question to establish some context. I had to kind of scan your entire project to figure out what you were referring to and reproduce it.

Steps to reproduce:
1. Create a sphere and select
2. Click "Add" in the qt window. Sphere entry pops into view.
3. Select entry and click "Remove". Maya crashes.

What I have found in playing with your code is that returning the internalPointer, and accessing it from the calling view, produces the crash. The way I normally handle returning the underlying object represented by a QModelIndex is to use a custom role for the model and access it through the data() attribute on the index.

## listbuild.py

class SceneGraphModel(QtCore.QAbstractItemModel):

    NodeRole = QtCore.Qt.UserRole

    ...
    def itemFromIndex( self, index ):
        return index.data(self.NodeRole).toPyObject() if index.isValid() else self._rootNode

    ...
    def data(self, index, role):
        if not index.isValid():
            return None
        node = index.internalPointer()

        if role == QtCore.Qt.DisplayRole or role==QtCore.Qt.EditRole:
            if index.column()==0:

                return node.name()

            else:
                return node.typeInfo()

        # Handle the NodeRole
        elif role == self.NodeRole:
            return node

## selectorGUI.py

class Selector( QtGui.QMainWindow ):
    ...
    def remove_Object(self):
        # Seems like you wanted just the first column
        # so that you can retrieve the Node?
        selModel = self.tree.selectionModel()
        indices = selModel.selectedRows()

        folders = []
        shapes = []

        for index in indices:
            # Modified itemFromIndex will return the Node
            # Alternatively, you could just do:
            # node = index.data(self._model.NodeRole)
            node = self._model.itemFromIndex(index)
            print node.name()

Let me know if that is what you were after?



--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/934ce0cf-3e97-45fa-be70-1990757b8172%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Bay

unread,
Oct 25, 2013, 1:25:24 AM10/25/13
to python_in...@googlegroups.com
Hi Justin, 
               first thank you for going through to the trouble to look through my code and think of a solution for me. I apologize for not providing the code snippet because I'm not knowledgeable enough about PyQT to isolate the problem, I was worried that the real problem was actually somewhere else in the model view so I thought it was safer for me to just give the whole code file, still having difficulties trying to understand what is being passed into the modelview, how it's stored and how to retrieve it. 

               And yes, what you gave me indeed solved the problem I've been having. I was wondering through, why it was causing the crash? The code examples I've been hunting through the net don't seemed to use the QtCore.Qt.UserRole(Or maybe it did and I didn't understand the relevance). Again, thanks for going through the time and trouble to give the solution. 

Thank you
Gann Boon Bay 


Justin Israel

unread,
Oct 25, 2013, 2:52:39 AM10/25/13
to python_in...@googlegroups.com

Hey no problem. Just giving some suggestions to help you get faster replies. Sometimes people can't grab the attachments and view them right away. But ya its valid to think the problem could have been elsewhere. So thanks for providing the proper code!

I'm guessing there is some underlying bug or issue with Qt causing the crash. The roles are used for getting various data results of a single index for different purposes. There are the built in ones like Display, Decoration, etc, and UserRole+N is provided to tack on more. I do stuff where I might have 3 custom bits of data I want to request from an index and returning the internal pointer is not always something you want to expose.
Its just a pattern I have followed and I don't claim its the gold standard. Just helps me define multiple custom types with constants that you can use.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

Bay

unread,
Oct 25, 2013, 4:06:23 AM10/25/13
to python_in...@googlegroups.com
Hi Justin, 
                thanks again for explaining to me. Another problem I've been having with understanding pyQT is handling the error from .isValid(). As I am just working from Yasin Uludag's tutorial codes I often just attempt to modify the code with stopgap solutions that may not be the proper way or efficient way of dealing with it because I do not understand why the methods are setup the way they are. 

For example, I introduced a root parent into my model so that I could parent all my later items under like so

class SceneGraphModel(QtCore.QAbstractItemModel):

        def __init__(self, parent=None):
            super(SceneGraphModel, self).__init__(parent)
            self._rootNode=Node(None).

However, if I attempted a self.model.rowCount(self.model._rootNode), which should give me 0 when queried before I add in any rows I get a :

AttributeError: 'Node' object has no attribute 'isValid'

I want to get a rowCount() off this particular parent because when I want to insert new rows which are entered at the end of the row list, rather than at the begining if I do it manually. What should be the proper way of creating a main root parent? 

Justin Israel

unread,
Oct 25, 2013, 6:55:12 AM10/25/13
to python_in...@googlegroups.com
My recommendation is to try your model against ModelTest, which has been ported from Qt to PySide (You should be able to just change the PySide import to PyQt4):

It tests your model to make sure it implements all the basic that could lead to issues. As it points out failures you can look at the test and adjust your model to properly handle each test.

Usage would be like this:
myModel = listbuild.SceneGraphModel()
test = ModelTest.ModelTest(myModel)
# exceptions will be raised when tests fail




--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

Bay

unread,
Oct 26, 2013, 2:08:03 PM10/26/13
to python_in...@googlegroups.com
Hi Justin, 
              thanks for the recommendation, I'd be sure to give it a try. Though it seems to me the problem is that the model view hates having to deal with 0 and as long as I avoid doing any checking before I add anything to the model it should be ok. 
Reply all
Reply to author
Forward
0 new messages