我正在构建的应用程序如下:
该应用程序将包括多个QToolButtons和QLineEdits。当按下qtool按钮时,始终会显示一个颜色对话框。颜色的十六进制代码被添加到LineEdit中。有没有可能每个QLineEdit都与一个QToolButton相关联,这样当按下一个按钮时,应用程序就会自动知道要向哪个LineEdit添加文本。目前我使用的代码是:
self.connect(self.bgbut, SIGNAL("clicked()"),self.bgbut_click) #bgbut is the QToolButton
然后打电话来
def bgbut_click(self):
但这对于我将添加的15个加线编辑和工具按钮来说是不切实际的。那么,有没有办法有效地将每个toolbutton与相应的LineEdit关联起来呢?你知道吗
当您有两个以某种方式交互的小部件时,创建一个自定义小部件来为您处理所有事情通常会更简单。我已经编写了一个定制的
QColorWidget
here,它显示一个按钮,一旦按下它就会显示一个颜色对话框。选择一种颜色会改变按钮的颜色,但在QLineEdit
中显示为十六进制颜色很简单。原始代码为:我们可以修改它来显示一个
QLineEdit
相邻的元素,如下所示。我们不是直接从QPushButton
继承,而是从泛型QWidget
继承。为此,我们设置了一个包含QLabel
和QPushButton
的布局。我们现在不再在内部存储颜色(作为_color
),而是通过QLabel.text()
和QLabel.setText()
从QLabel
中设置和检索颜色。你知道吗这就是它的样子:
你可以像平常一样把它添加到你的表单中
然后使用color()访问函数读取值。你知道吗
然而,如果您要在一个表单上处理很多小部件,这可能会变得非常麻烦。在这种情况下,我可以为PyQt配置管理器模块PyQtConfig制作一个小插件,它允许您通过标准Python dict接口访问(设置和获取)多个小部件。你知道吗
我建议你做以下三个步骤。你知道吗
步骤1:在设置小部件的UI时或之后,创建一个字典,其中所有按钮作为键,所有行编辑作为值。你知道吗
步骤2:将所有按钮连接到小部件中的相同方法。我想你已经这么做了。你知道吗
步骤3:在连接到所有按钮的方法中获取
sender
,从之前创建的字典中查找相应的行编辑,并相应地设置文本。你知道吗相关问题 更多 >
编程相关推荐