You can stop laughing now

You can now find version 0.0.2 posted in the Ephemeris Project section. I got the data classes created and the functions all lined up. This project is working so well that I’m actually starting to think that I’m no longer crazy. As promised here are some lessons learned with PyQt.

self.stupid = QWidget()

Although all classes can see all other classes, unless you specify “self” for your QWidget definitions they can’t be seen by any other functions within that class. It took me a while to figure out that “self” is needed. I’m sure there is some Id and Ego joke in there somewhere …

You can’t re-use widgets.

When creating QWidgets inside a loop, be sure to create unique QWidgets each time, even for non-visual artifacts. In this example I needed to create a list of TableWidgetItem() containing strings based on calculated dateTime() objects. I discovered that unique dateTime() items for each calculation had to be created in addition to the TableWidgetItem(). If the same dateTime() was used for the multiple calculations over and over, even when its associated to a unique TableWidgetItem(), Python eventually crashes if the subroutine is rerun. Weird, I know. Took me a while to fix it because the solution is counter-intuitive.

self.myTable = QTableWidget()
interval = 3600 # 1 hour example
neededRows = 744 #1 month example assuming above interval
stamps = []
cells = []
initialDateTime = self.timelineStartStamp.dateTime()
for n in range(0,neededRows):
     cells.append(QTableWidgetItem())
     stamps.append(initialDateTime.addSecs(n*interval))
     cells[n].setText(stamps[n].toString(“yyyy.MM.dd hh:mm:ss”))
     self.myTable.setItem(n,0,cells[n])

Signals and Slots.

Setting up signals is pretty easy, especially for QPushButtons and such. But if you’re needing to run a function triggered by a change in a spinbox, make sure you follow the self.connect definition exactly, otherwise it won’t work. In this example, when the spinbox is used an index is generated and passed to the function. The function NEEDS to accept the index, even if you’re not going to use it. Otherwise nothing happens.

def whatever(self)
    self.myBox = QSpinBox()
    self.myBox.setRange(0,89)
    self.connect(self.myBox,SIGNAL(“valueChanged(int)”), self.myBoxChanged)

def myBoxChanged(self, i):
    pass
    #continue function even if you don’t use i

 

Now for the cool part where I get to do work out some Astronomy calculations.

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