uiCode and mainCode > passing the QListWidget from the ui class in uiCode to a function in mainCode

65 views
Skip to first unread message

Padraig Ó Cuínn

unread,
Dec 9, 2015, 1:49:50 PM12/9/15
to Python Programming for Autodesk Maya
Hey there again...

so I have this uiCode.py with a QListWidget 

listWidget = QtGui.QListWidget(container)


also in uiCode.py my Signal Class

class ListWindow(QtGui.QMainWindow):


    refreshClicked
= Signal(list)


also in uiCode.py this function 

def refresh():
        window
.refreshClicked.emit(listWidget.item())
    button
.clicked.connect(refresh)

In my mainCode.py

        def refresh():
           
            listWidget
.clear()
            items
= pmc.selected()
           
for item in items:
                newItem
= QtGui.QListWidgetItem( item.nodeName())
                listWidget
.addItem(newItem)


           
print 'Print statement is Working...'


        _window
.refreshClicked.connect(refresh)

however, it says that listWidget is not referenced even tho it is imported with the uiCode.

Question : How do i actually get my the QListWidget and its QListWidgetItems referenced into the mainCode.py properly.

Justin Israel

unread,
Dec 9, 2015, 2:57:45 PM12/9/15
to Python Programming for Autodesk Maya
It's a little difficult to fully understand the context of the snippets you provided, but let me see if I understand you correctly. You are saying that you have a uiCode.py file which creates instances of your classes, and you import those instances and use them directly in your mainCode.py? If so, I would say that this design is not correct.

Your uiCode.py should only provide class definitions. Your mainCore.py should import that module, and then create instances of those classes, which is then owns and can use directly. You should not need to be importing a global listWidget instance from your uiCode.py. I am not clear on what the scope actually looks like in your uiCode module so I can't be sure where that instance is created and where it is accessible. 

You mainCode should be something along the lines of:
import uiCode

def main():
    app = QtGui.QApplication(...) # if not maya
    win = uiCode.ListWindow()
    # .. bunch of setup you want to do with win
    win.show()
    app.exec_() # if not maya
​
You can see that in this example, the main code imports the various ui element that it needs and creates instances that it now owns. You can connect extra signals at this point.

Justin
    

--
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/33a46e73-5745-45aa-b4e4-d1810e28d152%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Padraig Ó Cuínn

unread,
Dec 9, 2015, 3:25:04 PM12/9/15
to Python Programming for Autodesk Maya
still the same issue. 

here is the entire mainCopde.py


window
= None


def show():
   
global _window
   
if _window is None:
        cont
= uiCode.ListWindowController()





       
def refresh():


            listWidget
.clear()
            items
= pmc.selected()
           
for item in items:
                newItem
= QtGui.QListWidgetItem( item.nodeName())
                listWidget
.addItem(newItem)



           
print "This printed fine...
        _window.refreshClicked.connect(refresh)






    _window.show()

The rest is pretty much just a UI setup with a custom signal as stated in post One.

Justin Israel

unread,
Dec 9, 2015, 4:31:30 PM12/9/15
to Python Programming for Autodesk Maya
You code examples are completely wacky, so I can basically never follow much of what you are saying. Please just switch to using pastebin with more complete examples. You have various indentation problems that make it hard to track the flow. 

That being said, I don't understand the "window = None" at the root vs the "global _window" in your function. I still don't understand your refresh() function, which is still trying to reference a "listWidget" object which you have not created.

If you can post a proper pastebin, I will take a look and make comments. 

Justin


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

Padraig Ó Cuínn

unread,
Dec 9, 2015, 4:34:31 PM12/9/15
to python_in...@googlegroups.com

Hahaha wacky :-) sorry I thought it was funny. I'll be able to post in an out an hours time. A little busy. Thanks for the help Justin

Padraig

Message has been deleted
Message has been deleted

Padraig Ó Cuínn

unread,
Dec 9, 2015, 7:24:16 PM12/9/15
to Python Programming for Autodesk Maya
Ok so here are the links for you 

Here is the uiCode.py


Here is the mainCode.py(maya)

some of the previous code wasn't right such as _window being window

the statusbar is working perfectly in it so you wont need to worry about that unless you have something impressive to show me :)

Thanksies

Padraig

Justin Israel

unread,
Dec 9, 2015, 11:58:10 PM12/9/15
to Python Programming for Autodesk Maya
Ok, thanks for providing the pastebins. It is now completely clear why you are having the issue you reported.

In your uiCode module, you have that create_window() function. It creates a ListWindow instance, sets it up, and returns it. Then in your mainCode, you call create_window() which returns an instance of ListWindow. But then you try and set up a slot function that wants to refer to "listWidget". This is clearly not defined anywhere in the global space of uiCode. Even if it was defined in uiCode, you are referring to it without the "uiCode" namespace, which means you expect it to have been imported into the root of your mainCode.

More specifically... in the body of the create_window() function, you create a number of *local* variables. One of those local variables is "listWidget". I assume this is the variable you are trying to access. The problem is that you cannot access any local variables like that, from another module. They simply don't exist beyond the function. What you are trying to accomplish here should be done by making an accessor or just an attribute on your ListWindow class which stores the QListWidget in a way that it is easy to access. Your design of using a bunch of functions is probably working against you here. Since you already have a custom ListWindow class, can you  not just set all this up in the constructor? 

