1

I need to create buttons dynamically in a QMainWindow, and I'm trying it through the RefreshData() Slot function. The point is, despite the function runs and the Buttons are created, they are not linked to QMainWindow!! Buut when I call that function standalone, this Linking works. What could I be doing wrong, can't figure it out. LotOfThanks

array_stations = {}
a = Station("A", 0, 0, 0)
b = Station("B", 50, 50, 0)
c = Station("C", 100, 100, 0)
array_stations[a.ID] = a
array_stations[b.ID] = b
array_stations[c.ID] = c

class GuiView(QtGui.QMainWindow):

    def __init__(self):

        super(GuiView, self).__init__()
        self.initUI()

    def initUI(self):

        #CONTROLE DE ESTACOES - PARA CONTROLAR SE UMA ESTACAO EH NOVA OU NAO
        self.estacoes = {}

        #Set timer para atualizar Widget
        self.timer2 =QtCore.QTimer()

        self.timer2.timeout.connect(self.RefreshData)  ### THIS ONE DOESNT ADD THE BUTTONS....

        self.timer2.start(2000)

        self.RefreshData()  ### ... BUT THIS ONE DOES IT!
        self.layout = QtGui.QVBoxLayout()


    @pyqtSlot()
    def RefreshData(self):
        print "blabla"

        global array_stations

        ########## ADD OR UPDATE BUTTONS #################
        for s in array_stations:
            if not s in self.estacoes:
                # ADICIONO UM BOTAO A LISTA
                self.estacoes[s] = QtGui.QPushButton(s,self)  
                self.estacoes[s].move(array_stations[s].x,array_stations[s].y)
  • What do you mean "not linked"? I tried the code, and the buttons are all there, and in the correct locations, without explicitly having to call RefreshData. – mdurant Jul 31 '14 at 19:35

1 Answers1

0

Add into the loop:

self.estacoes[s].show()

EDIT:

I typed this response when I was in a bit of a hurry. To clarify a bit more, you are creating and adding the new buttons, but you're now telling them to show up. By default, widget's are not shown (including child widgets). But if you add new widgets, you will need to call show() for the new widget again to make it appear.

Benjamin
  • 1,223
  • 1
  • 13
  • 22