简体   繁体   中英

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). 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. So I should use something like .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.

You may need to cast this QWidget to a QComboBox, but then you should be able to call 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;

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

Spin Box Delegate Example (C++, also your can implemented in python) : http://qt-project.org/doc/qt-4.8/itemviews-spinboxdelegate.html


Regards,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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