update multiple views on multiple dialogs connected to a single data model - python

i have Two forms with two views connected to the same dataModel class, but when i change data on one form it does not change on the other.
i am trying to get a comboBox on several different dialogs/forms to get updated when the underlying data changes from another dialog/form.
here is my code:
class Model(QStringListModel):
def __init__(self, parent=None):
super(Model, self).__init__(parent)
data = QStringList()
data << "one" << "two" << "three" << "four" << "five"
self.setStringList(data)
class Form_2(QDialog):
def __init__(self, parent=None):
super(Form_2, self).__init__(parent)
self.model = Model()
self.combo = QListView()
self.combo.setModel(self.model)
layout = QVBoxLayout()
layout.addWidget(self.combo)
self.setLayout(layout)
class Form_1(QDialog):
def __init__(self, parent=None):
super(Form_1, self).__init__(parent)
self.model = Model()
self.listView = QListView()
self.listView.setModel(self.model)
self.combo = QComboBox()
self.combo.setModel(self.model)
self.form2_button = QPushButton("Open Form_2")
layout = QVBoxLayout()
layout.addWidget(self.listView)
layout.addWidget(self.combo)
layout.addWidget(self.form2_button)
self.setLayout(layout)
self.connect(self.form2_button, SIGNAL("clicked()"), self.form_2)
def form_2(self):
self.ft = Form_2()
self.ft.show()
app = QApplication(sys.argv)
form = Form_1()
form.show()
app.exec_()

Here is a basic example of how multiple views based on the same model update themselves when the any of those views is modified:
#!/usr/bin/env python
#-*- coding:utf-8 -*-
from PyQt4 import QtGui, QtCore
class MyDialog(QtGui.QWidget):
def __init__(self, modelSource, parent=None):
super(MyDialog, self).__init__(parent)
self.tableView = QtGui.QTableView(self)
self.tableView.horizontalHeader().setStretchLastSection(True)
self.tableView.setModel(modelSource)
self.listView = QtGui.QListView(self)
self.listView.setModel(modelSource)
self.listView.setModelColumn(0)
self.comboBox = QtGui.QComboBox(self)
self.comboBox.setModel(modelSource)
self.comboBox.setModelColumn(1)
self.layoutGrid = QtGui.QGridLayout(self)
self.layoutGrid.addWidget(self.comboBox, 0, 0, 1, 2)
self.layoutGrid.addWidget(self.listView, 1, 0, 1, 1)
self.layoutGrid.addWidget(self.tableView, 1, 1, 1, 1)
class MyWindow(QtGui.QWidget):
_dialogs = []
def __init__(self, parent=None):
super(MyWindow, self).__init__(parent)
self.modelSource = QtGui.QStandardItemModel(self)
for rowNumber in range(3):
items = []
for columnNumber in range(3):
item = QtGui.QStandardItem()
item.setText("row: {0} column {1}".format(rowNumber, columnNumber))
items.append(item)
self.modelSource.appendRow(items)
self.labelDialogs = QtGui.QLabel(self)
self.labelDialogs.setText("Select a number of dialogs to create:")
self.spinBoxDialogs = QtGui.QSpinBox(self)
self.spinBoxDialogs.setValue(3)
self.pushButtonShow = QtGui.QPushButton(self)
self.pushButtonShow.setText("Show Dialogs!")
self.pushButtonShow.clicked.connect(self.on_pushButtonShow_clicked)
self.pushButtonClose = QtGui.QPushButton(self)
self.pushButtonClose.setText("Close Dialogs")
self.pushButtonClose.clicked.connect(self.on_pushButtonClose_clicked)
self.layoutHorizontal = QtGui.QHBoxLayout(self)
self.layoutHorizontal.addWidget(self.labelDialogs)
self.layoutHorizontal.addWidget(self.spinBoxDialogs)
self.layoutHorizontal.addWidget(self.pushButtonShow)
self.layoutHorizontal.addWidget(self.pushButtonClose)
#QtCore.pyqtSlot()
def on_pushButtonShow_clicked(self):
self._dialogs = []
dialogsNumber = self.spinBoxDialogs.value()
for dialogNumber in range(dialogsNumber):
dialog = MyDialog(self.modelSource)
dialog.show()
dialog.move(100, 100)
self._dialogs.append(dialog)
#QtCore.pyqtSlot()
def on_pushButtonClose_clicked(self):
for dialog in self._dialogs:
dialog.close()
self._dialogs = []
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
app.setApplicationName('MyWindow')
main = MyWindow()
main.show()
sys.exit(app.exec_())

