You are not logged in.

Dear visitor, welcome to 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.


Sunday, April 1st 2012, 4:52pm

QLIstWidget Select Items

So I have a QListWidget on my forum and when it resizes I repopulate the widget with its items, if I don't then all the widgets stay where they were and don't move to fit the widget. But when I resize the widget with multiple items selected I lose the selection. I'm trying to maintain that selection. So after the widget is repopulated I have to manually select everything that was selected before.

Source code

void Explore::resizeEvent(QResizeEvent *e){
    QList<QListWidgetItem*> CurSelection = this->selectedItems();
//    QModelIndexList CurTextSelection = this->selectedIndexes();
//    qDebug() << CurTextSelection.size();
//    QModelIndex CurInd;
//    QModelIndexList Lst;
//    foreach(CurInd, CurTextSelection){
//        Lst.append(CurInd);
//    }
    for(int cnt = 0; cnt < CurSelection.size(); cnt++){
        this->setItemSelected(CurSelection[cnt], true);
//    QItemSelectionModel *NewSelection = this->selectionModel();
//    qDebug() << CurTextSelection.size();
//    QListWidgetItem *CurInd;
//    NewSelection->select(CurTextSelection, QItemSelectionModel::Current);
//    foreach(CurInd, CurTextSelection){
//        this->selectionModel()->select(CurInd, QItemSelectionModel::SelectCurrent);
//    }
//    qDebug() << CurTextSelection.size();

As you can see I'm trying to use the setItemSelected function but when this code executes I get a
segmentation fault. This is the call stack...

0 QAbstractItemView::model() const /usr/lib/ 0 0x503e4a7a
1 ?? /usr/lib/ 0 0x5045c08a
2 QListWidget::setItemSelected(QListWidgetItem const*, bool) /usr/lib/ 0 0x5045c37f
3 Explore::resizeEvent(QResizeEvent*) 0 0x804f16f
4 QWidget::event(QEvent*) /usr/lib/ 0 0x4fe93f3f
5 QFrame::event(QEvent*) /usr/lib/ 0 0x502aee76
6 QAbstractScrollArea::viewportEvent(QEvent*) /usr/lib/ 0 0x50342da2
7 QAbstractItemView::viewportEvent(QEvent*) /usr/lib/ 0 0x503e7f9c
8 ?? /usr/lib/ 0 0x503455a6
9 QCoreApplicationPrivate::sendThroughObjectEventFilters(QObject*, QEvent*) /usr/lib/ 0 0x4fb7e7a6
10 QApplicationPrivate::notify_helper(QObject*, QEvent*) /usr/lib/ 0 0x4fe39103
11 QApplication::notify(QObject*, QEvent*) /usr/lib/ 0 0x4fe3e67b
12 QCoreApplication::notifyInternal(QObject*, QEvent*) /usr/lib/ 0 0x4fb7e5fe
13 QWidgetPrivate::setGeometry_sys(int, int, int, int, bool) /usr/lib/ 0 0x4fedf39e
14 QWidget::setGeometry(QRect const&) /usr/lib/ 0 0x4fe8e7c5
15 QAbstractScrollAreaPrivate::layoutChildren() /usr/lib/ 0 0x5034392b
16 QAbstractScrollArea::event(QEvent*) /usr/lib/ 0 0x50344b0d
17 QAbstractItemView::event(QEvent*) /usr/lib/ 0 0x503f3c9c
18 QListView::event(QEvent*) /usr/lib/ 0 0x5040b064
19 QListWidget::event(QEvent*) /usr/lib/ 0 0x5045a294
20 QApplicationPrivate::notify_helper(QObject*, QEvent*) /usr/lib/ 0 0x4fe39134

I'm not sure whats going wrong because the fault seems to happen in QAbstractItemView::model. Also right before the segmentation fault occurs I can see it has selected items but not the right ones. Explore from the code is my class it inherits QListWidget. The commented out code is stuff I've tried already and the only that has worked is...

this->selectionModel()->select(CurInd, QItemSelectionModel::SelectCurrent);
but it only works with the last item. Anybody know whats happening? or got a better way to maintain selection and allow the QListWidgetItems to move to fit the widget's size?

Thanks in advance.

This post has been edited 2 times, last edit by "zedth2" (Apr 1st 2012, 6:32pm)


Sunday, April 1st 2012, 5:55pm

unable to read

use code markers


Monday, April 2nd 2012, 12:23pm

What does that do?

and why use 'this->' everywhere? It is not needed.
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.


Monday, April 2nd 2012, 12:46pm

this->AddFileSysItems() is what repopulates the QListWidget. When the size changes I clear out the QListWidgets item's and then re-add them so that they span the whole widget.


Monday, April 2nd 2012, 5:24pm

when I ask 'what does it do', I really mean, 'you need to show us the code' :)

From you description it sounds like it could be the cause as well.
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.