0

I have built a application which contained multi QComboBox. On change, each one add buttons (about 100) in a grid layout, here a part of the called function:

        for x in xrange(1,100):
            my_list_shots.append(str(x))

        x=2 # line 1 has the menu
        y=1 # row
        incrementLineX=1 # every 10 we go to line 2 (x+1) and reset incrementLine and y (row) to 1
        for i in xrange(1,121):
            try:
                my_shot_number=my_list_shots[i]
                my_shot_number = "%03d"%int(my_shot_number)
                self.buttons = QtWidgets.QPushButton(my_shot_number,self)
                self.buttons.setStyleSheet("background-color:brown;color:white")
                self.buttons.clicked.connect(partial(self.my_open_mov,my_shot_number))
                self.my_grid_layout.addWidget(self.buttons,x,y,1,1)
            except Exception as e:
                self.buttons = QtWidgets.QPushButton("",self)
                self.buttons.setEnabled(False)
                my_shot_number=""               
            y=y+1
            incrementLineX=incrementLineX+1
            if incrementLineX>10:
                x=x+1
                y=1
                incrementLineX = 1
            self.buttons.clicked.connect(partial(self.my_open_mov,my_shot_number))

and so on... I worried about the memory, if I keep my window open all day long with many changes, I'll create each time 99 buttons? or not..? How can I properly build my buttons? Do I need to destroy them when I call the function?

In another way, what I want is to replace new buttons by old ones and I don't want to create thousand of buttons if I keep the windows open.

I just need the way to do it.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
guiillt
  • 9
  • 3
  • There is a related question https://stackoverflow.com/questions/43937179/can-signal-handlers-memory-leak-in-pyqt, but with no answer :/ – zvone Nov 17 '19 at 12:41
  • You can also [edit](https://stackoverflow.com/posts/58900514/edit) the question, instead of adding a comment ;) – zvone Nov 17 '19 at 12:44
  • @zvone. AFAICS, that related question has already been answered [here](https://stackoverflow.com/q/47941743/984421). If you agree, it could be flagged for moderator attention, and your bounty should probably be refunded. – ekhumoro Nov 17 '19 at 22:32
  • Yes, you need to explicitly disconnect the signals and delete all the buttons every time you replace them, otherwise your program will leak memory. – ekhumoro Nov 18 '19 at 15:03
  • @ekhumoro True. And this one is then also a duplicate of https://stackoverflow.com/q/47941743/984421, i guess. – zvone Nov 18 '19 at 20:14
  • Does this answer your question? [Lifetime of object in lambda connected to pyqtSignal](https://stackoverflow.com/questions/47941743/lifetime-of-object-in-lambda-connected-to-pyqtsignal) – zvone Nov 18 '19 at 20:14
  • @zvone I don't think that is an exact duplicate of this question. The OP is asking here how to replace the buttons and also cleanup properly, whereas the other one is only about cleanup for signals and slots. This question is also about PySide rather than PyQt, and the behaviour can often be different when it comes to implementation details like object lifetimes. – ekhumoro Nov 18 '19 at 20:45

1 Answers1

0

I have got the solution. I use deleteLater() for each created buttons before to rebuild new ones.

# DELETE THE OLD WIDGET IN THE LAYOUT GRID
def clearShotsWidget(self):
    while self.shotsWidgetLayout.count():
        item = self.shotsWidgetLayout.takeAt(0)
        item.widget().deleteLater()
        self.shotsWidgetLayout.removeItem(item)

The other idea is to put all the build buttons in a list and delete them before to create the new ones.

Thanks a lot

Gui

guiillt
  • 9
  • 3