Results 1 to 6 of 6

Thread: Custom editor for derived QTreeWidgetItem in a QTreeWidget

  1. #1
    Join Date
    Jan 2011
    Location
    Paris
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Custom editor for derived QTreeWidgetItem in a QTreeWidget

    Hi all,

    I am trying to create an application which displays a scene graph from an OpenSceneGraph file. For now, it works. I started with an already existing project, and have created my own branch to make some tests. The whole code is available on github: QosgSceneEdit.

    Now, what I want is to set for each item in the QTreeView a custom editor to change the properties of the associated OSG node. For instance, if my node is a PagedLOD, I would like to be able to change its ranges, taht is to say, the editor has to provide some QLineEdit with QDoubleValidator. I tried, but I cannot manage to find a way to do this. From what I have read, it seems that I have to use a custom class derived from QStyledItemDelegate. However, I cannot figure what should I reimplement in the derived class, and, moreover, how can I attach a dedicated QStyledItemDelegate based on the node type contained in the item (PagedLODDelegate for items containing an osg::PagedLOD, and so on).

    Any help would be appreciated. And, if you could provide some code, or a detailled step by step solution, it would be perfect!

    Regards,

    Olivier

  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: Custom editor for derived QTreeWidgetItem in a QTreeWidget

    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2011
    Location
    Paris
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Custom editor for derived QTreeWidgetItem in a QTreeWidget

    Quote Originally Posted by wysota View Post
    OK, thanks wysota. However, how can I set a dedicated
    Qt Code:
    To copy to clipboard, switch view to plain text mode 
    for each (node) item type. I will have several
    Qt Code:
    To copy to clipboard, switch view to plain text mode 
    and need a way to choose the right one based on the osg node contained in the item. Or, I can try to pass the item type to the delegate, but, how can I do this?

    Regards

  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: Custom editor for derived QTreeWidgetItem in a QTreeWidget

    You don't set different delegates for different node types. When createEditor() is called, you receive the model index which you are about to edit. Based on that determine what node you are dealing with and create a proper editor for it.

    Qt Code:
    1. ...::createEditor(..., ..., index) {
    2. switch(determineTypeFromIndex(index)) {
    3. case someType: return new QLineEdit;
    4. case someOtherType: return new SomeOtherTypeOfWidget;
    5. // ...
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2011
    Location
    Paris
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Custom editor for derived QTreeWidgetItem in a QTreeWidget

    Quote Originally Posted by wysota View Post
    You don't set different delegates for different node types. When createEditor() is called, you receive the model index which you are about to edit. Based on that determine what node you are dealing with and create a proper editor for it.

    Qt Code:
    1. ...::createEditor(..., ..., index) {
    2. switch(determineTypeFromIndex(index)) {
    3. case someType: return new QLineEdit;
    4. case someOtherType: return new SomeOtherTypeOfWidget;
    5. // ...
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 
    OK. But how can I retrieve the right QTreeWidgetItem? I really do not understand why QTreeWidget::itemFromIndex is protected ... Does a workaround exists?

    Quote Originally Posted by wysota View Post
    You don't set different delegates for different node types. When createEditor() is called, you receive the model index which you are about to edit. Based on that determine what node you are dealing with and create a proper editor for it.

    Qt Code:
    1. ...::createEditor(..., ..., index) {
    2. switch(determineTypeFromIndex(index)) {
    3. case someType: return new QLineEdit;
    4. case someOtherType: return new SomeOtherTypeOfWidget;
    5. // ...
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 
    OK. But how can I retrieve the right QTreeWidgetItem? I really do not understand why QTreeWidget::itemFromIndex is protected ... Does a workaround exists?

    Quote Originally Posted by wysota View Post
    You don't set different delegates for different node types. When createEditor() is called, you receive the model index which you are about to edit. Based on that determine what node you are dealing with and create a proper editor for it.

    Qt Code:
    1. ...::createEditor(..., ..., index) {
    2. switch(determineTypeFromIndex(index)) {
    3. case someType: return new QLineEdit;
    4. case someOtherType: return new SomeOtherTypeOfWidget;
    5. // ...
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 
    OK. But how can I retrieve the right QTreeWidgetItem? I really do not understand why QTreeWidget::itemFromIndex is protected ... Does a workaround exists?

    Quote Originally Posted by wysota View Post
    You don't set different delegates for different node types. When createEditor() is called, you receive the model index which you are about to edit. Based on that determine what node you are dealing with and create a proper editor for it.

    Qt Code:
    1. ...::createEditor(..., ..., index) {
    2. switch(determineTypeFromIndex(index)) {
    3. case someType: return new QLineEdit;
    4. case someOtherType: return new SomeOtherTypeOfWidget;
    5. // ...
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 
    OK. But how can I retrieve the right QTreeWidgetItem? I really do not understand why QTreeWidget::itemFromIndex is protected ... Does a workaround exists?


    Added after 6 minutes:


    Quote Originally Posted by olivier1978 View Post
    OK. But how can I retrieve the right QTreeWidgetItem? I really do not understand why QTreeWidget::itemFromIndex is protected ... Does a workaround exists?
    Seems that it does the trick:
    Qt Code:
    1. QTreeWidgetItem *item = (QTreeWidgetItem*)index.internalPointer();
    2. TreeViewItem* treeitem = dynamic_cast<TreeViewItem*>(item);
    3. if (treeitem)
    4. {
    5. // ...
    6. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by olivier1978; 28th February 2013 at 11:12.

  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: Custom editor for derived QTreeWidgetItem in a QTreeWidget

    Quote Originally Posted by olivier1978 View Post
    OK. But how can I retrieve the right QTreeWidgetItem?
    In theory you shouldn't need to. You should put enough information in the item data to be able to operate on the data (e.g. determine the type) using solely QModelIndex::data(). And if you ask me, you shouldn't be using QTreeWidgetItem in the first place. Since you already have a data structure (OSG tree) you should implement a proper model around this. It will definitely pay back in future.

    I really do not understand why QTreeWidget::itemFromIndex is protected ...
    Because there shouldn't be a need to extract the item pointer from an index outside the tree widget. QTreeWidget already provides item-based alternatives for all index-based signals and methods.

    Does a workaround exists?
    If you really need that, you can subclass the tree widget and create a public method that will call itemFromIndex.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 4
    Last Post: 22nd September 2011, 19:37
  2. QTreeWidget::setCurrentItem ( QTreeWidgetItem * item )
    By gboelter in forum Qt Programming
    Replies: 1
    Last Post: 14th June 2011, 22:21
  3. Unable to remove QTreeWidgetItem row from Qtreewidget
    By moh.gup@gmail.com in forum Qt Programming
    Replies: 2
    Last Post: 1st December 2010, 05:40
  4. Customizing QTreeWidget and QTreeWidgetItem
    By mots_g in forum Qt Programming
    Replies: 0
    Last Post: 18th August 2010, 06:53
  5. QTreeWidget Custom Editor Question
    By chaoticbob in forum Qt Programming
    Replies: 2
    Last Post: 30th October 2008, 07:06

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.