0

I can add a widget off the grid of a layout in the init function, but after the fact, I can only add widgets to the layout.

What's going on here?

import sys
from PyQt5.QtWidgets import QApplication, QDialog, QPushButton, QGridLayout 


class MyDialog(QDialog):
    def __init__(self):
        super(MyDialog, self).__init__()

        self.resize(600, 400)
        self.setLayout(QGridLayout())
        self.button_1 = QPushButton(self, text="First!")
        self.button_1.setGeometry(10, 30, 150, 50)
        self.button_2 = QPushButton(self, text="Add Another!")
        self.button_2.setGeometry(30, 50, 150, 50)
        self.button_1.clicked.connect(self.add_button)
        self.button_2.clicked.connect(self.add_button)

    def add_button(self):
        self.button_3 = QPushButton(self, text="New!")
        # doesn't work
        # self.button_3.setGeometry(50, 70, 150, 50)

        # can only add on layout
        self.layout().addWidget(self.button_3, 2, 1, 1, 1)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MyDialog()
    window.show()
    app.exec_()
Eugene Gill
  • 339
  • 4
  • 15

0 Answers0