Results 1 to 4 of 4

Thread: Problems with getting QTreeView selected Index?

  1. #1
    Join Date
    Apr 2014
    Posts
    125
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Android Maemo/MeeGo

    Question Problems with getting QTreeView selected Index?

    Hello;

    I'm writing Qt widget application. I'm having a QTreeView located on a QTabWidget, this QTabWidget itself located on my QMainWindow.

    This following code does not give me the correct selected index when called from QMainWindow method:

    QModelIndex CurrentIndex = ui->MyTreeView->currentIndex();

    Which one of the four followings approach should I use:

    1)
    QModelIndexList indexes = ui->MyTreeView->selectionModel()->selection().indexes();

    // then capture the particular index in a loop

    2)
    QModelIndex currentIndex = ui->MTreeView->selectionModel()->currentIndex();

    3)
    QModelIndex CurrentIndex = ui->MreeView->currentIndex();

    4)
    void Parent::slot_MyTreeView_selectionChanged()
    {
    m_SelectedIndex= ui->MyTreeView->currentIndex();
    }

    Which one is the best?

    Is my problem a focus problem?

    Should I capture QTreeView selected index only when the QTreeView is visible and my mouse is on it (I'm asking this because sometimes the problematic code works fine)?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problems with getting QTreeView selected Index?

    The current selection does not necessarily have to be the current index.

    Current index can always only be one, while a view can have a lot of indexes selected.

    So it depends on what you want to achieve. The current index is the one with "keyboard focus".

    Cheers,
    _

  3. #3
    Join Date
    Apr 2014
    Posts
    125
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Android Maemo/MeeGo

    Default Re: Problems with getting QTreeView selected Index?

    Thank you for the reply. I want to have the index of the first item (column 0) of a row when the row is selected by the user.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problems with getting QTreeView selected Index?

    That sounds like a use case for reacting to the selectionChanged() signal and the extracting the wanted index from the connected slot's arguments.

    Cheers,
    _

Similar Threads

  1. recuperate index item selected in comboBox
    By johan07 in forum Newbie
    Replies: 6
    Last Post: 26th December 2012, 18:52
  2. Replies: 1
    Last Post: 7th August 2012, 12:41
  3. Replies: 1
    Last Post: 23rd December 2011, 08:30
  4. Replies: 1
    Last Post: 12th January 2011, 06:52
  5. Replies: 2
    Last Post: 4th December 2010, 08:09

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.