[PyQt] QDataWidgetMapper

Sibylle Koczian Sibylle.Koczian at t-online.de
Tue Nov 27 20:47:35 GMT 2007


Am Dienstag, 27. November 2007 20:02:51 schrieb Sibylle Koczian:
>
> I'm just playing with the QDataWidgetMapper myself, so I looked at your
> project and tried some changes. Changing the model to a QStandardItemModel,
> QSqlQueryModel, QSqlTableModel (with a SQLite memory "database") didn't
> help. But if I take the combobox out, then the QLineEdit shows content.
>
Wrong. I had added errors of my own. The problem _does_ lie in your model, 
only I can't find it there. 

> In my own project I've got a main window containing a QTableView and a
> QDialog containing only QLineEdit controls using a QDataWidgetMapper. That
> works.
>
> Could it be that you can't mix controls using a model directly and controls
> using a QDataWidgetMapper in the same window? Looks very strange.
>
No, it's not that. If you replace your dwm.py with the following, only 
slightly longer class definition, the QLineEdit shows the right content and 
this content follows changes in the QComboBox.

-----------------------------------------------------------------------------
from PyQt4 import QtGui, QtCore

from ui_dwm import Ui_Dwm

# from mymodel import MyModel
import mymodel

class Dwm(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)

        self.ui = Ui_Dwm()
        self.ui.setupUi(self)

#       self.model = mymodel.MyModel()
        # new, koc
        self.model = self.makeModel()
        self.ui.comboBox.setModel(self.model)

        self.dwm = QtGui.QDataWidgetMapper()
        self.dwm.setModel(self.model)
        self.dwm.addMapping(self.ui.lineEdit, 1)
        self.dwm.toFirst()
        # new, koc
        self.connect(self.ui.comboBox, 
QtCore.SIGNAL("currentIndexChanged(int)"), 
                     self.dwm, QtCore.SLOT("setCurrentIndex(int)"))
        
    
    # new, koc
    def makeModel(self):
            model = QtGui.QStandardItemModel()
            for word in mymodel.words:
                ll = []
                ll.append(QtGui.QStandardItem(word['name']))
                ll.append(QtGui.QStandardItem(word['opposite']))
                model.appendRow(ll)
            return model
-----------------------------------------------------------------------------

This just replaces your model with a QStandardItemModell filled with the same 
data. Which most probably isn't what you really need. But I really can't see 
what's wrong with your model (some quite trivial corrections excepted, which 
didn't help).        

-- 
Dr. Sibylle Koczian


More information about the PyQt mailing list