Results 1 to 3 of 3

Thread: QCombobox with QTreeView/QTreeWidget

  1. #1
    Join Date
    Oct 2007
    Location
    Poland
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QCombobox with QTreeView/QTreeWidget

    I have problem with QComboBox::setCurrentIndex( int ). I have siply tree model, like this ( QComboBox::setModel() and QComboBox::setView() ) :
    ---- A // index = 0
    ----------- B // index = 0
    ------------ C // index = 1
    ----------- D // index = 2
    ----- E // index = 1
    ----- F // index = 2
    ----- G // index = 3
    B,C and D have parent A. So, when i call QComboBox::setCurrentIndex( 1 ), then currentIndex is for E. But how can i set currentIndex for C ? SetCurrentIndex / SetCurrentItem in model / view doesn't work. Any idea?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QCombobox with QTreeView/QTreeWidget

    From what I know the combobox doesn't work well with hierarchical models. You can use setRootModelIndex() but then you won't see the indexes higher in the hierarchy.

  3. #3
    Join Date
    Aug 2011
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QCombobox with QTreeView/QTreeWidget

    I have been digging for this for a while and just figured out a solution. Just going to put it here in case anyone ever needs something similar. The approach is to set the root model index to the parent of the element you want, set the current index to the element index within the element's siblings, and then to set the root model index back to the invisiblerootitem of the tree. For my code, it was something like this (loDeviceTree was the QTreeWidget, and loDeviceComboBox was the comboBox that contained loDeviceTree as its model/view):



    loDeviceTree->setCurrentItem(DeviceIWant->parent(),0);
    loDeviceComboBox->setRootModelIndex(loDeviceTree->currentIndex());
    loDeviceComboBox->setCurrentIndex(IndexOfTheDeviceIWant)
    loDeviceTree->setCurrentItem(loDeviceTree->invisibleRootItem(),0);
    loDeviceComboBox->setRootModelIndex(loDeviceTree->currentIndex());

Similar Threads

  1. Editable QComboBox with QItemDelegate
    By Jmgr in forum Qt Programming
    Replies: 11
    Last Post: 10th December 2008, 09:21
  2. QComboBox drop list button events
    By maird in forum Qt Programming
    Replies: 5
    Last Post: 20th October 2007, 19:25
  3. QComboBox in QTableWidget : display troubles.
    By Nyphel in forum Qt Programming
    Replies: 2
    Last Post: 13th October 2007, 23:29
  4. using QComboBox as an ItemView
    By EricTheFruitbat in forum Qt Programming
    Replies: 3
    Last Post: 24th January 2007, 16:14
  5. QDataWidgetMapper <=> QComboBox best practice
    By saknopper in forum Qt Programming
    Replies: 1
    Last Post: 18th January 2007, 10:50

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.