I'm trying to add buttons on click event without using a layout. How should I define the new QPushButtons?
The reason I'm not using the layout is because I don't want the button get arrange by the layout. after creating new button I change his position. and when new button added to the layout it rearrange the order including the ones I've moved away.
so, I tried to add a static array of buttons to the initialize function of the widget, and then append a new button to the array. but unfortunately it is not refreshing the buttons or just not showing them ( I print the amount of items in the button list and it was growing although no button was shown).
My conclusions were:
1) try to disable the layout fixed positioning on addWidget function. or
2) refresh the widget after adding button to the button array.
for 1. I couldn't find a solution except for (maybe) recreating a custom layout which I'm trying to avoid. and 2. couldn't find any solution and tried the .show and .hide functions on parent element
This code creates 20 different buttons, when the first 2 moved away.
class Ex(QWidget):
def __init__(self, parent):
super().__init__()
self.initUI()
self.ID = 20
def initUI(self):
self.a = [QPushButton(f'btn{i}') for i in range(20)]
self.a[0].move(55, 55)
self.a[1].move(88, 88)
self.a[1].clicked.connect(self.clicked) # I know only this connected to the function..
def clicked(self):
self.ID+=1 # defined currectly in the class.
self.a += [QPushButton(f'btn{self.ID}')] #tried .append(btn) too
self.a[-1].move(300, 300)
print(len(self.a)) #prints 21 after one click (on a[1]) and so on...
but after clicked activated the last object is not shown.
I expect clicked function to add the QPushButton widget to the Parent widget, without disordering all the other QPushButton positions. Both fixing the disorder by layout and showing a changed array of elements are acceptable. any other idea is blessed too.
p.s I've searched all over the web to find a solution without success, although there are few posts which almost close to what I need.