Results 1 to 8 of 8

Thread: QItemSelectionModel::setCurrentIndex doesn't work

  1. #1
    Join Date
    May 2011
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    2

    Default QItemSelectionModel::setCurrentIndex doesn't work

    My code:
    Qt Code:
    1. void TreeModel::selectIndex(QModelIndex ix) {
    2. if (!ix.isValid()) return;
    3. qDebug() << "name1" << ix.data();
    4. tree->selectionModel()->clear();
    5. tree->setExpanded(ix.parent(), true);
    6. tree->selectionModel()->setCurrentIndex(ix, QItemSelectionModel::SelectCurrent);
    7. tree->scrollTo(ix);
    8. }
    To copy to clipboard, switch view to plain text mode 

    This snippet runs ok after deleting/inserting nodes. But when I try to select the remembered node from previous session in QMainWindow::showEvent the result is:
    Qt Code:
    1. name1 QVariant(QString, "Highlight elements")
    2. The program has unexpectedly finished.
    3. C:\Home\develop\qt\arm\designer\Designer exited with code -1073741819
    To copy to clipboard, switch view to plain text mode 

    without
    Qt Code:
    1. tree->selectionModel()->setCurrentIndex(ix, QItemSelectionModel::SelectCurrent);
    To copy to clipboard, switch view to plain text mode 
    works well, but I do need to select this item.

    Thank you very much in advance!
    Last edited by evgeniy; 16th February 2014 at 22:22.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: QItemSelectionModel::setCurrentIndex doesn't work

    You cannot store QModelIndexes between sessions, they are create-use-destroy objects.

  3. #3
    Join Date
    May 2011
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    2

    Default Re: QItemSelectionModel::setCurrentIndex doesn't work

    Quote Originally Posted by ChrisW67 View Post
    You cannot store QModelIndexes between sessions, they are create-use-destroy objects.
    I know it. See qDebug. Read carefully: without "tree->selectionModel()->setCurrentIndex(ix, QItemSelectionModel::SelectCurrent);" works well...

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: QItemSelectionModel::setCurrentIndex doesn't work

    Read carefully: without "tree->selectionModel()->setCurrentIndex(ix, QItemSelectionModel::SelectCurrent);" works well...
    So what is the problem again?

    exited with code -1073741819
    -1073741819 == 0xC0000005 Access Violation

    Your debug output shows that (with reasonable likelihood) that the index points to an item in a valid model. It does not show that the index is related in any way to the model currently displayed in the tree, but I am willing to assume that it is because that would not lead to a crash anyway. It does not show that tree is a valid pointer. It does not show that tree->selectionModel() is not 0. Either pointer being broken seems reasonably possible given the outcome. Exactly which line is your debugger telling you the crash occurred at? What is the backtrace to that point? What is the value of tree at this point? What does tree->selectionModel() return at that point?

  5. #5
    Join Date
    May 2011
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    2

    Default Re: QItemSelectionModel::setCurrentIndex doesn't work

    tree->selectionModel()->clear(); works
    tree->setExpanded(ix.parent(), true); works
    tree->scrollTo(ix); works
    Could you tell me how to get backtrace from qt creator/gdb?

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: QItemSelectionModel::setCurrentIndex doesn't work

    The "bt" gdb command:
    Qt Code:
    1. $ gdb ./mydebugexe
    2. (gdb) run
    3. ...
    4. Program received signal SIGSEGV, Segmentation fault.
    5. 0x00007ffff7ac5ba7 in ?? () from /lib64/libc.so.6
    6. (gdb) bt
    7. #0 0x00007ffff7ac5ba7 in ?? () from /lib64/libc.so.6
    8. #1 0x0000000000400597 in test () at main.cpp:6
    9. #2 0x00000000004005ad in main (argc=1, argv=0x7fffffffdae8) at main.cpp:10
    To copy to clipboard, switch view to plain text mode 

    If you are using gdb inside Qt Creator then you get a backtrace automatically.

  7. The following user says thank you to ChrisW67 for this useful post:

    evgeniy (20th February 2014)

  8. #7
    Join Date
    May 2011
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    2

    Default Re: QItemSelectionModel::setCurrentIndex doesn't work

    debugger log:
    >~"\nProgram received signal "
    >~"SIGSEGV, Segmentation fault.\n"
    >~"0x00438290 in QVariant::Private::Private (this="
    >&"warning: (Internal error: pc 0x0 in read in psymtab, but not in symtab.)\n"
    (Internal error: pc 0x0 in read in psymtab, but not in symtab.)
    >&"\n"
    >~"0x0) at ../../../../../../Qt/Qt5.0.2/5.0.2/mingw47_32/include/QtCore/qvariant.h:367\n"
    >~"367\t inline Private(): type(Invalid), is_shared(false), is_null(true)\n"

    >~"data=[{iname=\"local.node\",name=\"node\",addr=\"0x14384 220\",addr=\"0x14384220\",numchild=\"9\",origaddr= \"0x28d86c\",type=\"DNode\",value=\"{...}\",},{ina me=\"local.this\",name=\"this\",addr=\"0x14341820\ ",addr=\"0x14341820\",numchild=\"5\",origaddr=\"0x 28d890\",type=\"TreeModel\",value=\"{...}\",},{ina me=\"local.index\",name=\"index\",addr=\"0x28dba8\ ",numchild=\"0\",type=\"QModelIndex &\",value=\"(invalid)\",},{iname=\"local.role\",na me=\"role\",addr=\"0x28d898\",numchild=\"0\",type= \"int\",value=\"1\",},],typeinfo=[{name=\"aW50\",size=\"4\"}{name=\"Y29uc3QgUU1vZGVs SW5kZXggJg==\",size=\"4\"}{name=\"RE5vZGUgKg==\",s ize=\"4\"}{name=\"RE5vZGU=\",size=\"36\"}]\n"
    >2390^done
    dDISCARDING JUNK AT BEGIN OF RESPONSE:
    dProgram received signal SIGSEGV, Segmentation fault.
    d0x00438290 in QVariant::Private::Private (this=0x0) at ../../../../../../Qt/Qt5.0.2/5.0.2/mingw47_32/include/QtCore/qvariant.h:367
    d367 inline Private(): type(Invalid), is_shared(false), is_null(true)
    <Rebuild Watchmodel 243>
    sFinished retrieving data

  9. #8
    Join Date
    May 2011
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    2

    Default Re: QItemSelectionModel::setCurrentIndex doesn't work

    It's a Qt bug. The only way out:
    Qt Code:
    1. void MainWindow::showEvent(QShowEvent *event) {
    2. QMainWindow::showEvent(event);
    3. QTimer::singleShot(0, this, SLOT(selectLastNode()));
    4. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QComboBox setCurrentIndex doesn't work
    By trallallero in forum Qt Programming
    Replies: 4
    Last Post: 8th November 2011, 09:37
  2. autoScale doesn't work
    By Braunstein in forum Qwt
    Replies: 4
    Last Post: 12th November 2010, 19:22
  3. Example in Qt labs doesn't work
    By matko in forum Newbie
    Replies: 1
    Last Post: 19th November 2009, 19:59
  4. setCurrentIndex() doesn't display cursor(bounding rect)
    By DIMEDROLL in forum Qt Programming
    Replies: 0
    Last Post: 5th November 2008, 07:24
  5. setWindowModality doesn't work
    By dude in forum Qt Programming
    Replies: 3
    Last Post: 15th February 2008, 15:11

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.