Qt-interest Archive, July 2007
Change item selection with QListView
Message 1 in thread
Hi,
One of my windows have a QListView which display different Higlighting
rules. To contol this QListView, I have added buttons to add new rules, to
delete one, or move up/down.
My problem, is that when I press the "move up" or "move down" button, the
selected item is correctly moved but the selection is not kept. So, if you
want to move an item from the top of the QListView to the bottom, you need
to reselect the item each time you press the "move down" button.
I read a lot and try many different way to move the selection and make it
follow the item. But it still not work !!!!
The following function show you how i do to move an item down... A similar
function exist to move items up:
void HighlightingRulesDialog::moveDownHighlightingRule()
{
QModelIndex currentListViewIndex = listView->currentIndex();
if(currentListViewIndex.isValid() &&
currentListViewIndex.row()<_ListViewModel->rowCount(currentListViewIndex)-1)
{
_ListViewModel->moveHighlightingRule(currentListViewIndex.row(),currentListViewIndex.row()+1);
listView->setCurrentIndex(currentListViewIndex.child(currentListViewIndex.row()+1,0));
}
}
Can anyone tell me how to keep the selection while moving my items position
in the QListView ???
Thanks a lot.
Geofrey
--
[ signature omitted ]
Message 2 in thread
Locking into the source file of the QListWidget (Qt4 opensource version), I
found the solution.
void HighlightingRulesDialog::setCurrentRowInListView(int row)
{
QModelIndex index = listView->model()->index(row,0);
listView->selectionModel()->setCurrentIndex(index,
QItemSelectionModel::SelectCurrent);
}
"Geofrey van Hecke" <gvanhecke@xxxxxxxxxx> wrote in message
news:f7hs1t$d5l$1@xxxxxxxxxxxxxxxxxxxxx
> Hi,
>
> One of my windows have a QListView which display different Higlighting
> rules. To contol this QListView, I have added buttons to add new rules, to
> delete one, or move up/down.
>
> My problem, is that when I press the "move up" or "move down" button, the
> selected item is correctly moved but the selection is not kept. So, if you
> want to move an item from the top of the QListView to the bottom, you need
> to reselect the item each time you press the "move down" button.
>
> I read a lot and try many different way to move the selection and make it
> follow the item. But it still not work !!!!
>
>
> The following function show you how i do to move an item down... A similar
> function exist to move items up:
>
> void HighlightingRulesDialog::moveDownHighlightingRule()
> {
>
> QModelIndex currentListViewIndex = listView->currentIndex();
>
> if(currentListViewIndex.isValid() &&
> currentListViewIndex.row()<_ListViewModel->rowCount(currentListViewIndex)-1)
>
> {
>
>
> _ListViewModel->moveHighlightingRule(currentListViewIndex.row(),currentListViewIndex.row()+1);
>
>
> listView->setCurrentIndex(currentListViewIndex.child(currentListViewIndex.row()+1,0));
>
> }
>
> }
>
>
>
> Can anyone tell me how to keep the selection while moving my items
> position
> in the QListView ???
>
>
>
> Thanks a lot.
>
>
>
> Geofrey
>
>
>
>
>
>
--
[ signature omitted ]