gpt4 book ai didi

python - 具有不同参数的 QPushbutton 子类

转载 作者:行者123 更新时间:2023-11-30 22:25:05 25 4
gpt4 key购买 nike

如何在 Pyside 中子类化 QPushbutton 但需要一个参数。在我的示例中,我需要 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()

最佳答案

您必须将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)

关于python - 具有不同参数的 QPushbutton 子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47636893/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com