我想知道如何创建(使用 PyQt4)派生的 QTabWidget
,每个选项卡标题旁边都有一个复选框?像这样:
最佳答案
实际上我选择了只继承QTabWidget。
复选框在创建新选项卡时添加并保存到列表中以取回其索引。
setCheckState/isChecked 方法旨在控制由其选项卡索引指定的每个复选框的状态。
最后,捕获并发送“stateChanged(int)”信号,并带有一个指定相关复选框索引的额外参数。
class CheckableTabWidget(QtGui.QTabWidget):
checkBoxList = []
def addTab(self, widget, title):
QtGui.QTabWidget.addTab(self, widget, title)
checkBox = QtGui.QCheckBox()
self.checkBoxList.append(checkBox)
self.tabBar().setTabButton(self.tabBar().count()-1, QtGui.QTabBar.LeftSide, checkBox)
self.connect(checkBox, QtCore.SIGNAL('stateChanged(int)'), lambda checkState: self.__emitStateChanged(checkBox, checkState))
def isChecked(self, index):
return self.tabBar().tabButton(index, QtGui.QTabBar.LeftSide).checkState() != QtCore.Qt.Unchecked
def setCheckState(self, index, checkState):
self.tabBar().tabButton(index, QtGui.QTabBar.LeftSide).setCheckState(checkState)
def __emitStateChanged(self, checkBox, checkState):
index = self.checkBoxList.index(checkBox)
self.emit(QtCore.SIGNAL('stateChanged(int, int)'), index, checkState)
这可能不是做事的完美方式,但至少它仍然非常简单并且满足了我的所有需求。
关于python - 标题为 CheckBox 的 QTabWidget,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5818387/