2

我有一个 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())
4

1 回答 1

0

像这样的东西应该工作:

#!/usr/bin/env python
#-*- coding:utf-8 -*-

#---------
# IMPORT
#---------
from PyQt4 import QtGui, QtCore

#---------
# MAIN
#---------
class MyWindow(QtGui.QWidget):
    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent)

        numberRows    = 3
        numberColumns = 3

        self.tableWidget = QtGui.QTableWidget(self)
        self.tableWidget.setRowCount(numberRows)
        self.tableWidget.setColumnCount(numberColumns)

        self.signalMapper = QtCore.QSignalMapper(self)
        self.signalMapper.mapped[QtGui.QWidget].connect(self.on_signalMapper_mapped)

        for rowNumber in range(numberRows):
            for columnNumber in range(numberColumns):    
                comboBox = QtGui.QComboBox()
                comboBox.currentIndexChanged.connect(self.signalMapper.map)
                comboBox.addItems([
                    "{0}-{1}-{2}".format(rowNumber, columnNumber, itemNumber)
                    for itemNumber in range(3)
                ])
                comboBox.row = rowNumber
                comboBox.column = columnNumber

                self.tableWidget.setCellWidget(rowNumber, columnNumber, comboBox)

                self.signalMapper.setMapping(comboBox, comboBox)

        self.layoutVertical = QtGui.QVBoxLayout(self)
        self.layoutVertical.addWidget(self.tableWidget)

    @QtCore.pyqtSlot(QtGui.QWidget)
    def on_signalMapper_mapped(self, comboBox):
        print "row: {0} column: {1} text: {2}".format(
            comboBox.row,
            comboBox.column,
            comboBox.currentText()
        )

if __name__ == "__main__":
    import sys

    app = QtGui.QApplication(sys.argv)
    app.setApplicationName('MyWindow')

    main = MyWindow()
    main.resize(333, 111)
    main.show()

    sys.exit(app.exec_())
于 2013-08-25T14:48:05.203 回答