pyQt window is doesn't update until I change window

2.1k views Asked by At

I have a program that loads a file from the hard drive, with instructions to paint several squares of 50x50 pixels. I also have two ComboBoxes which should have an effect on the presented squares. Basically, the two ComboBoxes give the folder and the file name.

Everytime I call the ComboBox, I can tell that I call the paint event and the instructions used to paint the tiles are updated based upon the selection. However, the displayed squares aren't updated until I switch to another window and then turn back to the original window.

Here is how my paintEvent looks

def paintEvent(self,event):
    self.updateButtons()
    self.updateNameCombo()
    qp = QtGui.QPainter()
    qp.begin(self)
    self.paintTiles(qp)
    qp.end()
    return

updateButton is used to place the PushButton and ComboBox at the right side of the screen. UpdateNameComobo is used to update one of the comboBoxes and paintTiles is used to paint the squares on the screen.

  def paintTiles(self,qp):
        self.loadTileSet()
        width= self.frameSize().width()
        height = self.frameSize().height()
        self.endX = width - 120
        self.endY = width - 25
        x = self.startX
        y = self.startY
        i = self.startI
        while i < len(self.tiles):
            self.handleTile(qp,x,y,self.tiles[i])
            i += 1
            x += 60
            if x >= self.endX - 60:
                x = self.startX
                y += 60
            if y >= self.endY - 60:
                break
        return

loadTileSet is used to read the tile data from the hard drive. and handleTile is used to paint a single square.

    def handleTile(self,qp,x,y,tile):
        pen = QtGui.QPen(QtCore.Qt.blue, 1, QtCore.Qt.DotLine)
        for line in tile:
            r,g,b,a = tile[line]
            clr = QtGui.QColor(r,g,b,a)
            pen.setColor(clr)
            qp.setPen(pen)
            pX = x + line[0]
            pY = y + line[1]
            qp.drawPoint(pX,pY)

So, what is holding back the drawing of the tiles?

1

There are 1 answers

0
Yotam On

I solved this by linking the ComboBoxes to a function that call the update function:

Linking,

self.nameCombo.currentIndexChanged.connect(self.comboChanged)

the function,

self.comboChanged(self):
  self.updateComboData()
  self.update()

Note that when loading the gui for the first time, the currentIndexChanged signal is sent.