PyQt custom frameLayout widget

1,891 views
Skip to first unread message

Manuel Macha

unread,
May 10, 2012, 7:50:11 AM5/10/12
to python_in...@googlegroups.com
One thing I really miss with PyQt is having a Maya-style collapsible frameLayout readily available, so I hacked this together: http://pastebin.com/5y8tsBE7
It's pretty simple at the moment. There's neither a label nor an icon indicating the collapsed state.
Before I spend too much time on it, could you guys pls have a look and tell me if this is a valid approach? (I just started familiarizing myself with qt a few weeks ago)
In case this is way of doing things is not a good idea I'd appreciate if someone could push me into the right direction (or ideally share their working frameLayout code with the rest of us), otherwise any help in making this better is greatly appreciated.
Regards,
m
p.s. are there any websites that have custom pyqt widgets for download? I didn't really find anything through google.

Justin Israel

unread,
May 10, 2012, 11:47:41 AM5/10/12
to python_in...@googlegroups.com
I don't think there is anything wrong with the approach you are taking. This is the norm. The framework can't provide every type of functionality, but they do give you a ton of building blocks to make it easy to compose your own.

There isn't much to say about your code other than me nit picking a little :-)
But here are some small things...

    def mousePressEvent(self, *args, **kwargs):
        self.emit(QtCore.SIGNAL('clicked()'))
        return QtGui.QFrame.mousePressEvent(self, *args, **kwargs) 

An event method will only receive a single event argument, and you don't need to return anything. Right now this would be returning None all the time. You can just take the single event arg, and then call the superclass method with it.

Also, you might want to consider using the new-style signal-slots if you are just learning...
You can define signals as class attributes like this:

class TitleFrame(QtGui.QFrame):
    clicked = QtCore.pyqtSignal()

... And then you can emit like this:   

     self.clicked.emit()

... And connections to the slot in your other class would be like:   
    
    self.titleFrame.clicked.connect(self.printSomething)

Its much cleaner and easier to use. And you can create signals with different signatures and slots of the same name that take different signatures.

Thats pretty much it. Like I said, the rest is just nit-picking (I find it more obscure to define your UI setup in a bunch of smaller methods that you call in a row, when they depend on each other, such as needing to connect the signal and knowing that the UI object is there).


Manuel Macha

unread,
May 10, 2012, 10:20:16 PM5/10/12
to python_in...@googlegroups.com
Hi Justin,
many thanks for your help.
I've cleaned up the mousePressEvent method as suggested (the original was eclipse's suggested default syntax for overriding a method)
I'd prefer using the new-style signal-slots mechanism but in this case I couldn't get it to work, even trying several variations of the example that you've given.
As for breaking the UI-setup into a bunch of smaller methods. I think I saw that in some book and found it helped me with breaking stuff into meaningful subtasks. I'm aware that it inflates the code and it's uncommon to do things that way but for me it's working.
Anyways, here's how far I got with the frameLayout. I've added the collapse-arrow and a label:
http://pastebin.com/qYgDDYsB

Regards,
Manuel

Justin Israel

unread,
May 10, 2012, 10:29:50 PM5/10/12
to python_in...@googlegroups.com, python_in...@googlegroups.com
New style signal slots were introduced in Qt 4.5. Maybe you are using a really old version of Qt? 


Manuel Macha

unread,
May 11, 2012, 2:03:22 AM5/11/12
to python_in...@googlegroups.com
I'm on pyqt 4.7.3. The mistake that I was making was that I defined the signal inside my init method.
Thanks Justin, I'll definitely stick with the new syntax from now on.
p.s: here's a working example: http://pastebin.com/zcTVbat0

Jo Jürgens

unread,
May 14, 2012, 5:23:59 PM5/14/12
to python_in...@googlegroups.com
Nice work!

Blur Studios have published all their pipeline scripts, and there's a pretty nice collapsible groupBox there. Might be interesting to have a look at.

You can download a Windows installer from http://code.google.com/p/blur-dev/. If you just want the groupbox script, I pasted it here: http://pastebin.com/ZFUrj7sm

David Moulder

unread,
May 15, 2012, 10:09:46 AM5/15/12
to python_in...@googlegroups.com
Bur's implementation is very nice.  I've modified the widgets paintEvent method to add a Maya style look to them.  I can post back up if people are interested.

Dillon Bailey

unread,
May 15, 2012, 10:23:07 AM5/15/12
to python_in...@googlegroups.com
If you could post, that would be great!

--

David Moulder

unread,
May 16, 2012, 11:33:18 AM5/16/12
to python_in...@googlegroups.com
Sure ....


Now includes a Maya style and removed the triangle pixmap and used a QPolygon instead.  Made the text bold to match Maya also.
It's a near 1 - 1 match.

I did try to rotate the QPolygon with QMatrix.map(poly) but I think it rotated off screen.  If someone gets that working that would be nice to see.  Anyway, it was easy enough to describe 2 triangles for expanded and collapsed.

-Dave
Reply all
Reply to author
Forward
0 new messages