Thanks to ccc.larc for giving me the answer. we basically have to only use one instance of the model, it doesn't work if there several instances of the model. so you always have to pass the same instance of the model to which ever form you want it to use.
if i understand correctly this is what #X.Jacobs is saying, i m just showing my new code which is a bit more simple and could be better understood what contrasting it with my previous code (that didn't work)
so my above code just needs a minor change.
class Model(QStringListModel):
def __init__(self, parent=None):
super(Model, self).__init__(parent)
data = QStringList()
data << "one" << "two" << "three" << "four" << "five"
self.setStringList(data)
class Form_2(QDialog):
def __init__(self, model, parent=None): #get the model from the calling form
super(Form_2, self).__init__(parent)
self.model = model
self.combo = QListView()
self.combo.setModel(self.model)
layout = QVBoxLayout()
layout.addWidget(self.combo)
self.setLayout(layout)
class Form_1(QDialog):
def __init__(self, parent=None):
super(Form_1, self).__init__(parent)
self.model = Model()
self.listView = QListView()
self.listView.setModel(self.model)
self.combo = QComboBox()
self.combo.setModel(self.model)
self.form2_button = QPushButton("Open Form_2")
layout = QVBoxLayout()
layout.addWidget(self.listView)
layout.addWidget(self.combo)
layout.addWidget(self.form2_button)
self.setLayout(layout)
self.connect(self.form2_button, SIGNAL("clicked()"), self.form_2)
def form_2(self):
self.ft = Form_2(self.model) #pass the same model to the new form
self.ft.show()
app = QApplication(sys.argv)
form = Form_1()
form.show()
app.exec_()

Related

Make QGroupBox selectable and clickable

