简体   繁体   English

如何从QTableWidget获取许多QComboBoxes的文本

[英]How to Get Many QComboBoxes' Text from QTableWidget

i have inserted lots of QComboBox into a QTableWidget with setCellWidget (I don't know the number of qcomboboxes because it's coming from MySQL). 我已经使用setCellWidget将许多 QComboBox插入到QTableWidget中(我不知道qcombobox的数量,因为它来自MySQL)。 But when I want to get its text from table with 但是当我想从表中获取其文本时

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

or 要么

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

or 要么

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

it doesn't work . 它不起作用 Normally I can get text with combobox.currentText() but table has many comboboxes and I don't know the row and column (x, y) info. 通常,我可以使用combobox.currentText()获取文本,但是表中有很多组合框,而且我不知道行和列(x,y)的信息。 So I should use something like .item(14,1).text() 所以我应该使用类似.item(14,1).text()的东西

If you've used setCellWidget , then calling cellWidget(0,1) instead of item(0,1) will return you a QWidget instead of a QTableWidgetItem. 如果您使用过setCellWidget ,那么调用cellWidget(0,1)而不是item(0,1)将返回一个QWidget而不是QTableWidgetItem。

You may need to cast this QWidget to a QComboBox, but then you should be able to call currentText() . 您可能需要将此QWidget强制转换为QComboBox,但随后您应该能够调用currentText()

If your want to use; 如果您想使用;

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

or 要么

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

or 要么

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

I can't use item widget directly. 我不能直接使用项目小部件。 I suggest your use item delegate QtGui.QItemDelegate , for more information your can search in qt example to implement this delegate, And I have little example, hope is helps; 我建议您使用项目委托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 reference : http://pyqt.sourceforge.net/Docs/PyQt4/qitemdelegate.html QtGui.QItemDelegate参考http : QtGui.QItemDelegate

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


Regards, 问候,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM