QListWidget selection, preventing selection and revert to previous?
-
wrote on 20 Jan 2022, 10:34 last edited by SPlatten
When the currentItemChanged signal occurs, in the slot I look for changes in other related controls and as a result I get either true to allow the change or false to prevent the change.
The question is how do I instruct QListWidget to revert back to the previous selection?
In the slot I have the parameters:
QListWidgetItem* pobjCurrent, QListWidgetItem* pobjPrevious
Based on the result I've tried:
if ( blnOkToChange != true ) { setCurrentItem(pobjPrevious); return; }
This doesn't work.
Edit, I've also tried:
if ( blnOkToChange != true ) { pobjCurrent->setSelected(false); pobjPrevious->setSelected(true); return; }
This doesn't work at all.
-
@SPlatten
I find that hard to believe, don't you? You are just calling selection on a timer, why should that not work? Do some debugging of your code's behaviour. For all I know, to set aQListWidget
's selection you must do something on theQListWidget
, not on the items? Have you even tested yoursetSelected()
call in any circumstance?wrote on 20 Jan 2022, 13:42 last edited by SPlatten@JonB , so far all efforts at trying to prevent the selection have failed.
if ( blnOkToChange != true ) { //Select previous selection and exit QListWidgetItem* pobjItem(pobjPrevious); QTimer::singleShot(100, this, [pobjItem]() { pobjItem->setSelected(true); }); return; }
Thinking about this issue, I think I will change the approach and when any of the controls are edited I will disable the list widget until the any changes have been submitted.
-
When the currentItemChanged signal occurs, in the slot I look for changes in other related controls and as a result I get either true to allow the change or false to prevent the change.
The question is how do I instruct QListWidget to revert back to the previous selection?
In the slot I have the parameters:
QListWidgetItem* pobjCurrent, QListWidgetItem* pobjPrevious
Based on the result I've tried:
if ( blnOkToChange != true ) { setCurrentItem(pobjPrevious); return; }
This doesn't work.
Edit, I've also tried:
if ( blnOkToChange != true ) { pobjCurrent->setSelected(false); pobjPrevious->setSelected(true); return; }
This doesn't work at all.
wrote on 20 Jan 2022, 12:44 last edited by JonB@SPlatten
currentItemChanged
is only a signal to say it has happened, after the change has been made. It does not offer "reject".I would have thought your code would work, so long as you are correctly checking for recursive call, because I think you will get the signal again for the change back. You may have to set both current item & selected item.
If it does not work, presumably either subclass
QListWidget
to handle, or install event filter on the it and handle check there, or try your revert-select on a timer immediately afterward to allow the current signal-slot to exit as-is. Not very elegant, but might suffice. -
@SPlatten
currentItemChanged
is only a signal to say it has happened, after the change has been made. It does not offer "reject".I would have thought your code would work, so long as you are correctly checking for recursive call, because I think you will get the signal again for the change back. You may have to set both current item & selected item.
If it does not work, presumably either subclass
QListWidget
to handle, or install event filter on the it and handle check there, or try your revert-select on a timer immediately afterward to allow the current signal-slot to exit as-is. Not very elegant, but might suffice.wrote on 20 Jan 2022, 13:21 last edited by@JonB, thank you, I added this to the top of the slot of handle recursion:
if ( pobjCurrent == pobjPrevious ) { //Ignore! return; }
It still doesn't work. I get a prompt, if I answer no I can see it drops into the logic:
if ( blnOkToChange != true ) { //Select previous selection and exit setCurrentItem(pobjPrevious); return; }
But the display still shows the new selection.
-
setCurrentItem() does not set a selection but the current index. current index has nothing to do with the current selection.
-
setCurrentItem() does not set a selection but the current index. current index has nothing to do with the current selection.
wrote on 20 Jan 2022, 13:26 last edited by@Christian-Ehrlicher , thank you, then how to ?
-
@Christian-Ehrlicher , thank you, then how to ?
-
@SPlatten
Like I said earlier, you probably want to set both current item and selection for your purpose? You have to decide.wrote on 20 Jan 2022, 13:31 last edited by@JonB , I've just tried:
if ( blnOkToChange != true ) { //Select previous selection and exit QTimer::singleShot(50, [pobjCurrent, pobjPrevious]() { pobjCurrent->setSelected(false); pobjPrevious->setSelected(true); }); return; }
Still not right, the list selection doesn't revert from the new selection.
-
@JonB , I've just tried:
if ( blnOkToChange != true ) { //Select previous selection and exit QTimer::singleShot(50, [pobjCurrent, pobjPrevious]() { pobjCurrent->setSelected(false); pobjPrevious->setSelected(true); }); return; }
Still not right, the list selection doesn't revert from the new selection.
wrote on 20 Jan 2022, 13:34 last edited by@SPlatten
I find that hard to believe, don't you? You are just calling selection on a timer, why should that not work? Do some debugging of your code's behaviour. For all I know, to set aQListWidget
's selection you must do something on theQListWidget
, not on the items? Have you even tested yoursetSelected()
call in any circumstance? -
@SPlatten
I find that hard to believe, don't you? You are just calling selection on a timer, why should that not work? Do some debugging of your code's behaviour. For all I know, to set aQListWidget
's selection you must do something on theQListWidget
, not on the items? Have you even tested yoursetSelected()
call in any circumstance?wrote on 20 Jan 2022, 13:42 last edited by SPlatten@JonB , so far all efforts at trying to prevent the selection have failed.
if ( blnOkToChange != true ) { //Select previous selection and exit QListWidgetItem* pobjItem(pobjPrevious); QTimer::singleShot(100, this, [pobjItem]() { pobjItem->setSelected(true); }); return; }
Thinking about this issue, I think I will change the approach and when any of the controls are edited I will disable the list widget until the any changes have been submitted.
-
@JonB , so far all efforts at trying to prevent the selection have failed.
if ( blnOkToChange != true ) { //Select previous selection and exit QListWidgetItem* pobjItem(pobjPrevious); QTimer::singleShot(100, this, [pobjItem]() { pobjItem->setSelected(true); }); return; }
Thinking about this issue, I think I will change the approach and when any of the controls are edited I will disable the list widget until the any changes have been submitted.
wrote on 21 Jan 2022, 07:57 last edited by@SPlatten said in QListWidget selection, preventing selection and revert to previous?:
Thinking about this issue, I think I will change the approach and when any of the controls are edited I will disable the list widget until the any changes have been submitted.
You don't have to fully disable the list widget (which is when it is grayed out), but just
blockSignals
for the list widget.If I'm not mistaken I always go through the model to tweak the selection. This is the only way I have found so far to work properly.
1/10