Loading ui files, addItems in QListWidgets

72 views
Skip to first unread message

sand...@gmail.com

unread,
Apr 27, 2014, 9:41:39 AM4/27/14
to python_in...@googlegroups.com
Hi Guys!

I'm trying to learn how to use ui files instead of converting the ui files to python all the time. I use Nathan's http://dl.dropboxusercontent.com/u/1633130/PyQt%20tuts/PySide_loadUiType.py (but import it as "uic")


# --------------- file called myAwesome.py
__uiFile__ = path/ui.ui

form_class, base_class = uic.loadUiType(__uiFile__)

class AweSome(form_class, base_class):
    def __init__(self, parent=None):
        super(AweSome, self).__init__(parent)
        self.setupUi(self)
#---------------------------------------------------------

import myAwesome
a = myAwesome.AweSome()
a.show()


So that works and I see my ui. And I can also connect signals to my buttons using

self.btnPressMe.connect.clicked(self.btnPressMe_clicked())

But how to I populate QListWidgets? I normally get a list using this command.

category = [folder for folder in os.listdir(self.ASSETPATH) if os.path.isdir(os.path.join(self.ASSETPATH,folder))]

and then use: self.categorys.addItems(category)

But how to I use the QtGui.QListWidget.addItems() command now that I import my ui file? Does it make sense? Sorry for the n00b quesiton about this.

regards
stefan andersson


sand...@gmail.com

unread,
Apr 27, 2014, 10:31:00 AM4/27/14
to python_in...@googlegroups.com
never mind......


a = QtGui.QListWidget(self.listCategory.addItems(category))


Sorry for the noise

regards
stefan

Fredrik Averpil

unread,
Apr 27, 2014, 12:56:38 PM4/27/14
to python_in...@googlegroups.com
If you like, you can check out my boilerplate which intends to load .ui files exactly the same way whether you are using PySide or PyQt. Also, it's prepared to run inside Maya, Nuke or as standalone.


// Fredrik

Justin Israel

unread,
Apr 27, 2014, 3:53:45 PM4/27/14
to python_in...@googlegroups.com

Hey Stefan,

Are you sure that is the right syntax to be using?

a = QtGui.QListWidget(self.listCategory.addItems( category))

This is adding your items to and existing list widget from your UI file, and then creating a new list widget with the None return value. This widget will then most likely be garbage collected after the function scope ends because it didn't have a parent.

It is effectively the same as writting:

self.listCategory.addItems(category)
a = QtGui.QListWidget(None)

Because addItems() doesn't return anything. Isn't the first line all you want? listCategory comes from the name you gave your list widget in Qt Designer.

--
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/7c0a0efe-0abc-434e-acc1-25a0193d8885%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Stefan Andersson

unread,
Apr 27, 2014, 6:32:34 PM4/27/14
to python_in...@googlegroups.com
Hey Justin!
The syntax isn't correct :) Though I figured it all out. I'm writing (or improving) a update for my mini-asset system that I have for projects. And being swedish I named it "meatball" :)
It's was quite tricky to find out how to use ui files directly, but I managed to convert everything and I must say it's a lot better and not have to convert the ui files to python code. It was a good sunday exercise and skimming through this group helped a lot of the way. Some of the not-yet-cleaned-up code (pasted below) looks like this. 

regards
stefan


#--- snip ---
from meatball.util import uic

__uiFile__ = os.path.join(os.path.dirname(__file__), "gather.ui")

form_class, base_class = uic.loadUiType(__uiFile__)

class GatherAsset(form_class, base_class):
    def __init__(self, parent=None):
        super(GatherAsset, self).__init__(parent)
        self.setupUi(self)
        self.projectAssets = os.environ["ASSETS"]
        category = [folder for folder in os.listdir(self. projectAssets) if os.path.isdir(os.path.join(self. projectAssets,folder))]
        category.sort(key=lambda v: v.upper())
        QtGui.QListWidget(self.listCategory.addItems(category))
        QtGui.QGroupBox(self.infoBoxProject.setTitle("Assets for Project: %s" % os.path.basename(os.environ["JOB"])))
        QtGui.QGroupBox(self.infoBoxAsset.setTitle("INFO:"))
        self.ui_signals()

    def ui_signals(self):
        self.btnGather.clicked.connect(self.on_btnGather_clicked)
        self.listCategory.itemClicked.connect(self.on_listCategory_clicked)
        self.listName.itemClicked.connect(self.on_listName_clicked)
        self.listVersions.itemClicked.connect(self.on_listVersions_clicked)





For more options, visit https://groups.google.com/d/optout.



--
Stefan Andersson | Digital Janitor
social media -> "sanders3d"

Justin Israel

unread,
Apr 27, 2014, 6:49:13 PM4/27/14
to python_in...@googlegroups.com

I like Swedish meatballs. Its my favorite reason to go to IKEA.

Ya you are creating local qwidgets for no reason, using the None return value from the setters you are calling on the existing widgets

QtGui.QListWidget(self.listCategory.addItems(category))

QtGui.QGroupBox(self.infoBoxProject.setTitle("Assets for Project: %s" % os.path.basename(os.environ["JOB"])))

QtGui.QGroupBox(self.infoBoxAsset.setTitle("INFO:"))

Each one of those is just creating a widget with no parent that gets garbage collected after your constructor ends.

You just want:

self.listCategory.addItems(category)

self.infoBoxProject.setTitle("Assets for Project: %s" % os.path.basename(os.environ["JOB"]))

self.infoBoxAsset.setTitle("INFO:")

Stefan Andersson

unread,
Apr 27, 2014, 6:52:27 PM4/27/14
to python_in...@googlegroups.com
That's what I tried at first but that gave me errors (maya2014 osx). I'll give it a go again tomorrow.

Thanks for the tip.

Regards
Stefan


-- Sent from a phone booth in purgatory

Justin Israel

unread,
Apr 27, 2014, 9:09:59 PM4/27/14
to python_in...@googlegroups.com
If you post the errors you are getting from the fixed version, we can definitely debug it from there. 



Stefan Andersson

unread,
Apr 28, 2014, 2:40:34 AM4/28/14
to python_in...@googlegroups.com
Woke up and tried it, and of course you are right.... I have no idea of what I tested that didn't work out as it should.

Thanks for the heads up!

regards
stefan




For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages