具有不同args的子类QPushbutton

时间:2017-12-04 15:39:48

标签: python pyside

如何在Pyside中对QPushbutton进行子类化,但需要arg。在我的例子中,我需要arg是一个整数列表[0,0,0]。

我的目标是做到这一点,我可以像这样创建MyButton:

# GOAL
MyButton([0,255,0])

当传入包含值列表的争论时,应设置值self._data。我不确定我是否正确设置了此设置,因此我们非常感谢所有更正。

class MyButton(QtGui.QPushButton):

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

        self._data = stuff

    @property
    def data(self):
        return self._data

    @data.setter
    def data(self, value):
        self._data = value


MyButton([0,255,0])

更新:我注意到当我将该值传递给我的Init时,它似乎没有触发该属性的setter?这是为什么?如果您测试下面的代码,您将看到按钮的颜色在实例化时未设置。我该如何解决这个问题?

import os
import sys
import json
from PySide import QtCore, QtGui


class QColorSwatch(QtGui.QPushButton):

    colorClicked = QtCore.Signal(list)
    colorChanged = QtCore.Signal(list)

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

        self._color = stuff
        self.setMaximumWidth(22)
        self.setMaximumHeight(22)
        self.setAutoFillBackground(True)
        self.pressed.connect(self.color_clicked)

    @property
    def color(self):
        return self._color

    @color.setter
    def color(self, value):
        self._color = value
        pixmap = QtGui.QPixmap(self.size())
        pixmap.fill(QtGui.QColor(value[0], value[1], value[2]))
        self.setIcon(pixmap)
        self.colorChanged.emit(value)
        # swatch.setIconSize(pixmap.size() - QtCore.QSize(6,6))

    def color_clicked(self):
        self.colorClicked.emit(self.color)



class ExampleWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(ExampleWindow, self).__init__(parent)
        self.resize(300, 200)

        self.ui_swatch = QColorSwatch([255,0,0])

        # main layout
        main_layout = QtGui.QVBoxLayout()
        main_layout.setContentsMargins(5,5,5,5)
        main_layout.setSpacing(5)
        main_layout.addWidget(self.ui_swatch)

        main_widget = QtGui.QWidget()
        main_widget.setLayout(main_layout)
        self.setCentralWidget(main_widget)

        # Signals
        self.ui_swatch.colorClicked.connect(self.color_clicked)
        self.ui_swatch.colorChanged.connect(self.color_changed)

    def color_clicked(self, col):
        print 'CLICKED:', col
        self.ui_swatch.color = [255,0,0]

    def color_changed(self, col):
        print 'CHANGED:', col


def main():
    app = QtGui.QApplication(sys.argv)
    ex = ExampleWindow()
    ex.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:3)

您必须将stuff作为参数。

class MyButton(QtGui.QPushButton):
    def __init__(self, stuff, *args, **kwargs):
        super(MyButton, self).__init__(*args, **kwargs)

        self._data = stuff

    @property
    def data(self):
        return self._data

    @data.setter
    def data(self, value):
        self._data = value

if __name__ == '__main__':

    app = QtGui.QApplication(sys.argv)
    mainWin = MyButton([0,255,0])
    print(mainWin.data)
    mainWin.show()
    sys.exit(app.exec_())

更新

你必须使用self.color来使用setter。

class QColorSwatch(QtGui.QPushButton):

    colorClicked = QtCore.Signal(list)
    colorChanged = QtCore.Signal(list)

    def __init__(self, stuff, *args, **kwargs):
        super(QColorSwatch, self).__init__(*args, **kwargs)
        self._color = None
        self.color = stuff
        self.setMaximumWidth(22)
        self.setMaximumHeight(22)
        self.setAutoFillBackground(True)
        self.pressed.connect(self.color_clicked)

    @property
    def color(self):
        return self._color

    @color.setter
    def color(self, value):
        self._color = value
        pixmap = QtGui.QPixmap(self.size())
        pixmap.fill(QtGui.QColor(value[0], value[1], value[2]))
        self.setIcon(pixmap)
        self.colorChanged.emit(value)
        # swatch.setIconSize(pixmap.size() - QtCore.QSize(6,6))

    def color_clicked(self):
        self.colorClicked.emit(self.color)