Hermit Crab
Hermit Crab

Reputation: 13

PySide PyQt QDataWidgetMapper

I try to connect a QtGui.QPlainTextEdit to a Model with QDataWidgetMapper. I dont get any errors, just nothing in the TextEdit. I dont get it and i cant find good example Code.

Here is some ExampleCode. I really hope that someone could help me.

from PySide import QtCore, QtGui
import sys


class ComponentsListModel(QtCore.QAbstractListModel):
    def __init__(self, components=[], parent = None):
        super(ComponentsListModel, self).__init__(parent=None)
        self.components = components
        self.list = parent

    def rowCount(self, parent):
        return len(self.components)

    def data(self, index, role):
        row = index.row()

        if role == QtCore.Qt.DisplayRole:#index.isValid() and
            value = self.components[row]
            return value



class MainWindow(QtGui.QWidget):
    def __init__(self):
        super(MainWindow, self).__init__()
        self._build_ui()

    def _build_ui(self):
        self.layout = QtGui.QVBoxLayout()

        self.listView = QtGui.QListView()
        self.model = ComponentsListModel(components = ['1', '2', '3'])
        self.listView.setModel(self.model)
        self.text = QtGui.QPlainTextEdit()
        self.layout.addWidget(self.listView)
        self.layout.addWidget(self.text)
        self.setLayout(self.layout)

        self._mapper = QtGui.QDataWidgetMapper(self)
        self._mapper.setModel(self.model)
        self._mapper.setSubmitPolicy(QtGui.QDataWidgetMapper.AutoSubmit)
        self._mapper.addMapping(self.text, 0)
        self._mapper.toFirst()


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    app.setStyle('Plastique')
    mySW = MainWindow()
    mySW.show()
    sys.exit(app.exec_())

Upvotes: 1

Views: 660

Answers (1)

syedelec
syedelec

Reputation: 1300

You will need to add a condition for Qt.EditRole in your data function inside ComponentsListModel class

if role == Qt.EditRole: 
        value = self.components[row]
        return value

Upvotes: 2

Related Questions