如何构建一个标题中有按钮的QGroupBox?

发布于 2025-01-07 03:49:33 字数 311 浏览 0 评论 0 原文

我的应用程序需要一个标题中带有按钮的组框。 或者类似的东西。

这是我的用例。

该应用程序读取数据结构并从中构建一个属性面板。可以通过此属性面板添加/删除某些属性。

如果属性是终端,例如字符串或整数,则该属性将表示为经典标签加上适当的小部件。我可以轻松地向此布局添加删除按钮。

但是如果属性更复杂并且本身具有一些子属性,则它会表示为组框。原始的 QGroupBox 只允许一个字符串作为标题,而我想在那里添加一个删除按钮。

我是 QT 新手,可能会遗漏一些东西,有什么想法吗?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心不设防 2025-01-14 03:49:33

PyQt 附带的小部件是基本构建块和方便的小部件,可满足常见用法和需求。但是,当您开始需要自定义小部件时,您就可以自由地对相近的东西进行子类化,并编写您自己的自定义小部件。

让我们以 QGroupBox 为例。它基本上是一个由顶部的 QLabel 组成的 QFrame。然后该类包装了一些方法,允许您设置该标签的文本。这个基本的小部件将像这样开始:

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

我们可以做的是采用一个 QWidget,并添加一个带有空白标签的 QGroupBox。然后将按钮放置在父窗口小部件的绝对位置。我们本可以使用 QFrame,但使用 QGroupBox 可以让您立即获得所需的样式。

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) 

您可以使用可更改按钮文本的方法来扩展此自定义类。顶部小部件的边距是必要的,以便为您提供额外的空间来在 GroupBox 上方布局按钮

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

奶茶白久 2025-01-14 03:49:33

实际上,QGroupBox类允许您通过可检查属性

当用户与复选框交互时,会发出以下信号:

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

PyQt

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文