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.

New contributor

Claud N’guetta is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật