本文整理汇总了Python中PySide.QtGui.QButtonGroup.id方法的典型用法代码示例。如果您正苦于以下问题:Python QButtonGroup.id方法的具体用法?Python QButtonGroup.id怎么用?Python QButtonGroup.id使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PySide.QtGui.QButtonGroup
的用法示例。
在下文中一共展示了QButtonGroup.id方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: PeriodicTableWidget
# 需要导入模块: from PySide.QtGui import QButtonGroup [as 别名]
# 或者: from PySide.QtGui.QButtonGroup import id [as 别名]
class PeriodicTableWidget(QWidget):
selectionChanged = Signal()
def __init__(self, parent=None):
QWidget.__init__(self, parent)
# Widgets, layouts and signals
self._group = QButtonGroup()
layout = QGridLayout()
layout.setSpacing(0)
for i in range(18):
layout.setColumnMinimumWidth(i, 40)
layout.setColumnStretch(i, 0)
for i in list(range(7)) + [8, 9]:
layout.setRowMinimumHeight(i, 40)
layout.setRowStretch(i, 0)
## Element
for z, position in _ELEMENT_POSITIONS.items():
widget = ElementPushButton(z)
widget.setCheckable(True)
layout.addWidget(widget, *position)
self._group.addButton(widget, z)
## Labels
layout.addWidget(QLabel(''), 7, 0) # Dummy
layout.addWidget(QLabel('*'), 5, 2, Qt.AlignCenter)
layout.addWidget(QLabel('*'), 8, 2, Qt.AlignCenter)
layout.addWidget(QLabel('**'), 6, 2, Qt.AlignCenter)
layout.addWidget(QLabel('**'), 9, 2, Qt.AlignCenter)
for row in [0, 1, 2, 3, 4, 5, 6, 8, 9]:
layout.setRowStretch(row, 1)
self.setLayout(layout)
# Signals
self._group.buttonClicked.connect(self.selectionChanged)
# Default
self.setColorFunction(_category_color_function)
def setColorFunction(self, func):
if not callable(func):
raise ValueError('Not a function')
self._color_function = func
# Redraw
for widget in self._group.buttons():
z = self._group.id(widget)
bcolor = func(z)
fcolor = 'white' if _calculate_brightness(bcolor) < 128 else 'black'
sheet = 'background-color: %s; color: %s' % (bcolor.name(), fcolor)
widget.setStyleSheet(sheet)
def colorFunction(self):
return self._color_function
def setMultipleSelection(self, multiple):
self._group.setExclusive(not multiple)
def isMultipleSelection(self):
return not self._group.exclusive()
def setSelection(self, selection):
def _uncheckedAll():
for widget in self._group.buttons():
widget.setChecked(False)
if selection is None:
_uncheckedAll()
self.selectionChanged.emit()
return
if isinstance(selection, (int, six.string_types)):
selection = [selection]
if not self.isMultipleSelection() and len(selection) > 1:
raise ValueError('Multiple selection mode is off. Cannot select more than one element')
_uncheckedAll()
for z in selection:
if isinstance(z, six.string_types):
z = ep.atomic_number(z.strip())
self._group.button(z).setChecked(True)
self.selectionChanged.emit()
#
def selection(self):
selection = set()
for widget in self._group.buttons():
if widget.isChecked():
selection.add(self._group.id(widget))
if self.isMultipleSelection():
return frozenset(selection)
#.........这里部分代码省略.........