My widget’s got two thingys

I’ve posted version 0.0.1 of “Orbital Motion” on the site under the Ephemeris Project tab. The GUI development is basically finished. Nothing works, but the visual framework is there.

Things started working much better after I ditched QDesigner. Although its a beautiful tool, I found that I had much more control and understanding if I built each widget manually.

Everything in Qt is based on QWidgets with each having its own class, methods, and signals (basically OOP foundations). You can see them all here. Anytime you need something, you make a copy of its class then make changes to that copy by calling its methods. Once you understand that relationship, everything else becomes really easy. I’ve prepared the following diagram and programming example in the hopes of demonstrating this:

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class Main(QMainWindow):
    def __init__(self, parent=None):
       super(Main, self).__init__(parent)
       self.centralThingy = QWidget()
       self.centralLayout = QGridLayout()
       self.thingy1 = QRadioButton(“Thingy 1”)
       self.thingy1.setChecked(True)
       self.thingy2 = QRadioButton(“Thingy 2”)
       self.centralLayout.addWidget(self.thingy1,0,0)
       self.centralLayout.addWidget(self.thingy2,1,0)
       self.centralThingy.setLayout(self.centralLayout)
       self.setCentralWidget(self.centralThingy)

app = QApplication(sys.argv)
form = Main()
form.show()
app.exec_()

Step 1 – Define the Widget. A widget is a fancy name for a box to put stuff in.
Step 2 – Define the Layout. This explains how stuff appears in the box.
Step 3 – Define the Thingys. In this case, two radio buttons. But its could be anything.
Step 4 – Add the Thingys to the Layout. This tells the Layout how to show the stuff.
Step 5 – Set the Layout on the Widget. This puts the stuff in the box.
Step 6 – Set the Widget as the Central Widget. This makes the box visible.

Most PyQt classes follow this principle. The thingys could be tables, the layouts could be policies and the Central Widget could be … well just a plain old Widget, but you get the idea.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s