I have the following toy interface:
from PyQt5 import QtWidgets, QtGui, QtCore
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
w = QtWidgets.QWidget()
layout = QtWidgets.QVBoxLayout()
w.setLayout(layout)
self.setCentralWidget(w)
my_tree = QtWidgets.QTreeWidget()
layout.addWidget(my_tree)
alpha = QtWidgets.QTreeWidgetItem(my_tree, ['Alpha'])
beta = QtWidgets.QTreeWidgetItem(my_tree, ['Beta'])
alpha.addChild(QtWidgets.QTreeWidgetItem(['one']))
alpha.addChild(QtWidgets.QTreeWidgetItem(['two']))
beta.addChild(QtWidgets.QTreeWidgetItem(['first']))
beta.addChild(QtWidgets.QTreeWidgetItem(['second']))
my_tree.expandAll()
alpha.child(0).setSelected(True)
scroll = QtWidgets.QScrollArea()
layout.addWidget(scroll)
scrollLayout = QtWidgets.QVBoxLayout()
scrollW = QtWidgets.QWidget()
scroll.setWidget(scrollW)
scrollW.setLayout(scrollLayout)
scrollLayout.setAlignment(QtCore.Qt.AlignTop)
scroll.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
scroll.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
scroll.setWidgetResizable(True)
for _ in range(5):
fooGroup = QtWidgets.QGroupBox()
fooLayout = QtWidgets.QVBoxLayout()
fooGroup.setLayout(fooLayout)
fooItem1 = QtWidgets.QLabel("fooItem1")
fooItem2 = QtWidgets.QLabel("fooItem2")
fooItem3 = QtWidgets.QLabel("fooItem3")
fooLayout.addWidget(fooItem1)
fooLayout.addWidget(fooItem2)
fooLayout.addWidget(fooItem3)
scrollLayout.addWidget(fooGroup)
self.show()
app = QtWidgets.QApplication([])
window = MainWindow()
app.exec_()
How can I make each group in the scroll area selectable and clickable by the user?
I have so far tried to add the following code in the loop:
def onFooGroupClick():
print("Group")
fooGroup.clicked.connect(onFooGroupClick)
and (as per this post):
def onFooGroupClick():
print("Group")
def f():
return onFooGroupClick()
fooGroup.mousePressEvent = f()
However, all my efforts have been unsuccessful and I cannot seem to be able to make it work.
Create a class that inherits from QGroupBox.
Define the clicked signal in it and override the mousePressEvent method.
from PyQt5 import QtWidgets, QtGui, QtCore
class GroupBox(QtWidgets.QGroupBox): # +++ !!!
clicked = QtCore.pyqtSignal(str, object) # +++
def __init__(self, title):
super(GroupBox, self).__init__()
self.title = title
self.setTitle(self.title)
def mousePressEvent(self, event):
child = self.childAt(event.pos())
if not child:
child = self
self.clicked.emit(self.title, child) # +++
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
w = QtWidgets.QWidget()
layout = QtWidgets.QVBoxLayout()
w.setLayout(layout)
self.setCentralWidget(w)
my_tree = QtWidgets.QTreeWidget()
layout.addWidget(my_tree)
alpha = QtWidgets.QTreeWidgetItem(my_tree, ['Alpha'])
beta = QtWidgets.QTreeWidgetItem(my_tree, ['Beta'])
alpha.addChild(QtWidgets.QTreeWidgetItem(['one']))
alpha.addChild(QtWidgets.QTreeWidgetItem(['two']))
beta.addChild(QtWidgets.QTreeWidgetItem(['first']))
beta.addChild(QtWidgets.QTreeWidgetItem(['second']))
my_tree.expandAll()
alpha.child(0).setSelected(True)
scroll = QtWidgets.QScrollArea()
layout.addWidget(scroll)
scrollLayout = QtWidgets.QVBoxLayout()
scrollW = QtWidgets.QWidget()
scroll.setWidget(scrollW)
scrollW.setLayout(scrollLayout)
scrollLayout.setAlignment(QtCore.Qt.AlignTop)
scroll.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
scroll.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
scroll.setWidgetResizable(True)
for _ in range(5):
fooGroup = GroupBox(f'GroupBox_{_}') # - QtWidgets.QGroupBox()
fooGroup.setObjectName(f'fooGroup {_}')
fooGroup.clicked.connect(self.onFooGroupClick) # +++
fooLayout = QtWidgets.QVBoxLayout()
fooGroup.setLayout(fooLayout)
fooItem1 = QtWidgets.QLabel("fooItem1", objectName="fooItem1")
fooItem1.setStyleSheet('background: #44ffff')
fooItem2 = QtWidgets.QLabel("fooItem2", objectName="fooItem2")
fooItem2.setStyleSheet('background: #ffff56;')
fooItem3 = QtWidgets.QLabel("fooItem3", objectName="fooItem3")
fooItem3.setStyleSheet('background: #ff42ff;')
fooLayout.addWidget(fooItem1)
fooLayout.addWidget(fooItem2)
fooLayout.addWidget(fooItem3)
scrollLayout.addWidget(fooGroup)
def onFooGroupClick(self, title, obj): # +++
print(f"Group: {title}; objectName=`{obj.objectName()}`")
if __name__ == '__main__':
app = QtWidgets.QApplication([])
window = MainWindow()
window.show()
app.exec_()

How can i add push button to each and every item of the list view

