Skip to content Skip to sidebar Skip to footer

Qdatawidgetmapper Does Not Update In All Windows

I'm trying to get a multi window application in PyQt to work which should display in more than one window the data in different ways. Now I'm running into an issue I don't understa

Solution 1:

Explanation:

The problem is trivial but difficult to track but I am going to explain it step by step, but before doing so you have to understand that:

  • If you reset a model set to a mapper then the currentIndex will be -1.
  • Using select() resets the QSqlTableModel.

Steps:

  • In the first MainWindow you create the model (SqlModel), you set the model on the view (QTableView) and the mapper (QDataWidgetMapper), then you use select () and set the currentIndex of the mapper to be 0.

  • In the second MainWindow it is no longer necessary to create the module since the Singleton uses the cache, you set it in the view and the mapper, you call select that will set the currentIndex to -1 to the mapper of the first MainWindow, and then you set the currentIndex to 0 of the second mapper.

In conclusion: The currentIndex of the mapper of the first window will be -1 and instead the currentIndex of the second mapper is 0, so the first QLineEdit is not updated since it does not have an assigned row. The problem is caused by calling select() twice which resets the mappers of the previous windows.

Answer :

One possible solution is to only use select() once:

classMainWindow(QMainWindow):
    def__init__(self):
        super().__init__()
        self.setMinimumSize(QSize(400, 200))
        widget = QWidget()
        layout = QVBoxLayout()
        widget.setLayout(layout)
        self.setCentralWidget(widget)

        self.table = QTableView()
        layout.addWidget(self.table)

        self.inputField = QLineEdit()
        layout.addWidget(self.inputField)

        self.model = SqlModel()

        ifnotgetattr(self.model, "is_loaded", False):
            self.model.setTable("Track")
            ifnot self.model.select():
                print(self.model.lastError().text())
            self.model.is_loaded = True

        self.table.setModel(self.model)

        self.mapper = QDataWidgetMapper()
        self.mapper.setModel(self.model)

        self.mapper.addMapping(self.inputField, 0)

        self.mapper.toFirst()

Another possible solution is that the information load (set the table and use select()) is done in the constructor.

Post a Comment for "Qdatawidgetmapper Does Not Update In All Windows"