Results 1 to 8 of 8

Thread: QTreeWidget select item

  1. #1
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default QTreeWidget select item

    Hi,

    I'm porting my application from Qt 4.1 to 4.3.

    I have a QTreeWidget that selects items depending on internal data. In Qt 4.1 I use "QTreeWidget ::setItemSelected", but now this method is obsolete and I don't realy know how to get it works. I readed that I have to use "QTreeWidgetItem::setItemSelected" instead. My QTreeWidget is configured as "SingleSelection" mode selection and "SelectItems" as behavior selection.

    I'm right if I say that I have to use "QTreeWidget::setCurrentItem(item)" and "QTreeWidgetItem::setSelected(true)" to the same item?

    Thanks,
    Òscar Llarch i Galán

  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: QTreeWidget select item

    the selected item may be different than the current item, so it's really up to you to decide if you want the selected item to be current or not.

  3. #3
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTreeWidget select item

    Hi,

    the selected item may be different than the current item, so it's really up to you to decide if you want the selected item to be current or not.
    Yes, I want.

    Thanks, but I have another question:
    I have a root item, I create a child and I insert a QComboBox into column 1 of this child. I've connected the "QComboBox::currentIndexChanged(int)" to a SLOT. When the SLOT is called I want to get the item that is in the column 0.

    Qt Code:
    1. ...
    2. pqItem = new QTreeWidgetItem(pqRoot);
    3. pqComboBox = new QComboBox();
    4. m_pqTree->setItemWidget(pqItem ,1,pqComboBox);
    5. connect(pqComboBox,SIGNAL(currentIndexChanged(int)),this,SLOT(setMode(int)));
    6. ...
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. void myClass::setMode(int)
    2. {
    3. //Here I want to get the QTreeWidgetItem*. I have tested QObject::sender()->parent(), but I don't really know if it is its parent.
    4. }
    To copy to clipboard, switch view to plain text mode 

    The problem is that when I click on the QComboBox, the QTreeWidgetItem* is not selected and I don't want to force first click the item an then let change the combo.

    Thanks,
    Òscar Llarch i Galán

  4. #4
    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: QTreeWidget select item

    The item has nothing to do with the combobox. You can save an association between the index and the widget, but I don't see what you need it for? Could you explain what you are trying to obtain?

  5. #5
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTreeWidget select item

    Hi,

    What I want is like an item that in first column has the name and an icon representing its state(green=enabled, red=disabled). The combo is inserted in the second column of the item, so whe the user change the combo(enabled or disabled are the possibilities), the icon of the item is changed.

    I have solved by a different way that is working well for me.
    I have internal data that is represented by the tree. When I change one combo to disabled or enabled, I change the internal data(is a object list) and I do a "for" getting the "state" variable and depending on the position of the list I change the corresponding item.

    Thanks for replying,
    Òscar Llarch i Galán

  6. #6
    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: QTreeWidget select item

    A strange requirement... but if you insist this is what you want, I'd provide a custom delegate that would return a combobox as an editor (see QAbstractItemDelegate::createEditor). Then either the model should react to setData in that column and/or you should also reimplement setModelData() in the delegate to set the icon in the appropriate column. Or even make the delegate display icon in column 1 based on the contents of another column.

    Your solution seems a bit crude and slow, but if it does what you want...

  7. #7
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTreeWidget select item

    Hi,

    Thanks.

    The problem is that I don't have a model. I have readed about view/model programming but the problem is that I did wrote my code pure C++, creating LinkedLists manually, ... Then I decided wich toolkit to use and mixed the code. By this I use QTreeWidget instead of QTreeView, because I don't know how to convert my code to a model.

    I know that this lines of code are slow but it is not important.

    The problem of using a model/view interface is that I really don't know how to rewrite the code struct to use it.

    Thanks your help,
    Òscar Llarch i Galán

  8. #8
    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: QTreeWidget select item

    Of course you have a model. QTreeWidget uses an internal model that uses QTreeWidgetItems, so you can call model methods like you would for real models, so the delegate approach is still valid.

  9. The following user says thank you to wysota for this useful post:

    ^NyAw^ (26th October 2007)

Similar Threads

  1. Inserting Item to a QTreeWidget from Thread?
    By vishal.chauhan in forum Qt Programming
    Replies: 3
    Last Post: 23rd June 2007, 11:23
  2. Cannot select an item in QComboBox
    By Declan in forum Qt Programming
    Replies: 2
    Last Post: 19th April 2007, 08:08
  3. QTreeWidget item editing: want column specificity
    By McKee in forum Qt Programming
    Replies: 12
    Last Post: 10th December 2006, 22:12
  4. Removing items properly from qtreewidget item
    By Djony in forum Qt Programming
    Replies: 6
    Last Post: 21st November 2006, 12:20
  5. insert Item to my QTreeWidget
    By raphaelf in forum Qt Programming
    Replies: 7
    Last Post: 28th February 2006, 15:42

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.