Here is my sample code,i want to add a push button to each and every row of the list view.I am not found any method to set the widget to model.Can any one please help me how to add widget for each and every row of the list view.Thank you in advance.
Given below is my code:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class MyCustomWidget(QWidget):
def __init__(self,parent=None):
super(MyCustomWidget, self).__init__(parent)
self.row = QHBoxLayout()
self.row.addWidget(QPushButton("add"))
self.setLayout(self.row)
class Dialog(QtGui.QDialog):
def __init__(self, parent=None):
super(Dialog, self).__init__(parent=parent)
vLayout = QtGui.QVBoxLayout(self)
hLayout = QtGui.QHBoxLayout()
self.lineEdit = QtGui.QLineEdit()
hLayout.addWidget(self.lineEdit)
self.filter = QtGui.QPushButton("filter", self)
hLayout.addWidget(self.filter)
self.list = QtGui.QListView(self)
vLayout.addLayout(hLayout)
vLayout.addWidget(self.list)
self.model = QtGui.QStandardItemModel(self.list)
codes = [
'windows',
'windows xp',
'windows7',
'hai',
'habit',
'hack',
'good'
]
self.list.setModel(self.model)
for code in codes:
item = QtGui.QStandardItem(code)
self.model.appendRow(item)
self.list.setIndexWidget(item.index(), QtGui.QPushButton("button"))
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
w = Dialog()
w.show()
sys.exit(app.exec_())
You have to create a custom widget where you must set the button on the right side with a layout.
import sys
from PyQt4 import QtCore, QtGui
class CustomWidget(QtGui.QWidget):
def __init__(self, parent=None):
super(CustomWidget, self).__init__(parent)
self.button = QtGui.QPushButton("button")
lay = QtGui.QHBoxLayout(self)
lay.addWidget(self.button, alignment=QtCore.Qt.AlignRight)
lay.setContentsMargins(0, 0, 0, 0)
class Dialog(QtGui.QDialog):
def __init__(self, parent=None):
super(Dialog, self).__init__(parent=parent)
vLayout = QtGui.QVBoxLayout(self)
hLayout = QtGui.QHBoxLayout()
self.lineEdit = QtGui.QLineEdit()
hLayout.addWidget(self.lineEdit)
self.filter = QtGui.QPushButton("filter", self)
hLayout.addWidget(self.filter)
self.list = QtGui.QListView(self)
vLayout.addLayout(hLayout)
vLayout.addWidget(self.list)
self.model = QtGui.QStandardItemModel(self.list)
codes = [
'windows',
'windows xp',
'windows7',
'hai',
'habit',
'hack',
'good'
]
self.list.setModel(self.model)
for code in codes:
item = QtGui.QStandardItem(code)
self.model.appendRow(item)
self.list.setIndexWidget(item.index(), CustomWidget())
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
w = Dialog()
w.show()
sys.exit(app.exec_())

When one checkbox is checked, by pressing a button to print some texts from a LineEdit if another checkbox is also checked

