PDA

View Full Version : Qt::ItemIsSelectable in a model/view structure



schall_l
30th January 2008, 16:50
I am trying to do an iTunes like sidebar with different sections.

In the flags function from my QAbstractItemModel derived class I make sure not to return Qt::ItemIsSelectable for the items I like to use as titel for a section.
At the execution, the different titels are not selectable if I try to click them which is good, but going up and down with the keyboard will make me go through these titels (they do not appear as selected, but I loose the selection on the previously selected item)

What I would like to have is like in iTunes; if I have the selection on "iTunes Store" and pres the up key I will arrive immediatelly to the "Ringtone" element without stoping at the "Store" titel.

If someone has an idea...

jpn
30th January 2008, 22:18
I guess you could reimplement virtual method QAbstractItemView::moveCursor() to skip over them.

schall_l
31st January 2008, 15:16
Thank you very much, you're a genius. :)

schall_l
2nd February 2008, 20:15
I would like to come back to my initial post:

If I do have the selection on an item and then click on an item that can not be selected (means when the function flags from my QAbstractItemModel derived class does not return Qt::ItemIsSelectable) I do observe the following: the newly clicked item will not receive the selection which is what I want, but the previously selected one will loose it selection which is not what I want.

What can I do to avoid the latest selected item to loose it selection when I do click (or double click) on an item that is not selectable ?

jpn
3rd February 2008, 09:35
I'd try with reimplementing QItemSelectionModel::select().

schall_l
12th February 2008, 11:42
Thank you,

I've tested this by implementing a selection model class deriving from QItemSelectionModel and saw the following behavior

-1- Clicking on a Qt::ItemIsSelectable item /b/ when beeing on a Qt::ItemIsSelectable item /a/ will call:
select(const QItemSelection ... with command = 35 with selection containing the destinated selected item /b/
select(const QModelIndex ... for the destinated selected item /b/ with command = 0
select(const QItemSelection ... with command = 0 with selection containing the destinated selected item /b/


-2- Clicking on a non Qt::ItemIsSelectable /b/ item when beeing on a Qt::ItemIsSelectable item /a/ will call:
select(const QItemSelection ... with command = 35 with selection containing no item


-3- Clicking on a Qt::ItemIsSelectable item /b/ when beeing on a non Qt::ItemIsSelectable /a/ item will call:
select(const QItemSelection ... with command = 35 with selection containing the destinated selected item /b/
select(const QModelIndex ... for the destinated selected item /b/ with command = 0
select(const QItemSelection ... with command = 0 with selection containing the destinated selected item /b/


-4- Clicking a non Qt::ItemIsSelectable item when beeing on a non Qt::ItemIsSelectable item will call:
select(const QItemSelection ... is called with command = 35 with selection containing no item


So it looked like the calls of the select functions are already the result of some kind of treatment resulting in the new selection.

Finally I went to reimplement mousePressEvent and mouseReleaseEvent in order to catch the mouse events on a QModelIndex I do not want to be selectable:



void
SideBarTree::mousePressEvent(QMouseEvent *event)
{
QPoint pos = event->pos();
QModelIndex index = indexAt(pos);
if (index.row() == SideBar::Spacer1) return;
if (index.row() == SideBar::Spacer2) return;

QTreeView::mousePressEvent(event);
}

void
SideBarTree::mouseReleaseEvent(QMouseEvent *event)
{
QPoint pos = event->pos();
QModelIndex index = indexAt(pos);
if (index.row() == SideBar::Spacer1) return;
if (index.row() == SideBar::Spacer2) return;

QTreeView::mouseReleaseEvent(event);
}

This seems to work.