QTabWidget与标题中的复选框

编程入门 行业动态 更新时间:2024-10-28 03:26:14
本文介绍了QTabWidget与标题中的复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想知道如何使用PyQt4创建一个派生的 QTabWidget 与每个选项卡标题旁边的复选框?像这样:

解决方案

实际上,我选择了只是子类QTabWidget。 在创建一个新标签页时添加复选框,并保存到列表,以便将其索引返回。 setCheckState / isChecked方法旨在控制由其标签索引指定的每个复选框的状态。 最后,stateChanged int)信号被捕获并且通过指定相关复选框的索引的额外参数被汇出。

class CheckableTabWidget (QtGui.QTabWidget): checkBoxList = [] def addTab(self,widget,title): QtGui.QTabWidget.addTab(self,widget,标题) 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)

这可能不是最好的方法,但至少它仍然很简单,涵盖我所有的需要。

I was wondering how to create (using PyQt4) a derived QTabWidget with a check box next to each tab title? Like this:

解决方案

Actually I chose to only subclass QTabWidget. The checkBox is added at the creation of a new tab and saved to a list in order to get its index back. setCheckState/isChecked methods are intended to control the state of each checkBox specified by its tab index. Finally, the "stateChanged(int)" signal is captured and remitted with an extra parameter specifying the index of the checkBox concerned.

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)

This is maybe not the perfect way to do things, but at least it remains pretty simple and covers all my needs.

更多推荐

QTabWidget与标题中的复选框

本文发布于:2023-11-07 08:58:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1566039.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:复选框   标题   QTabWidget

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!