用于组合框的PyQt Custom QAbstractTableModel

时间:2014-03-17 10:27:43

标签: pyqt qcombobox

在下面的示例代码中,我想使用QDataWidgetMapper和Combobox来显示从数据库中检索的数据。

不幸的是我不能,例如预选组合框的选择,我无法在组合框中选择值时检索数据库ID。因此,我认为我真的错过了一些基本的东西。

我将程序缩减为此示例代码,我希望能够解释我的问题。

import sys
from PyQt4 import QtGui
from PyQt4 import QtCore


class ComboModel(QtCore.QAbstractTableModel):
    def __init__(self, parent=None, *args):
        super(ComboModel, self).__init__()
        # The data would normally be read from a database (not using the Qt SQL classes)
        self.dbIds   = [ 13, 49, 81 ]
        self.dbValues = [ 'Value 1', 'Value 2', 'Value 3' ]

    def rowCount(self, parent=QtCore.QModelIndex()):
        if parent.isValid():
            return 0
        return len(self.dbIds)

    def columnCount(self, parent=QtCore.QModelIndex()):
        if parent.isValid():
            return 0
        return 2

    def data(self, index, role=QtCore.Qt.DisplayRole): 
        print 'ComboModel data'
        if not index.isValid() or ( role != QtCore.Qt.DisplayRole and role != QtCore.Qt.EditRole ): 
            print 'ComboModel return invalid QVariant'
            return QtCore.QVariant()
        if index.column() == 0:
            return QtCore.QVariant(self.dbIds[index.row()])
        if index.column() == 1:
            return QtCore.QVariant(self.dbValues[index.row()])
        print 'ComboModel return invalid QVariant'
        return QtCore.QVariant()


class TableModel(QtCore.QAbstractTableModel):
    def __init__(self, parent=None, *args):
        super(TableModel, self).__init__()
        self.editValue = 'Value to edit'
        self.comboValue = 49 # This is the database id of the selected entry

    def rowCount(self, parent=QtCore.QModelIndex()):
        if parent.isValid():
            return 0
        return 1

    def columnCount(self, parent=QtCore.QModelIndex()):
        if parent.isValid():
            return 0
        return 2

    def data(self, index, role=QtCore.Qt.DisplayRole): 
        print 'TableModel data'
        if not index.isValid() or ( role != QtCore.Qt.DisplayRole and role != QtCore.Qt.EditRole ):
            print 'TableModel return invalid QVariant'
            return QtCore.QVariant()
        if index.column() == 0:
            return QtCore.QVariant(self.editValue)
        if index.column() == 1:
            return QtCore.QVariant(self.comboValue)
        print 'TableModel return invalid QVariant'
        return QtCore.QVariant()

    def setData(self, index, value, role=QtCore.Qt.DisplayRole):
        print 'TableModel setData'
        if not index.isValid():
            return False
        if index.column() == 0:
            print 'new Value: ' + value.toString()
            self.editValue = value.toString()
        if index.column() == 1:
            print 'new combobox value'
            print 'new Value: ' + value.toString()
            self.comboValue = value.toString() # how do I get the dbId?
        return True



class MainWindow(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)

        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('Test')

        self.edit = QtGui.QLineEdit(self)
        self.edit.setGeometry(10, 40, 80, 30)

        self.combo = QtGui.QComboBox(self)
        self.combo.setGeometry(10, 80, 80, 30)

        self.combo_model = ComboModel()
        self.combo.setModel(self.combo_model)
        self.combo.setModelColumn(1)

        self.model = TableModel()
        self.mapper = QtGui.QDataWidgetMapper()
        self.mapper.setModel(self.model)
        self.mapper.addMapping(self.edit, 0)
        self.mapper.addMapping(self.combo, 1)
        self.mapper.toFirst();


app = QtGui.QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()

0 个答案:

没有答案