如何获得许多QComboBoxes'来自QTableWidget的文字

时间:2014-08-19 14:13:32

标签: python qt qt4 pyqt pyqt4

我已经将大量的 QComboBox插入到带有setCellWidget的QTableWidget中(我不知道qcombobox的数量,因为它来自MySQL)。但是,当我想从表中获取其文本时

self.table.item(0,1).itemText() 

self.table.item(0,1).text() 

self.table.item(0,1).currentText() 

它不起作用。 通常我可以使用combobox.currentText()获取文本但是表有很多组合框,我不知道行和列(x,y)信息。所以我应该使用像 .item(14,1).text()

这样的东西

3 个答案:

答案 0 :(得分:4)

如果您使用过setCellWidget,则调用cellWidget(0,1)而不是item(0,1)会返回QWidget而不是QTableWidgetItem。

您可能需要将此QWidget转换为QComboBox,但之后您应该可以调用currentText()

答案 1 :(得分:0)

如果你想使用;

self.table.item(0,1).itemText() 

self.table.item(0,1).currentText() 

self.table.item(0,1).text() 

我无法直接使用item小部件。我建议您使用项目委托QtGui.QItemDelegate,有关更多信息,您可以在qt示例中搜索以实现此委托,我有一些例子,希望是有帮助的;

import sys
from PyQt4 import QtGui, QtCore

class QCustomDelegate (QtGui.QItemDelegate):
    def __init__(self, *args, **kwargs):
        QtGui.QItemDelegate.__init__(self, *args, **kwargs)
        self.listsData = ['Data 1', 'Data 2', 'Data 3']

    def createEditor (self, parentQWidget, optionQStyleOptionViewItem, indexQModelIndex):
        column = indexQModelIndex.column()
        if column == 0:
            editorQWidget = QtGui.QComboBox(parentQWidget)
            editorQWidget.clear()
            for data in self.listsData:
                editorQWidget.addItem(data)
            return editorQWidget
        else:
            return QtGui.QItemDelegate.createEditor(self, parentQWidget, optionQStyleOptionViewItem, indexQModelIndex)

    def setEditorData (self, editorQWidget, indexQModelIndex):
        column = indexQModelIndex.column()
        if column == 0:
            index, _ = indexQModelIndex.model().data(indexQModelIndex, QtCore.Qt.EditRole).toInt()
            editorQWidget.setCurrentIndex(index)
        else:
            QtGui.QItemDelegate.setEditorData(self, editorQWidget, indexQModelIndex)

    def setModelData (self, editorQWidget, modelQAbstractItemModel, indexQModelIndex):
        column = indexQModelIndex.column()
        if column == 0:
            index = editorQWidget.currentIndex()
            modelQAbstractItemModel.setData(indexQModelIndex, index, QtCore.Qt.EditRole)
        else:
            QtGui.QItemDelegate.setModelData(self, editorQWidget, modelQAbstractItemModel, indexQModelIndex)

    def updateEditorGeometry(self, editorQWidget, optionQStyleOptionViewItem, indexQModelIndex):
        column = indexQModelIndex.column()
        if column == 0:
            editorQWidget.setGeometry(optionQStyleOptionViewItem.rect)
        else:
            QtGui.QItemDelegate.updateEditorGeometry(self, editorQWidget, optionQStyleOptionViewItem, indexQModelIndex)

    def paint (self, painterQPainter, optionQStyleOptionViewItem, indexQModelIndex):
        column = indexQModelIndex.column()
        if column == 0:
            value, _ = indexQModelIndex.model().data(indexQModelIndex, QtCore.Qt.EditRole).toInt()
            self.drawDisplay(painterQPainter, optionQStyleOptionViewItem, optionQStyleOptionViewItem.rect, QtCore.QString(self.listsData[value]));
        else:
            QtGui.QItemDelegate.paint(self, painterQPainter, optionQStyleOptionViewItem, indexQModelIndex)

class QCustomTableWidget (QtGui.QTableWidget):
    def __init__ (self, parent = None):
        super(QCustomTableWidget, self).__init__(parent)
        # Setup row & column data
        listsVerticalHeaderItem = ['Device 1', 'Device 2', 'Device 3', 'Device 4', 'Device 5']
        self.setRowCount(len(listsVerticalHeaderItem))
        for index in range(self.rowCount()):
            self.setVerticalHeaderItem(index, QtGui.QTableWidgetItem(listsVerticalHeaderItem[index]))
        self.setColumnCount(5)
        listsHorizontalHeaderItem = ['Option 1', 'Option 2']
        self.setColumnCount(len(listsHorizontalHeaderItem))
        for index in range(self.columnCount()):
            self.setHorizontalHeaderItem(index, QtGui.QTableWidgetItem(listsHorizontalHeaderItem[index]))
        self.myQCustomDelegate = QCustomDelegate()
        self.setItemDelegate(self.myQCustomDelegate)
        # Test insert data
        index = 2
        self.setItem(0, 0, QtGui.QTableWidgetItem(str(index)))
        # Test read data
        index = int(self.item(0, 0).text())
        print self.myQCustomDelegate.listsData[index]


if __name__ == '__main__':
    myQApplication = QtGui.QApplication(sys.argv)
    myQCustomTableWidget = QCustomTableWidget()
    myQCustomTableWidget.show()
    sys.exit(myQApplication.exec_())

QtGui.QItemDelegate参考http://pyqt.sourceforge.net/Docs/PyQt4/qitemdelegate.html

Spin Box委托示例(C ++,也可以在python中实现)http://qt-project.org/doc/qt-4.8/itemviews-spinboxdelegate.html


此致

答案 2 :(得分:-1)

self.nameoftablewidget.cellWidget(row,col).currentText()