PDA

View Full Version : How do I remove the inverted background on selected item in QListView



cqubed
11th December 2012, 18:49
I have a dialog with a set of radio buttons and a QListView.

The ui behavior that I'm trying to get is:
1. If user selects/activates an item in the QListViewthe the radio buttons should be unchecked.
2. If user checks a radio button, the selection should be cleared in the QListView.

I have gotten the behaviour I want by sub-classing QListView and implementing void selectionChanged ( const QItemSelection & selected, const QItemSelection & deselecte). This allows me to uncheck the radio buttons when an item is selected. When a radio button is checked by the user, I use QListView::clearSelection() to clear the selection. It works great.

However there is a visual issue. When the user selects an item either with the mouse or using tab and keystrokes, the background of the row containing the item is inverted. QListView::clearSelection() removes the selection but leaves the background of the row inverted. This might be confusing to the user. How to a restore the background of the item row so that it is not inverted.

wysota
11th December 2012, 22:54
Can you provide a screenshot of what you mean?

cqubed
12th December 2012, 02:40
Here are some screen shots. In the process of taking the screen shots, I may have discovered the solution.
"initial dialog.png" This screen shot shows the dialog box after initial "show". Nothing is selected.
8496
item selected.png This screen shot shows the dialog with radio button unchecked and an item selected in QListView with its background inverted.
8497
If the user then clicks on radio button or tabs to radio button and checks it with a keystroke The selection is cleared, (I know this because if I fetch the selection list its empty), but the background on the QListView item stays inverted.

What I discovered in the process of trying to get a screen shot of this last state (radio button checked and non-selected item with inverted background) was that I couldn't get a screen shot
of it. It appears that when the screen capture tool takes focus it forces some or all of the dialog to repaint and the repaint removes the inverted background. I also discovered that if I cover the dialog
with another window when dialog is uncovered the inverted background is cleared. So I'm guessing that QListView::selectionClear() clears that selection, as I wanted, but doesn't remove the inverted background.Ssomehow I have t force a repaint. Its confusing because I can use the tab key to move around to different widgets on the dialog and the inverted background remains. I would have thought that tabbing or clicking between widgets after the state change with QListView:: selectionClear() would have triggered the required repaint.

So I'd appreciate any hints you can give me ZenMaster. In the mean time I'll trying "dirtying" parts of the dialog and calling repaint on the dialog and or QListView objects to see if that does the trick. Given all the optimizations in windowing systems I'm guessing that unless I can convince the Window system that the QListView is dirty, just calling repaint won't do anything.

Added after 22 minutes:

Problem solved. Trying to get the screen shot Master of Zen requested put me on the track.
The solution is:
1) Save the QModelIndex of the selected row (if there is one)
2) Call QListView::selectionClear() to clear the selection.
3) Call QListView::update(const QModelIndex) using saved row index.
The update immediately clears the inverted background.