## uiCode.py

class ListWindow(QtGui.QMainWindow):

    refreshClicked = Signal(list)

    def __init__(self, controller, parent = None):
        super(ListWindow, self).__init__(parent)

        self.setWindowTitle('Padraigs List')
        # ..    
        container = QtGui.QWidget(self)
        # ...     
        self.listWidget = QtGui.QListWidget(container)
        # ...

## mainCode.py
#...
    def refresh():
        _window.listWidget.clear()
​
Or better yet, hide away the implementation details of the composed widgets, and expose just the API you want to be public...
## uiCode.py
class ListWindow(QtGui.QMainWindow):

    refreshClicked = Signal(list)

    def __init__(self, controller, parent = None):
        super(ListWindow, self).__init__(parent)

        self.setWindowTitle('Padraigs List')
        # ..    
        container = QtGui.QWidget(self)
        # ...     
        self.__listWidget = QtGui.QListWidget(container)
        # ...

    def clearList(self):
        self.__listWidget.clear()

## mainCode.py
#...
    def refresh():
        _window.clearList()
​
Also, I am not sure if this is causing a bug for you, or just something you did accidentally while simplifying the example, but you are emitting a python "list" type here and not any kind of value:
    def refresh():
        window.refreshClicked.emit(list)
    button.clicked.connect(refresh)
​

Justin

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

Padraig Ó Cuínn

unread,
Dec 10, 2015, 1:49:44 AM12/10/15
to Python Programming for Autodesk Maya
bah i tried both way and got confused where to put what the code changed accordingly but the items never returned to me in the listWidget further more i got no errors. its like the function didn't exist

Padraig Ó Cuínn

unread,
Dec 10, 2015, 2:49:10 AM12/10/15
to Python Programming for Autodesk Maya
would those methods not invalidate the Signal, but what you are doing is rather just calling up a function?

Justin Israel

unread,
Dec 10, 2015, 3:40:12 AM12/10/15
to python_in...@googlegroups.com
On Thu, Dec 10, 2015 at 7:49 PM Padraig Ó Cuínn <patchquin...@gmail.com> wrote:
bah i tried both way and got confused where to put what the code changed accordingly but the items never returned to me in the listWidget further more i got no errors. its like the function didn't exist

I don't really understand what you mean here. You have a function called create_window(), and it creates objects. These are all local variables. The only thing that leaves that function is the window object that you return. You are somehow expecting variables that are creating in another function of another module to be reachable from your main module. Not possible.

My example showed how to get rid if your functions and just do the configuration of the widget inside of the __init__()

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

Justin Israel

unread,
Dec 10, 2015, 3:41:39 AM12/10/15
to python_in...@googlegroups.com
On Thu, Dec 10, 2015 at 8:49 PM Padraig Ó Cuínn <patchquin...@gmail.com> wrote:
would those methods not invalidate the Signal, but what you are doing is rather just calling up a function?

The signal will still emit fine and call your slot fine. But you are emitting it with something that is pointless and unusable. It will pass the "list" builtin type to your slot, which you will just ignore. Why emit an argument that has no purpose?
 


On Wednesday, 9 December 2015 22:49:44 UTC-8, Padraig Ó Cuínn wrote:
bah i tried both way and got confused where to put what the code changed accordingly but the items never returned to me in the listWidget further more i got no errors. its like the function didn't exist

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

Padraig Ó Cuínn

unread,
Dec 10, 2015, 10:16:33 PM12/10/15
to Python Programming for Autodesk Maya
Hey the emit list was a coding typo, I wasnt really sure on what to do with it as you have a different setup than custom signals

Justin Israel

unread,
Dec 11, 2015, 12:57:11 AM12/11/15
to Python Programming for Autodesk Maya


On Fri, 11 Dec 2015 4:16 PM Padraig Ó Cuínn <patchquin...@gmail.com> wrote:

Hey the emit list was a coding typo, I wasnt really sure on what to do with it as you have a different setup than custom signals

You can just put nothing there for arguments. When you define a custom signal, you can tell it what types it expects to emit.

--
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/5a4a4482-210a-458b-9cb4-c73d06f38a5e%40googlegroups.com.

Padraig Ó Cuínn

unread,
Dec 11, 2015, 1:01:14 AM12/11/15
to python_in...@googlegroups.com

I've given it a second try and recode everything. I'll have another paste bin for you tomorrow. But as of right now I am getting weird errors that say __init__ only takes 2 arguments 3 given. I only used 2. And another saying emit is not an identified argument of signal :-/

Padraig

Padraig Ó Cuínn

unread,
Dec 11, 2015, 3:17:56 AM12/11/15
to Python Programming for Autodesk Maya
HI again I am finally getting somewhere, 

so I last thing i am trying to fix is the reList()  function which basically clears the listWidget and reitterates over them and adds them.

here is what I have so far

def reList():
      _window.listWidget.clear()
      items = pmc.selected()
      for item in items:
            newItem = QtGui.QListWidgetItem(item.nodeName())
            _window.listWidget.addItem(newItem)

Ive no idea what it is but it will only add them to the widget on first launch and not by the refreshbutton signal to the reList function above




Reply all
Reply to author
Forward
0 new messages