import sys, os
import PyQt4
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class Tab1Widget1(QWidget):
def __init__(self, parent=None):
super().__init__()
self.Tab1Widget1initUI()
self.bridge = Tab1Widget2()
def Tab1Widget1initUI(self):
self.setLayout(QGridLayout())
self.T1W1_checkbox = QCheckBox('checkbox1', self)
self.layout().addWidget(self.T1W1_checkbox, 1, 0)
def test(self):
print ('123')
def run(self):
if self.T1W1_checkbox.isChecked() == True:
self.test()
if self.bridge.T1W2_checkbox.isChecked() == True:
print (self.bridge.T1W2_le.text())
class Tab1Widget2(QWidget):
def __init__(self, parent=None):
super().__init__()
self.setLayout(QGridLayout())
self.T1W2_checkbox = QCheckBox('checkbox2', self)
self.layout().addWidget(self.T1W2_checkbox, 0, 0)
self.T1W2_le = QLineEdit()
self.layout().addWidget(self.T1W2_le, 0, 1)
class Tab1Layout(QWidget):
def __init__(self, parent=None):
super().__init__()
self.setLayout(QGridLayout())
self.group1 = Tab1Widget1(self)
scroll = QScrollArea(self)
scroll.setWidget(self.group1)
scroll.setWidgetResizable(True)
self.layout().addWidget(scroll, 0, 0)
self.group2 = Tab1Widget2(self)
self.layout().addWidget(self.group2, 1, 0)
self.btnRun = QPushButton('Run', self)
self.layout().addWidget(self.btnRun, 3, 0)
self.btnRun.clicked.connect(self.group1.run)
class Page1(QTabWidget):
def __init__(self, parent=None):
super().__init__()
self.tab1 = Tab1Layout()
self.addTab(self.tab1, "Tab1")
self.tab2 = QWidget()
self.tab3 = QWidget()
self.addTab(self.tab2, "Tab2")
self.addTab(self.tab3, "Tab3")
self.tab2_initUI()
self.tab3_initUI()
def tab2_initUI(self):
grid = QGridLayout()
self.tab2.setLayout(grid)
def tab3_initUI(self):
grid = QGridLayout()
self.tab3.setLayout(grid)
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super().__init__()
self.setGeometry(450, 250, 800, 550)
self.startPage1()
def startPage1(self):
x = Page1(self)
self.setWindowTitle("Auto Benchmark")
self.setCentralWidget(x)
self.show()
def main():
app = QApplication(sys.argv)
main = MainWindow()
main.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
If checkbox1 is checked and I press the run button, it will print 123. However, by pressing run button, I want checkbox2 to also print some texts entered in lineedit if the checkbox1 are also checked (i.e. it should print 123 first and then print 456).
I've looked up some similar types of questions, but none of that provides a proper answer. If anyone knows how to solve it, pls let me know thanks!!
The problem is that you are creating several Tab1Widget2, the first one you created in Tab1Layout, and that is the one you see, then you have created another one in Tab1Widget1, but not the time because you have not passed a parent, if you pass it to ** self ** as parent you will observe the following:
self.bridge = Tab1Widget2(self)
which is not what you want, so instead of creating a new you must pass the one that already exists, an option is to pass it through the constructor:
class Tab1Widget1(QWidget):
def __init__(self, bridge, parent=None): # Modify here
super().__init__(parent)
self.Tab1Widget1initUI()
self.bridge = bridge #Modify here
# ...
def test(self): print ('123')
def run(self):
if self.T1W1_checkbox.isChecked():
self.test()
if self.bridge.T1W2_checkbox.isChecked():
print (self.bridge.T1W2_le.text())
class Tab1Widget2(QWidget):
# ...
class Tab1Layout(QWidget):
def __init__(self, parent=None):
super().__init__()
self.setLayout(QGridLayout())
self.group2 = Tab1Widget2(self) # Modify here
self.group1 = Tab1Widget1(self.group2, self) # Modify here
scroll = QScrollArea(self)
scroll.setWidget(self.group1)
scroll.setWidgetResizable(True)
self.layout().addWidget(scroll, 0, 0)
# self.group2 = Tab1Widget2(self) # Modify here
self.layout().addWidget(self.group2, 1, 0)
# ...

PyQt5:How to communicate between two subwindows?

