Emit Signal When CheckState is Changed in the Model
We add checkboxes for a spesific column in a QTableView via changing the setData() and data() methods of the model with CheckStateRole. We did not create QCheckBox. Checkboxes are created automatically with the help of the Qt::CheckStateRole and Qt::ItemIsUserCheckable flag. (This is the way wanted from us.)
When a checkbox is checked/unchecked by mouseclick we want to emit a signal. However, we couldnt do that. We cannot emit our own signal after dataChanged() signal, which can lead to emit some singals more than once.
Is there any way to do it?
Im not sure why a new signal would affect your current handling.
If you in setData emit a new signal , could you not just do what needs to be done in that slot
without triggering other signals ?
You could also do
view->blockSignals(true); ...what..ever view->blockSignals(false);
in that slot to prevent triggering stuff.
if you try this way, do note
Which is safer than calling blockSignals
DzCode last edited by DzCode
@mrjj I will emit the same signal.
For example for clicking "space" , I am emitting a signal that emit only once for multiple checking of all chekboxes. It emits after space key pressed. I also want to emit the same signal for only clicking checkbox. So that your solution is not suitable for me
You can call a slot without a signal, it just a normal member function.
So you could block others and directly call the slot.
If that would help ?
@mrjj the main problem is how I can detect that the checkbox is clicked by mouse or not? How can I get it
I assume its not a std item model so we cant use
so all we have is setData.
My friend @Ratzz suggested to use a delegate.
You could the overwrite setData, check if item check/unchecked.
and emit signal there. it would then come before the model handle it.
@mrjj custom model implemented from QAbstractItemModel is used in the project.
Is there any way to do it with mouseevents?
Well you test if you click in a cell but the "checkbox" is just drawn so it cant emit any mouse events by itself.
Overwrite view's mousePress . remember to call base class after
The read the model if its currently checked or unchecked and then assume the opposite
when model updates. Then it might be possible to send signal before model is actually updated.
@mrjj Actually, I tried it with mousepress and mouserelease and couldnt do it. In press event I hold the previous data and in release I look at the current data. I checked for the changes but not worked
I would assume that on mouseRelease the data would still be the same.
But since its a bool checked/not checked could you not emit signal anyway
with just the opposite of what it is ? so if unchecked send checked and reverse.
It will first be changed after but you can get a signal before dataChanged is called ?
Now, I am checking the position of mouse and controlling the table item in mouse press and mouse release. Via holding a state as a private member variable, I did it.
Pablo J. Rogina last edited by
I did it.
does it mean your issue is solved? if so please don't forget to mark your post as such!