You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

kuhtee

Beginner

  • "kuhtee" is male
  • "kuhtee" started this thread

Posts: 14

Location: Germany

Occupation: Engineer

  • Send private message

1

Friday, June 8th 2007, 10:11am

QListWidget deselection and multiple selection

Hi,

i'm just searching for more than a hour in the docu and can not find the solution for the following two problems with QListWidget:

a) i would like to deselect (deactivate) items in the QListWidget from the software

b) i use multiple selection: setSelectionMode(QAbstractItemView::MultiSelection)
I would like to pre-select more than one item from the software. If i use
setCurrentRow() i can only select on item (it toggles the selected item). How can i do multiple selections?

Your help is very welcome, i have already been gone throw the complete Members List of QListWidget but without finding a suitable method....

lifthrasir

Trainee

  • "lifthrasir" is male

Posts: 159

Location: France

Occupation: Student

  • Send private message

2

Friday, June 8th 2007, 11:06am

you must use QItemSelectionModel

Source code

1
2
3
4
5
6
7
8
9
QItemSelectionModel * selectionModel = myListWidget->selectionModel();

// clear the selection
selectionModel->clearSelection();    // or QItemSelectionModel::clear()

// make a multiple selection (i'm not sure, maybe you need to use QItemSelection)
selectionModel->select(index1, QItemSelectionModel::SelectCurrent);
selectionModel->select(index2, QItemSelectionModel::SelectCurrent);
selectionModel->select(index3, QItemSelectionModel::SelectCurrent);

This post has been edited 1 times, last edit by "lifthrasir" (Jun 8th 2007, 11:07am)


kuhtee

Beginner

  • "kuhtee" is male
  • "kuhtee" started this thread

Posts: 14

Location: Germany

Occupation: Engineer

  • Send private message

3

Friday, June 8th 2007, 12:06pm

lifthrasir, many thanksfor the hint,

i tried your proposal, using the following code:

Source code

1
2
3
4
5
  QItemSelectionModel *selectionModel=listWidgetRunName->selectionModel();
  int row=get_the_row(i);
  QListWidgetItem *myItem=listWidgetRunName->item(row);
  QModelIndex myIndex=listWidgetRunName->indexFromItem(myItem);
  selectionModel->select(myIndex, QItemSelectionModel::Select);

Unfortunately the indexFromItem function is protected -> i can not use it. But i need to access the entries by the row index, because the row index is the data i am interested in.

The problem is now reduced to: How to get the QModelIndex for a given row in the QListWidget :-(

kuhtee

Beginner

  • "kuhtee" is male
  • "kuhtee" started this thread

Posts: 14

Location: Germany

Occupation: Engineer

  • Send private message

4

Thursday, July 12th 2007, 5:44pm

If somebody is interested in a solution, i did the following:

I inherited a class from QListWidget that only made the protected function visible to the public.

Source code

1
2
3
4
5
6
7
8
class ListWidgetMulti : public QListWidget
{
  Q_OBJECT

  public:
    ListWidgetMulti(QListWidget *parent = 0);
    QModelIndex indexFromItem ( QListWidgetItem * item ) const;
};

Source code

1
2
3
4
5
6
7
8
9
ListWidgetMulti::ListWidgetMulti(QListWidget *parent)
   : QListWidget(parent)
{
}

QModelIndex ListWidgetMulti::indexFromItem ( QListWidgetItem * item ) const
{
  return QListWidget::indexFromItem(item);
}

Use this class instead of the standard QListWidget in the code above and it is possible to select multiple lines in a QListWidget from the code.

5

Monday, March 10th 2008, 8:58am

lifthrasir's solution works fine, but is there a way to stop the signal selectionChanged() being emitted on every call to selectionModel->select(..).?

This really kills the application when you have a large number of items in the list that you wish to select.