How to update the combobox in subwindow1 from subwindow2.The following code should be working fine but it doesnot update the combobox and doesnot show any errors at the line
AddressBook().updatestock(name)
in the add function.It looks like it creates an instance of the AddressBook.If so how can i rectify it.
import os
import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
class menudemo(QMainWindow):
def __init__(self, parent=None):
super(menudemo, self).__init__(parent)
self.pilot_widget=AddInventory()
self.drop_widget=AddressBook()
self.centralWidget=QMdiArea(self)
self.setCentralWidget(self.centralWidget)
self.sub1=QMdiSubWindow()
self.sub1.setWidget(self.drop_widget)
self.centralWidget.addSubWindow(self.sub1)
self.sub1.show()
self.sub2=QMdiSubWindow()
self.sub2.setWidget(self.pilot_widget)
self.centralWidget.addSubWindow(self.sub2)
self.sub2.show()
self.setWindowTitle("menu demo")
self.showMaximized()
class AddInventory(QWidget):
def __init__(self, parent=None):
super(AddInventory, self).__init__(parent)
self.statement=""
self.nameLabel = QLabel("Item:")
self.name=QLineEdit()
self.addButton = QPushButton("&Add")
self.addButton.setDefault(True)
self.addButton.clicked.connect(self.add)
mainLayout = QGridLayout()
mainLayout.addWidget(self.nameLabel, 0, 0)
mainLayout.addWidget(self.name,0,1)
mainLayout.addWidget(self.addButton, 0, 3)
self.setLayout(mainLayout)
self.setWindowTitle(" Address Book")
self.setFixedSize(self.sizeHint())
def add(self,text):
name=self.name.text()
AddressBook().updatestock(name)
self.name.clear()
class AddressBook(QWidget):
def __init__(self, parent=None):
super(AddressBook, self).__init__(parent)
nameLabel2 = QLabel("Item:")
self.itemstock=QComboBox(self)
self.itemstock.activated[str].connect(self.updatestock)
mainLayout = QGridLayout()
mainLayout.addWidget(nameLabel2, 0, 0)
mainLayout.addWidget(self.itemstock, 0, 1)
self.setLayout(mainLayout)
self.setWindowTitle("Simple Address Book")
self.setFixedSize(self.sizeHint())
def updatestock(self,name):
print(name)
self.itemstock.addItem(name)
def main():
app = QApplication(sys.argv)
ex = menudemo()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
It doesn't work, because you're just creating a new instance of AddressBook, you call a method on it.
It is not linked to self.drop_widget
You have to link both widgets using the constructor. You do this in the main
self.pilot_widget=AddInventory()
self.drop_widget=AddressBook()
I would do:
self.drop_widget=AddressBook()
self.pilot_widget=AddInventory(self.drop_widget)
and change AddInventory class parameters
class AddInventory(QWidget):
def __init__(self, drop_widget, parent=None):
self.drop_widget = drop_widget # now it is linked in your class as a member
now, in add:
def add(self,text):
name=self.name.text()
self.drop_widget.updatestock(name)
self.name.clear()

Recursively collect all checked QTreeview items in python

Could someone help me create a recursive function which loops through the treeview QStandardItemModel and collects all items which are 'checked true'
I'm not entirely clear on how to go about doing this myself.
from PySide import QtGui, QtCore
from PySide import QtSvg, QtXml
import sys
class Person:
def __init__(self, name="", children=None):
self.name = name
self.children = children if children else []
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.resize(300, 400)
self.init_ui()
def init_ui(self):
# Setup Tabs Widget
# self.treeview = QtGui.QTreeView()
self.treeview = QtGui.QTreeView()
self.treeview.setHeaderHidden(True)
self.treeview.setUniformRowHeights(True)
self.treeview.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
self.model = QtGui.QStandardItemModel()
self.treeview.setModel(self.model)
self.action = QtGui.QAction('Print', self)
self.action.setShortcut('F5')
self.action.triggered.connect(self.get_checked)
fileMenu = QtGui.QMenu("&File", self)
fileMenu.addAction(self.action)
self.menuBar().addMenu(fileMenu)
# Setup central widget
self.setCentralWidget(self.treeview)
# populate data
self.populate_people()
self.treeview.expandAll()
def populate_people(self):
parent = Person("Kevin", [
Person("Tom", [Person("Sally"), Person("Susan")]),
Person("Snappy", [Person("John"), Person("Kimmy"),
Person("Joe")]),
Person("Chester", [Person("Danny"), Person("Colleen")])
]
)
self.create_nodes(parent, self.model)
def create_nodes(self, node, parent):
tnode = QtGui.QStandardItem()
tnode.setCheckable(True)
tnode.setData(QtCore.Qt.Unchecked, role=QtCore.Qt.CheckStateRole)
tnode.setData(node.name , role=QtCore.Qt.DisplayRole)
tnode.setData(node, role=QtCore.Qt.UserRole) # store object on item
parent.appendRow(tnode)
for x in node.children:
self.create_nodes(x, tnode)
def get_checked(self):
print "collecting..."
def main():
app = QtGui.QApplication(sys.argv)
ex = MainWindow()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
It can be done with the model's match method:
def get_checked(self):
model = self.treeview.model()
checked = model.match(
model.index(0, 0), QtCore.Qt.CheckStateRole,
QtCore.Qt.Checked, -1,
QtCore.Qt.MatchExactly | QtCore.Qt.MatchRecursive)
for index in checked:
item = model.itemFromIndex(index)
print(item.text())

Categories