- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个动态创建的表,每行有 N 行和 M 个 QTableWidgetItems(仅用作复选框) - 无论何时选中或取消选中复选框,我都需要运行知道行和列的代码。
我的 CheckBox 子类如下所示:
class CheckBox(QTableWidgetItem):
def __init__(self):
QTableWidgetItem.__init__(self,1000)
self.setTextAlignment(Qt.AlignVCenter | Qt.AlignJustify)
self.setFlags(Qt.ItemFlags(
Qt.ItemIsSelectable | Qt.ItemIsUserCheckable | Qt.ItemIsEnabled ))
def stateChanged(self):
do_something(self.row(),self.column())
...
显然,这不会重新定义当 SIGNAL('stateChanged(int)')
-thingy 发生时调用的函数,因为什么也没有发生。
但是,如果我这样做:
item = CheckBox()
self.connect(item, SIGNAL('stateChanged(int)'), item.stateChanged)
在创建表的循环中,出现错误:
TypeError: arguments did not match any overloaded call:
QObject.connect(QObject, SIGNAL(), QObject, SLOT(), Qt.ConnectionType=Qt.AutoConnection): argument 1 has unexpected type 'CheckBox'
QObject.connect(QObject, SIGNAL(), callable, Qt.ConnectionType=Qt.AutoConnection): argument 1 has unexpected type 'CheckBox'
QObject.connect(QObject, SIGNAL(), SLOT(), Qt.ConnectionType=Qt.AutoConnection): argument 1 has unexpected type 'CheckBox
编辑:我还尝试重新定义 setCheckState()
但显然,当选中或取消选中该项目时,不会被调用。
编辑 2:此外,将连接更改为
self.connect(self.table, SIGNAL('itemClicked(item)'),
self.table.stateChanged)
其中table = QTableWidget()
也没有帮助。
我该如何正确地做到这一点?
最佳答案
最简单的解决方案可能是连接到QTableWidget
的cellChanged(int, int)
信号;看一下下面的例子:
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
#signal handler
def myCellChanged(row, col):
print row, col
#just a helper function to setup the table
def createCheckItem(table, row, col):
check = QTableWidgetItem("Test")
check.setCheckState(Qt.Checked)
table.setItem(row,col,check)
app = QApplication(sys.argv)
#create the 5x5 table...
table = QTableWidget(5,5)
map(lambda (row,col): createCheckItem(table, row, col),
[(row, col) for row in range(0, 5) for col in range(0, 5)])
table.show()
#...and connect our signal handler to the cellChanged(int, int) signal
QObject.connect(table, SIGNAL("cellChanged(int, int)"), myCellChanged)
app.exec_()
它创建一个 5x5 的复选框表;每当选中/取消选中其中之一时,都会调用 myCellChanged
并打印更改的复选框的行和列;当然,您可以使用QTableWidget.item(someRow, someColumn).checkState()
来查看它是否被选中或未选中。
关于python - 当 PyQt 中选中/取消选中某个 QTableWidgetItem 时,如何获取要执行的某些槽/函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3829349/
给定一个 QTableWidget,有没有办法为单元格 (QTableWidgetItem) 设置一个不同于显示值的“隐藏”值? 例如,我的单元格应显示“项目 1”文本,但双击它编辑应该仅针对值 1,
我想在QTableWidgetItem 中画一条线。为了划清界线,我重新实现了 QStyledItemDelegate::paint 方法。 但是,当我滚动或选择 QTableWidget 中的项目时
如果 QTableWidgetItem 已更改其文本,我正在寻找一种干净的方式来对事件使用react。我尝试使用 Signal QTableWidget::itemChanged(QTableWidg
我想将附加数据与插入表中的每个 QTableWidgetItem 相关联,以便将来在单击表项时使用该数据。但这些数据不应该是可见的。我怎样才能做到这一点? 最佳答案 您可以使用 QTableWidge
我在 PyQt5 中创建了一个表,并通过此方法检索单元格中的数据。但是当我尝试 QTableWidgetItem 中的单元格变为 None 时。那么,我如何解决这个问题以及如何在特定单元格中插入其他确
我想实现可以选择和取消选择 QTableWidget 内的多个 QTableWidgetItem 的功能,以便了解这些 QTableWidgetItem 的值和“位置”以供进一步使用。 QTableW
我正在使用 Qt 和 python(以及 pyqt)编写一个 QGIS 插件。 在 QtDesigner 中,我有一个 QTableWidget 对象,如果有办法获取列项类型,我真的很挣扎。 例如下表
我正在使用 PySide 库用 Python 编写一个应用程序。我有一个QTableWidget,它大约每秒更新一次。问题是,我希望能够手动更改数据,并且我认为如果我可以找出用户是否正在更改单元格中的
我的程序中有一个 QTableWidget。我希望它的行和列根据表格小部件中的时间条目改变颜色。当我第一次添加时间时,它将与当前系统时间进行比较,并且颜色更改确实生效,但随着时间的推移,它不会跟上并且
我有一个qtablewidgetitem,里面有一个QCheckbox 禁用qtablewigetitem时如下 flags = self.item(row+1, self.columns["
有没有办法在QTableWidgetItem中使用富文本? 目前我有这个: widgetItem = QtWidgets.QTableWidgetItem() widget = QtWidgets.Q
您可能知道,您可以使用 setCellWidget 为 QTableWidget 中的单元格设置小部件: table->setCellWidget(0, 0, new QProgressBar); 在
我正在尝试将 QTableWidgetItem 转换为子类。我有一个这样的类层次结构: (Parent -> Child) QTableWidgetItem -> SortableTableWidge
我有一个 QTableWidget 的子类,代码如下: connect(this, SIGNAL(cellChanged(int, int)), this, SLOT(pushCellChange(i
我有一个 QtGui.QTableWidgetItem,我通过下面的 createRow 函数添加到表格中: def createRow(self, listA): rowNum = self
以下 PyQt4 示例演示了我的问题。 QTableWidget 单元格中的文本很好地居中,直到您编辑单元格。当您编辑单元格时,文本左对齐。 我想在每个单元格的编辑过程中保持居中对齐。 import
我有一个QThread,它更新QTableWidget列值。当用户双击该列的一行时,QThread 停止更新值以让用户编辑上次读取的值。 如果用户按 Enter,则发出 cellChanged(int
我认为标题不言自明:给定一个 QTableWidget,其中的项目由 setItem 成员函数添加,我想知道每个项目的边距是多少。特别是,我想要这些单元格中左边距的宽度。 最佳答案 我准备了一个计算用
我有一个 QTableWidget,上面有一些 QTableWidgetsItems。有些项目使用复选框。我使用以下代码添加了复选框: QTableWidgetsItem->setCheckState
现在我可以在函数 eventFilter() 中处理我的 QTableWidget 中的所有按键(在调用 myTable->viewport()->installEventFilter( this);
我是一名优秀的程序员,十分优秀!