Model-view checkstate signal
-
Hello,
@JonB and @Pl45m4 thank you for the brief info.
correct me if i am wrong, just rephrasing above for clarity@JonB said in Model-view checkstate signal:
I am not sure I understand what you mean by this.
what i am looking for is, how we connect
QCheckBox.clicked.connect(<function>), same logic or for something similar in MVC, whichever index i click should do something(that something would be in a function)This would be a bad practice if i try signaling a function.
@Pl45m4 said in Model-view checkstate signal:I think OP is looking for some kind of 1to1 connection in the model/view, between the checkable item (which is not directly a QCheckBox) and some receiver of the signal... which kinda destroys or bypasses a clean MV(C) structure...
If I need to try such a thing i can do it with
dataChanged.emitin thesetData, but again this would be a bad practice, correct? -
Hello,
@JonB and @Pl45m4 thank you for the brief info.
correct me if i am wrong, just rephrasing above for clarity@JonB said in Model-view checkstate signal:
I am not sure I understand what you mean by this.
what i am looking for is, how we connect
QCheckBox.clicked.connect(<function>), same logic or for something similar in MVC, whichever index i click should do something(that something would be in a function)This would be a bad practice if i try signaling a function.
@Pl45m4 said in Model-view checkstate signal:I think OP is looking for some kind of 1to1 connection in the model/view, between the checkable item (which is not directly a QCheckBox) and some receiver of the signal... which kinda destroys or bypasses a clean MV(C) structure...
If I need to try such a thing i can do it with
dataChanged.emitin thesetData, but again this would be a bad practice, correct?That's why I wrote:
@Pl45m4 said in Model-view checkstate signal:
You can't connect directly to that checkbox.
Either you use the dataChanged(...) signal when the data is, well, changed and compare the QModelIndex (row, col) or you add your own logic when you set your data.So, use the
dataChangedsignal, which is already there and sort out the correct model index + value. -
noted.
if possible can you provide a code snippet, just for understanding connections?
It would be helpful
Thank You.@blossomsg said in Model-view checkstate signal:
if possible can you provide a code snippet, just for understanding connections?
Code for what? Who made your first (the
QCheckBox) example? There you already have similar code.
Now you just need to use your model'sdataChanged(...)signal and figure out what index your checkbox has, depending on its row and column.
Or you implement your ownsetDatalogic and emit your custom signal there -
Hi,
A model can have multiple views and a view does not necessarily mean a QWidget.
As my fellow already wrote: use your model.
Rather than creating it as part of the setModel call, create it before and keep a reference to it. Then connect its dataChanged signal to the slot you want to use it with. The only thing you have to do is extract the information you want to act accordingly. -
Hello All,
I have tried a version as suggested to connect directly with dataChage in the model
getting the expected result, can you review and let me know, if there is a better way to do it.
I have added # NEW to those lines and functionsfrom PySide6 import QtCore, QtGui class LModel1(QtCore.QAbstractListModel): def __init__(self, data: list[str], parent=None): super().__init__(parent) self._data = data # from ai claude and fluent python - dict Comprehensions - pg: 79 self._check_states = {item: QtCore.Qt.CheckState.Unchecked for item in self._data} def data(self, index, /, role=...): if not index.isValid(): return None item = self._data[index.row()] if role == QtCore.Qt.ItemDataRole.DisplayRole: return item if role == QtCore.Qt.ItemDataRole.CheckStateRole: return self._check_states[item] if role == QtCore.Qt.ItemDataRole.DecorationRole: pixmap = QtGui.QPixmap(10, 10) pixmap.fill(QtCore.Qt.transparent) painter = QtGui.QPainter(pixmap) painter.setRenderHint(QtGui.QPainter.Antialiasing) painter.setBrush(QtGui.QColor("red")) painter.drawRect(pixmap.rect()) painter.end() return QtGui.QIcon(pixmap) if role == QtCore.Qt.ItemDataRole.ToolTipRole: return item if role == QtCore.Qt.ItemDataRole.FontRole: font = QtGui.QFont("Cursive", 12) font.bold() return font return None def setData(self, index, value, /, role=...): if not index.isValid(): return None item = self._data[index.row()] if role == QtCore.Qt.ItemDataRole.CheckStateRole: self._check_states[item] = value self.dataChanged.connect(self.print_something) # NEW self.dataChanged.emit(index, index, [role]) return True return None def flags(self, index, /): if not index.isValid(): return None return QtCore.Qt.ItemFlag.ItemIsUserCheckable | QtCore.Qt.ItemFlag.ItemIsEnabled | QtCore.Qt.ItemFlag.ItemIsSelectable def rowCount(self, /, parent=...): return len(self._data) def print_something(self, index): # NEW item = self._data[index.row()] if self._check_states[item] == QtCore.Qt.CheckState.Checked.value: print("something") return "something" elif self._check_states[item] == QtCore.Qt.CheckState.Unchecked.value: print("something else") return "something else" return None
-
Hello All,
I have tried a version as suggested to connect directly with dataChage in the model
getting the expected result, can you review and let me know, if there is a better way to do it.
I have added # NEW to those lines and functionsfrom PySide6 import QtCore, QtGui class LModel1(QtCore.QAbstractListModel): def __init__(self, data: list[str], parent=None): super().__init__(parent) self._data = data # from ai claude and fluent python - dict Comprehensions - pg: 79 self._check_states = {item: QtCore.Qt.CheckState.Unchecked for item in self._data} def data(self, index, /, role=...): if not index.isValid(): return None item = self._data[index.row()] if role == QtCore.Qt.ItemDataRole.DisplayRole: return item if role == QtCore.Qt.ItemDataRole.CheckStateRole: return self._check_states[item] if role == QtCore.Qt.ItemDataRole.DecorationRole: pixmap = QtGui.QPixmap(10, 10) pixmap.fill(QtCore.Qt.transparent) painter = QtGui.QPainter(pixmap) painter.setRenderHint(QtGui.QPainter.Antialiasing) painter.setBrush(QtGui.QColor("red")) painter.drawRect(pixmap.rect()) painter.end() return QtGui.QIcon(pixmap) if role == QtCore.Qt.ItemDataRole.ToolTipRole: return item if role == QtCore.Qt.ItemDataRole.FontRole: font = QtGui.QFont("Cursive", 12) font.bold() return font return None def setData(self, index, value, /, role=...): if not index.isValid(): return None item = self._data[index.row()] if role == QtCore.Qt.ItemDataRole.CheckStateRole: self._check_states[item] = value self.dataChanged.connect(self.print_something) # NEW self.dataChanged.emit(index, index, [role]) return True return None def flags(self, index, /): if not index.isValid(): return None return QtCore.Qt.ItemFlag.ItemIsUserCheckable | QtCore.Qt.ItemFlag.ItemIsEnabled | QtCore.Qt.ItemFlag.ItemIsSelectable def rowCount(self, /, parent=...): return len(self._data) def print_something(self, index): # NEW item = self._data[index.row()] if self._check_states[item] == QtCore.Qt.CheckState.Checked.value: print("something") return "something" elif self._check_states[item] == QtCore.Qt.CheckState.Unchecked.value: print("something else") return "something else" return None
@blossomsg A slot usually does not return anything
-
Noted. This is just for testing, if it works according to check/uncheck values(0 and 2).
But the end goal is to add a function which will be loading/unloading obj in autodesk maya.
But is this in the right direction?@blossomsg There's no reason for that slot to be in the model.
Depending on how you want to integrate that with Maya, create your own custom view on top of the model that will translate that information in whatever is needed for Maya.
A "view" does not mandatory mean a tree or a table, it's really whatever uses the model as a source of information. -
B blossomsg has marked this topic as solved