PyQt:QDataWidgetMapper不显示小部件

时间:2015-05-29 13:45:09

标签: python-3.x pyqt pyqt4

我有一个AbstractItemModel,我想映射到一些小部件。在窗口的构造函数中,我有:

    print("creating model")
    self.model = Model(self.cluster)
    print("creating mapper")
    self.mapper = QtGui.QDataWidgetMapper()
    print("setting model")
    self.mapper.setModel(self.model)
    print("setting root index")
    self.mapper.setRootIndex(self.model.root_index())
    print("setting index")
    self.mapper.setCurrentIndex(0)
    print("mapping widgets")
    self.mapper.addMapping(self.realNameLineEdit, 1)
    self.mapper.addMapping(self.emailLineEdit, 2)
    self.mapper.addMapping(self.infoPlainTextEdit, 3)
    print("done")

这导致以下输出:

creating model
Model.__init__()
Model.rowCount(<PySide.QtCore.QModelIndex(0,0,0x7faa2ff446a0,Model(0x17c6f50) )   at 0x7faa2d9ea4c8>) = 0
Model.rowCount(<PySide.QtCore.QModelIndex(0,0,0x7faa2ff446a0,Model(0x17c6f50) )   at 0x7faa2d9d2888>) = 1
creating mapper
setting model
setting root index
Model.root_index()
setting index
Model.rowCount(<PySide.QtCore.QModelIndex(-1,-1,0x0,QObject(0x0) )  at 0x7faa2ff50d08>) = 2
Model.index(0, 0, <PySide.QtCore.QModelIndex(-1,-1,0x0,QObject(0x0) )  at 0x7faa2ff50d08>)
Model.rowCount(<PySide.QtCore.QModelIndex(-1,-1,0x0,QObject(0x0) )  at 0x7faa2d9ea608>) = 2
Model.columnCount(<PySide.QtCore.QModelIndex(-1,-1,0x0,QObject(0x0) )  at 0x7faa2d9ea608>) = 4
mapping widgets
Model.index(0, 1, <PySide.QtCore.QModelIndex(-1,-1,0x0,QObject(0x0) )  at 0x7faa2ff50d08>)
Model.rowCount(<PySide.QtCore.QModelIndex(-1,-1,0x0,QObject(0x0) )  at 0x7faa2d9ea608>) = 2
Model.columnCount(<PySide.QtCore.QModelIndex(-1,-1,0x0,QObject(0x0) )  at 0x7faa2d9ea608>) = 4
Model.index(0, 2, <PySide.QtCore.QModelIndex(-1,-1,0x0,QObject(0x0) )  at 0x7faa2ff50d08>)
Model.rowCount(<PySide.QtCore.QModelIndex(-1,-1,0x0,QObject(0x0) )  at 0x7faa2d9ea608>) = 2
Model.columnCount(<PySide.QtCore.QModelIndex(-1,-1,0x0,QObject(0x0) )  at 0x7faa2d9ea608>) = 4
Model.index(0, 3, <PySide.QtCore.QModelIndex(-1,-1,0x0,QObject(0x0) )  at 0x7faa2ff50d08>)
Model.rowCount(<PySide.QtCore.QModelIndex(-1,-1,0x0,QObject(0x0) )  at 0x7faa2d9ea608>) = 2
Model.columnCount(<PySide.QtCore.QModelIndex(-1,-1,0x0,QObject(0x0) )  at 0x7faa2d9ea608>) = 4
done

对我来说,到目前为止看起来是正确的。但是没有映射的小部件显示任何数据。 Model.data()甚至根本没有被调用(我也有打印)。

当我在realNameLineEdit中输入内容并按回车时,我得到:

Model.setData(<PySide.QtCore.QModelIndex(0,1,0x7faa2da31908,Model(0x17c6f50) )   at 0x7faa2d9eadc8>, real name, 2)

看起来编辑正确地通过映射器到达模型。那么为什么不显示?

1 个答案:

答案 0 :(得分:1)

为了完整起见,从评论中复制: 在为映射显示列映射后,必须将mapper.to First()mapper.setCurrentIndex()或类似名称称为以显示任何数据。