0

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.

Dav k
  • 150
  • 1
  • 12

1 Answers1

0

The second argument of QPushButton is parent widget co you can change clicked function to:

   def clicked(self):
        self.ID+=1 # defined currectly in the class.
        self.a += [QPushButton(f'btn{self.ID}', self)] #tried .append(btn) too
        self.a[-1].move(300, 300)
        self.a[-1].show()
        print(len(self.a)) #prints 21 after one click (on a[1]) and so on...

Then it should shown.

More generic. All Qt widget has function setParent co it can be written as:

   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].setParent(self)
        self.a[-1].move(300, 300)
        self.a[-1].show()
        print(len(self.a)) #prints 21 after one click (on a[1]) and so on...
Grzegorz Bokota
  • 1,736
  • 11
  • 19
  • the original lines are: ```python self.a = self.a + [Button(f'Button{self.ID}', self)] ``` and the class defined: ```python class Button(QPushButton): def __init__(self, title, parent): super().__init__(title, parent) ``` and It still not working :/ – Dav k Aug 08 '19 at 21:34
  • I do not understand this comment. – Grzegorz Bokota Aug 08 '19 at 21:36
  • It's not showing the element even when I'm setting the parent. <__main__.Ex object at 0x000002204169E0D8> I tested the parentship and this is the result. – Dav k Aug 08 '19 at 21:42
  • Ok. I see. I test and add usage of `show()` function. On my computer it work. I'm not sure why `show` is needed. – Grzegorz Bokota Aug 08 '19 at 21:54
  • Yes, Suddenly it worked for me too. Although I'm pretty sure I tried this. Thanks man! – Dav k Aug 08 '19 at 21:57
  • The weird fact is that I don't even need to store them any more in an array. – Dav k Aug 08 '19 at 22:01
  • Yes, you do not need, because `addParent` add the reference to button in parent widget. – Grzegorz Bokota Aug 08 '19 at 22:06