View Single Post
Posts: 13 | Thanked: 20 times | Joined on Jun 2010
#1
I've been working on learning Python and PyQt for a few months now. I slowly come across various problems, and end up spending varying amounts of time finding solutions to the problems. One issue I find is that there's a lot of incomplete or incorrect examples floating around out there, which makes parsing through the possibilities somewhat difficult.

After reading a post about PyQt crashing, fatalsaint posted some code using a python lambda function to use with a clicked() signal to send to a slot/function. This is pretty helpful, and I thought there might be more useful tips out there people have, so I started this thread. This can include code snippets or sites out there that are helpful. This really isn't meant to be a question thread, though, just something to look through for ideas.

Here's a quick go of a few that I can quickly think of:

Identify the sender of a signal in a slot
This is a helpful snippet that effectively allows you to pass an argument to a clicked() signal. This is helpful when you don't want to have a lot of functions around, and you can simply use code inside a single function to do something.

Code:
def notifyMe(foo)
  print foo

x = lambda: notifyMe("they call him tim")
self.connect(b, SIGNAL("clicked()"), d.close)
self.connect(f, SIGNAL("clicked()"), x)
How to use Qt Designer .ui files
I like to use Qt Designer to design forms and then output the .ui files, from there, you can use pyuic4 to turn them into .py files. I had a hard time grasping how to use the .py files from there. I found this link helpful in a way I could understand. Some people prefer to integrate the object creation directly into their code, which is fine, but I have a lot of issues with doing that.


Qt Documentation
While the official PyQt documentation is nice, I've found some really nice things with the official Qt Documentation. One is that you can easily find all members available for an object. There's also some nice documentation about various frameworks within Qt.

Learn to use Qt CSS
Much like web development, CSS is also available within Qt. There's a list of examples that can be tried out just using Qt Designer. If you're using custom CSS, using palette() in your CSS definitions will allow you to use colors that the theme already knows about. As an example, this is an easy way to get the default blue color on your buttons when you want that color.

Depending on your needs, there's a number of built-in icons that are available for use.

Getting native Maemo feeling windows
I know this is covered elsewhere here, but I thought I'd throw it in here, too. To get nice slidey maemo-like windows in pyqt, do something like the following

Code:
USE_MAEMO = False


class showWindowQtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        
        self.ui = Ui_main_window()
        self.ui.setupUi(self)
        try:
            self.setAttribute(Qt.WA_Maemo5StackedWindow)
            USE_MAEMO = True
        except:
            USE_MAEMO = False
This will allow you to have the stacked effect with the windows making it look & feel like a 'real' Maemo application.

Are there other tips for common problems you've come across when working with PyQt that might be helpful for other people?
 

The Following 11 Users Say Thank You to timconradinc For This Useful Post: