Meloun
Meloun

Reputation: 15119

pyQt - how to make a widgets selection based on QGroupBox

In my application, I have three same QGroupBoxes with some pushbuttons.

I want to have same names for pushbuttons and acces to them through QGroupBox name.

Is that possible?

QGroupBox_one.button_export
QGroupBox_one.button_import

QGroupBox_two.button_export
QGroupBox_two.button_import

Than I could make method with QGroupBox as parameter and configure buttons easier. Thanks.

Upvotes: 1

Views: 973

Answers (1)

crispamares
crispamares

Reputation: 530

I think a clean way of doing what you want is making a new class (MyQGroupBoxes) that has the push buttons and the configure method you need.

from PyQt4 import QtGui

class MyQGroupBoxes(QtGui.QGroupBox):
    def __init__(self, parent):
        QtGui.QGroupBox.__init__(self, parent)

        self.button_export = QtGui.QPushButton("Export", self)
        self.button_import = QtGui.QPushButton("Import", self)

        layout = QtGui.QVBoxLayout(self)
        self.setLayout(layout)

        layout.addWidget(self.button_export)
        layout.addWidget(self.button_import)

    def config_export(self):
        # config your button
        pass

    def config_import(self):
        # config your button
        pass

if __name__ == "__main__":

    app = QtGui.QApplication(sys.argv)

    my_box1 = MyQGroupBoxes(None)
    my_box2 = MyQGroupBoxes(None)

    my_boxes = [my_box1, my_box2]

    # Config all the boxes
    for my_box in my_boxes:
        my_box.config_export()
        my_box.config_import()

        my_box.show()

    app.exec_()

Upvotes: 1

Related Questions