Claud N'guetta
Claud N'guetta

Reputation: 1

Use QCheckbox to change data and view for a QStandardItemModel

I want to have a QChecbox that when checked it will make the model fetch some type of data and show it, and when is not to fetch some other kind of data. The problem that I find is that I am not able to update the model view. I am printing the data and it seems to be fetching the correct data as I press the checkbox, but the view stays the same and I can not figure out why.

Here is my code:

This first bit is supposed to retrieve the data and it works, as the print(self._items) gives the right output.

from PySide2.QtWidgets import *
from PySide2 import QtCore
from PySide2.QtGui import QColor, QFont
from PySide2.QtGui import QStandardItemModel, QStandardItem
 
import smart_read
 
import sys
import collections
from collections import Counter
import nuke
 
 
class ShotModel(QStandardItemModel):
    def __init__(self):
        super(ShotModel, self).__init__()
        self.rootItem = self.invisibleRootItem()
        self._items = {}
        #self.set_items()
        
    def set_items(self,elements_collapsed):
        shots_path = smart_read.find_current_job()
        self.elements_collapsed = elements_collapsed
 
        if self.elements_collapsed == False:
            print("False")
            all_elements = smart_read.Job(shots_path).find_JOB_elements()
            self._items = all_elements
            print(self._items)
            for shot,elements in sorted(self._items.items()):
                shot = QStandardItem(str(shot))
                self.rootItem.appendRow(shot)
 
                for element in elements:
                #print(element)
                    element = QStandardItem(str(element))
                    shot.appendRow(element)
            self.modelReset.emit()
 
        else:
            print("True")
 
            
            collapsed_elements = smart_read.Job(shots_path).find_JOB_elements()
            self._items = smart_read.collapse(collapsed_elements)
            print(self._items)
            for shot,elements in sorted(self._items.items()):
                shot = QStandardItem(str(shot))
                self.rootItem.appendRow(shot)
                for element in elements:
                    #print(element)
                    element = QStandardItem(str(element))
                    shot.appendRow(element)
            self.modelReset.emit()

On this second bit I create the Widget that contains my model.


class Panel(QWidget):
    def __init__(self):
        super(Panel, self).__init__()
        layout = QVBoxLayout()
        self.treeView = QTreeView()
        self.treeModel = ShotModel()
        self.treeView.setHeaderHidden(True)
 
        self.version_checkbox = QCheckBox("show only latest version")
        self.version_checkbox.setChecked(True)
        self.version_checkbox.clicked.connect(self.show_latest_version)
        layout.addWidget(self.version_checkbox)
        layout.addWidget(self.treeView)
 
        self.setLayout(layout)
        self.treeView.setModel(self.treeModel)
    

This function should then update the model data and view by calling the function above.

    def show_latest_version(self):
        sender = self.sender()
        if sender.isChecked():
            ShotModel().set_items(elements_collapsed=True)
            self.treeModel.modelReset.emit()
            self.treeView.setModel(self.treeModel)
        else:
            ShotModel().set_items(False)
            self.treeModel.modelReset.emit()
            self.treeView.setModel(self.treeModel)
 
 
panel = Panel()
panel.show()

So what I tried to do is to have a function (show_latest_version) that when the checkbox is chcked will be called. This should then update the dictionary from which the model takes the data from (self._items) and it actually does as when I am printing it the output is correct for both checked and unchecked. But this doesn't make the view change - the data seems to remain the same.

Upvotes: 0

Views: 55

Answers (1)

Claud N'guetta
Claud N'guetta

Reputation: 1

This is the solution found, using the same instance that was used before instead of calling a new one:

    def show_latest_version(self):
    sender = self.sender()
    if sender.isChecked():
        self.treeModel.set_items(True)
    else:
        self.treeModel.set_items(False)

Upvotes: 0

Related Questions