将许多QButtons与许多QLineEdits关联

2024-10-02 08:16:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在构建的应用程序如下:enter image description here

该应用程序将包括多个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关联起来呢?你知道吗


Tags: 代码文本self应用程序颜色按钮对话框click
2条回答

当您有两个以某种方式交互的小部件时,创建一个自定义小部件来为您处理所有事情通常会更简单。我已经编写了一个定制的QColorWidgethere,它显示一个按钮,一旦按下它就会显示一个颜色对话框。选择一种颜色会改变按钮的颜色,但在QLineEdit中显示为十六进制颜色很简单。原始代码为:

class QColorButton(QPushButton):
    '''
    Custom Qt Widget to show a chosen color.

    Left-clicking the button shows the color-chooser, while
    right-clicking resets the color to None (no-color).    
    '''

    colorChanged = pyqtSignal()

    def __init__(self, *args, **kwargs):
        super(QColorButton, self).__init__(*args, **kwargs)

        self._color = None
        self.setMaximumWidth(32)
        self.pressed.connect(self.onColorPicker)

    def setColor(self, color):
        if color != self._color:
            self._color = color
            self.colorChanged.emit()

        if self._color:
            self.setStyleSheet("background-color: %s;" % self._color)
        else:
            self.setStyleSheet("")

    def color(self):
        return self._color

    def onColorPicker(self):
        '''
        Show color-picker dialog to select color.

        Qt will use the native dialog by default.

        '''
        dlg = QColorDialog(self)
        if self._color:
            dlg.setCurrentColor(QColor(self._color))

        if dlg.exec_():
            self.setColor(dlg.currentColor().name())

    def mousePressEvent(self, e):
        if e.button() == Qt.RightButton:
            self.setColor(None)

        return super(QColorButton, self).mousePressEvent(e)

我们可以修改它来显示一个QLineEdit相邻的元素,如下所示。我们不是直接从QPushButton继承,而是从泛型QWidget继承。为此,我们设置了一个包含QLabelQPushButton的布局。我们现在不再在内部存储颜色(作为_color),而是通过QLabel.text()QLabel.setText()QLabel中设置和检索颜色。你知道吗

class QColorLineEditButton(QWidget):
    '''
    Custom Qt Widget to show a chosen color alongside a QLineEdit.

    Left-clicking the button shows the color-chooser, while
    right-clicking resets the color to None (no-color).    
    '''

    colorChanged = pyqtSignal()

    def __init__(self, *args, **kwargs):
        super(QColorLineEditButton, self).__init__(*args, **kwargs)

        layout = QHBoxLayout()

        self.le = QLineEdit()
        self.le.setMaximumWidth(100)

        self.btn = QPushButton()
        self.btn.setMaximumWidth(32)

        layout.addWidget(self.le)
        layout.addWidget(self.btn)
        self.setLayout(layout)

        self.btn.pressed.connect(self.onColorPicker)

    def setColor(self, color):
        if color != self.le.text():
            self.le.setText(color)
            self.colorChanged.emit()

        if self.le.text():
            self.btn.setStyleSheet("background-color: %s;" % self.le.text())
        else:
            self.btn.setStyleSheet("")

    def color(self):
        return self.le.text()

    def onColorPicker(self):
        '''
        Show color-picker dialog to select color.

        Qt will use the native dialog by default.

        '''
        dlg = QColorDialog(self)
        if self.le.text():
            dlg.setCurrentColor(QColor(self.le.text()))

        if dlg.exec_():
            self.setColor(dlg.currentColor().name())

这就是它的样子:

Screenshot of QColorLineEditButton

你可以像平常一样把它添加到你的表单中

color1 = QColorLineEditButton()
layout.addWidget(color1)

然后使用color()访问函数读取值。你知道吗

current_color_1 = color1.color()

然而,如果您要在一个表单上处理很多小部件,这可能会变得非常麻烦。在这种情况下,我可以为PyQt配置管理器模块PyQtConfig制作一个小插件,它允许您通过标准Python dict接口访问(设置和获取)多个小部件。你知道吗

我建议你做以下三个步骤。你知道吗

步骤1:在设置小部件的UI时或之后,创建一个字典,其中所有按钮作为键,所有行编辑作为值。你知道吗

self.button_map = {}
self.button_map[self.but1] = self.edit1
self.button_map[self.but2] = self.edit2
..

步骤2:将所有按钮连接到小部件中的相同方法。我想你已经这么做了。你知道吗

self.but1.clicked.connect(self.buttons_clicked)
self.but2.clicked.connect(self.buttons_clicked)
..

步骤3:在连接到所有按钮的方法中获取sender,从之前创建的字典中查找相应的行编辑,并相应地设置文本。你知道吗

def buttons_clicked(self):
  text = color_dialog_get_some_text() # customize this
  self.button_map[self.sender].setText(text)

相关问题 更多 >

    热门问题