微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何将Qcombobox数据存储到QTableView对象的数据表中

如何解决如何将Qcombobox数据存储到QTableView对象的数据表中

我正在尝试将 QComboBox 的 currentText() 信息存储在 QTableView 对象中。存储 QTableView 对象中的其他单元格,但 QComboBox 不会向模型发出信号。注释行显示我尝试发送信号失败。

请参阅下面的“我的”代码。这个简化版是从论坛借来的,并进行了编辑以表达我的观点。非常感谢任何帮助。

from PyQt5 import QtWidgets,QtCore

class Delegate(QtWidgets.QItemDelegate):
    def __init__(self,owner,choices):
        super().__init__(owner)
        self.items = choices
    def createEditor(self,parent,option,index):
        self.editor = QtWidgets.QComboBox(parent)
        self.editor.addItems(self.items)
        #self.editor.currentTextChanged().connect(self.oncomboBoxchanged,self.editor.currentIndex())
        return self.editor
    def paint(self,painter,index):
        value = index.data(QtCore.Qt.displayRole)
        style = QtWidgets.QApplication.style()
        opt = QtWidgets.qstyleOptionComboBox()
        opt.text = str(value)
        opt.rect = option.rect
        style.drawComplexControl(QtWidgets.qstyle.CC_ComboBox,opt,painter)
        QtWidgets.QItemDelegate.paint(self,index)
    def setEditorData(self,editor,index):
        value = index.data(QtCore.Qt.displayRole)
        num = self.items.index(value)
        editor.setCurrentIndex(num)
    def setModelData(self,model,index):
        value = editor.currentText()
        model.setData(index,QtCore.Qt.displayRole,QtCore.QVariant(value))
    def updateEditorGeometry(self,index):
        editor.setGeometry(option.rect)

    def oncomboBoxchanged(self,value):
        print("ComboBox changed :",value)

class Model(QtCore.QAbstractTableModel):
    def __init__(self,table):
        super().__init__()
        self.table = table
    def rowCount(self,parent):
        return len(self.table)
    def columnCount(self,parent):
        return len(self.table[0])
    def flags(self,index):
        return QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable
    def data(self,index,role):
        if role == QtCore.Qt.displayRole:
            return self.table[index.row()][index.column()]
    def setData(self,value,role):
        print('Not activated here if the comboBox is changed.')
        if role == QtCore.Qt.EditRole:
            self.table[index.row()][index.column()] = value
            print('Role here is EditRole.')
        else:
            print('Role is : ',role)
            print("Value :",value)
        return True

class Main(QtWidgets.QMainWindow):
    def __init__(self,parent=None):
        super().__init__(parent)
        # set combo Box choices:
        choices = ['apple','orange','banana']
        # create table data:
        table   = []
        table.append(['A',choices[0]])
        table.append(['B',choices[0]])
        table.append(['C',choices[0]])
        table.append(['D',choices[0]])
        # create table view:
        self.model     = Model(table)
        self.tableView = QtWidgets.QTableView()
        self.tableView.setModel(self.model)
        self.tableView.setItemDelegateForColumn(1,Delegate(self,choices))
        self.tableView.setEditTriggers(QtWidgets.QAbstractItemView.AllEditTriggers)
        # make combo Boxes editable with a single-click:
        for row in range( len(table) ):
            self.tableView.openPersistentEditor(self.model.index(row,1))
        # initialize
        self.setCentralWidget(self.tableView)
        self.setwindowTitle('Delegate Test')
        self.show()

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    main = Main()
    app.exec_()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。