我有一个 QTableWidget 表,其中每行的第一列是 QComboBoxex,这又是表的 QTableWidgetItems,其余的是普通的文本输入单元格。
您能否告诉我如何从任何 QComboboxes 中获取“currentIndexChanged(int)”信号,这些信号会告诉我 comboboxex 从中触发它们的行号?
以下不起作用:
def insert_row_cb(self, table, cb_col):
rows = table.rowCount()
table.insertRow(rows)
self.set_row_items_cb(table, cb_col)
self.resize_rows(table)
return table
def set_row_items_cb(self, table, cb_col):
cb = QtGui.QComboBox()
signalMapper = QtCore.QSignalMapper()
rows = table.rowCount()
cols = table.columnCount()
for col in range(cols):
if col == cb_col:
table.setCellWidget(rows - 1, cb_col, cb)
signalMapper.setMapping(cb, table.item(0, 0))
cb.currentIndexChanged.connect(signalMapper.map)
signalMapper.mapped.connect(self.cb_index_changed)
else:
table.setItem(rows - 1, col, QtGui.QTableWidgetItem(''))
return table
def cb_index_changed(self, index):
print(index)
谢谢你。
* 编辑 - 我明白了!*
我的数据表类:
from PyQt4 import QtGui, QtCore
class DataTable(QtCore.QObject):
cb_index_changed_signal = QtCore.pyqtSignal(QtGui.QWidget)
def __init__(self, parent = None):
QtCore.QObject.__init__(self)
self.signalMapper = QtCore.QSignalMapper()
self.signalMapper.mapped[QtGui.QWidget].connect(self.on_signalMapper_mapped)
def insert_row_cb(self, table, cb_col):
rows = table.rowCount()
table.insertRow(rows)
self.set_row_items_cb(table, cb_col)
self.resize_rows(table)
return table
def set_row_items_cb(self, table, cb_col):
cb = QtGui.QComboBox()
cb.currentIndexChanged.connect(self.signalMapper.map)
rows = table.rowCount()
cols = table.columnCount()
for col in range(cols):
if col == cb_col:
table.setCellWidget(rows - 1, cb_col, cb)
cb.row = rows - 1
cb.column = cb_col
self.signalMapper.setMapping(cb, cb)
else:
table.setItem(rows - 1, col, QtGui.QTableWidgetItem(''))
return table
def on_signalMapper_mapped(self, cb):
self.cb_index_changed_signal.emit(cb)
在另一个类中,我创建了一个表:
from scripts.data_table import DataTable
self.data_table = DataTable(self)
self.consumption_table = self.data_table.insert_row_cb(self.consumption_table, 0)
self.data_table.cb_index_changed_signal.connect(self.cb_index_changed_signal)
def cb_index_changed_signal(self, cb):
print ("row: "+str(cb.row)+" column: "+str(cb.column)+" text: "+cb.currentText())