PDA

View Full Version : QTableView Item Selection



johnmauer
22nd January 2010, 23:39
When building a QTableView, I have noticed that the selection of a row or column in the ExtendedSelection mode remains in effect even when an element in that row or column is clicked. The current item changes; the selection does not. I've tried to use the clicked() SIGNAL to implement a change but it does not fire into a test SLOT. Is there any workaround for this situation? (I have a custom model, but even the demo examples exhibit this behavior.)

wysota
24th January 2010, 01:24
You can change selections through QItemSelectionModel instance that is linked to your view. Note though that the behaviour you are observing is the intended one for the selection mode you have chosen.

johnmauer
24th January 2010, 13:47
Thanks. I did exactly as you suggested but had to use the low level mouse signals; the table clicked() SIGNAL doesn't seem to send. The selection behavior works fine in general, but leaves a lot to be desired for a single column table. The user can select the column, but never unselect it. (I know, use a list, but I really like the vertical header for a column of 10000 numbers)

wysota
24th January 2010, 14:25
I have no problems with unselecting selected rows for a single column table view with selection mode set to Extended. Maybe there is something wrong with your code?

johnmauer
24th January 2010, 15:48
What I could not do was unselect the single column once selected by clicking on an item as the user would likely do. I am using Extended mode specifically, not by default. I can select different rows, but not unselect the row when clicking on the same row item either. (In either case, a drag will deselect using the rubber band.)

However, I did as you suggested and modified the behavior with the low level mouse SLOT and the QItemSelectionModel; it works fine now.

wysota
24th January 2010, 16:03
Could you open Qt Designer, place a QTableWidget inside a form, add a column and few rows to it, set the appropriate selection mode, preview the form and see if it works then?

johnmauer
25th January 2010, 01:53
Done. One QTableWidget in a dialog with one column and 7 rows, enter data in each row, select column, then clicking an item does not deselect the column. I used QtDesigner in Visual Studio with all defaults untouched, the columns and rows built in QtDesigner and viewed in Preview mode. Same behavior in compile and execution.

Sorry for the delay: family day.

wysota
25th January 2010, 10:51
How many items did you select before trying to change selection by clicking on the item? Extended selection works in such a manner that it deselects all rows selected and selects the ones you click or drag. So if you have one row selected and you click on the same row again it won't be deselected. You need to Ctrl+click on it. If you want a different behaviour then use MultiSelection instead of ExtendedSelection.

johnmauer
25th January 2010, 14:20
When the user selects an item in the usual way, the selection is cleared and the new item selected.

The documentation suggests clearing the selection if Ctrl or Shift keys are not pressed. But the behavior appears to be the lack of item selection when the table has a single column. At the beginning, selecting an item also selects a row. If the column is then selected, selecting an item is always in the same column and the column does not deselect. In any event, the correct behavior is restored through the mousePressed SLOT and clearing the selection.

faldzip
25th January 2010, 14:59
The documentation suggests clearing the selection if Ctrl or Shift keys are not pressed. But the behavior appears to be the lack of item selection when the table has a single column. At the beginning, selecting an item also selects a row. If the column is then selected, selecting an item is always in the same column and the column does not deselect. In any event, the correct behavior is restored through the mousePressed SLOT and clearing the selection.
I don't understand what number of columns has to do with selection in this case... In documentation sentence quoted by you it is clearly said that when you click on item then selection is cleared and the new item selected. So if you click on the selected item then even if the item was selected it would be reselected - so there is no chance to deselect selected item by just clicking on it.

johnmauer
25th January 2010, 16:24
In short, using the Extended mode, if a column has been selected by clicking the header, then clicking an item in that column does not deselect the column before it selects the item. The only ways to deselect the column are to click the row header which selects a row or drag a selection. I wasn't talking about selecting the same item, but selecting the column (which sets row 0 as the current item), then clicking another item. That should deselect the column and select the item. Currently, it leaves the column selected, and moves only the current item.

wysota
25th January 2010, 17:07
That's not how it works on my computer... I click the header and the whole column gets selected. I click on one of the items in the column and the column gets deselected and the item gets selected. If it doesn't work for you this way then maybe that's a platform dependent behaviour and it is intended to work this way on Windows.

johnmauer
25th January 2010, 18:39
Maybe. In any case the following code seems to solve the behavior and make what you must see on another platform.


void DataTable::mousePressEvent(QMouseEvent *event)
{

if ((event->button() & Qt::LeftButton) &&
!(event->modifiers() & Qt::ShiftModifier) &&
!(event->modifiers() & Qt::ControlModifier))
{
selectionModel()->clearSelection();
}
QAbstractItemView::mousePressEvent(event);
}

faldzip
25th January 2010, 20:22
hmm I have Windows 7 and I checked that when I select the column by clicking header and then click one item then only this one item is selected. What Windows and Qt version do you have?

johnmauer
25th January 2010, 21:48
I'm running Windows XP Professional SP3, Visual Studio 2008, with Qt 4.6.0 (commercial license) and the VS addin. I'll try to bring up a Windows 7 system with Qt if the licenses will let me.

Edit Note: I loaded Qt 4.6.1 and the behavior is changed, but on mouseReleased. I went back and verified with 4.6.0. Makes sense now why you didn't see it.

wysota
25th January 2010, 22:23
I was using 4.5.3.

johnmauer
25th January 2010, 23:50
Weird. I have attached a jpg of the test table in 4.6.0 where the column header was checked, then item 3. As you can see, item 3 is the current item , but the column is still selected. As I noted, in 4.6.1, this was fixed (again?).

Thanks a lot for your help, both of you. I can see why you were skeptical.:confused: