4

I need for my app a groupbox with a button in title. Or something equivalent.

Here is my usecase.

The app reads a datastructure and build a propery panel out of it. Some properties can be added/removed via this property panel.

If a property is a terminal, like a string or an integer, the property is represented as a classic label plus the proper widget. I can easily add a delete button to this layout.

But if a property is more complex and has itself some subproperties, it is represented as a groupbox. The original QGroupBox allow only a string as a title while I would like to add a delete button there.

I am new to QT and may be missing something, any idea ?

LBarret
  • 1,113
  • 10
  • 23

2 Answers2

5

The widgets that come stock with PyQt are you basic building blocks and convenience widgets to cover the common usages and needs. But when you start to want custom widgets, you then have the freedom to subclass something that is close, and compose your own custom widgets.

Lets take the QGroupBox. Its basically a QFrame composed with a QLabel at the top. Then the class wraps some methods that allow you to set the text of this label. This basic widget would start out like:

group = QtGui.QGroupBox()
group.setTitle("FOO")

What we can do instead is take a QWidget, and add a QGroupBox with a blank label. And then place a button at an absolute position to the parent widget. We could have used a QFrame but using a QGroupBox gets you the instant styling you wanted.

class ButtonGroupBox(QtGui.QWidget):

    def __init__(self, parent=None):
        super(ButtonGroupBox, self).__init__(parent=parent)

        self.layout = QtGui.QVBoxLayout(self)
        self.layout.setContentsMargins(0,24,0,0)
        self.groupBox = QtGui.QGroupBox(self)
        self.button = QtGui.QPushButton("FOO", parent=self)
        self.layout.addWidget(self.groupBox)

        self.button.move(0, -4) 

You can expand this custom class with methods that let you change the text of the button. The margin of the widget at the top is neccessary to give you the extra space to layout the button above the GroupBox

jdi
  • 90,542
  • 19
  • 167
  • 203
4

Actually, the QGroupBox class allows you to place a check box next to the label via checkable property.

When the user interacts with the check box, it will emits the following signals:

void clicked(bool checked);
void toggled(bool on);

The corresponding methods and signals are equal in PyQt.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
borges
  • 3,627
  • 5
  • 29
  • 44