Results 1 to 12 of 12

Thread: How to drop as QTreeWidgetItem subclass

  1. #1
    Join Date
    Sep 2009
    Posts
    19
    Qt products
    Platforms
    Unix/X11

    Default How to drop as QTreeWidgetItem subclass

    I'm implementing a drag-n-drop between a list and a tree where I have different sorts of QTreeWidgetItem subclasses. I want the dropped list item to be inserted as QTreeWidgetCustomItem instead of QTreeWidgetItem. How could I achieve that?

  2. #2
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: How to drop as QTreeWidgetItem subclass

    You could derive your QTreeWidgetCustomItem from QTreeWidgetItem.

  3. #3
    Join Date
    Sep 2009
    Posts
    19
    Qt products
    Platforms
    Unix/X11

    Default Re: How to drop as QTreeWidgetItem subclass

    QTreeWidgetCustomItem is a subclass of QTreeWidgetItem with additional features. Now when I drop an element from another view, it gets inserted as QTreeWidgetItem. I'd like it to be inserted as QTreeWidgetCustomItem. Is this possible?
    For now, I'm gonna do it by replacing it as soon as it gets inserted but if there's a cleaner way, that would be better...

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: How to drop as QTreeWidgetItem subclass

    Learn about casting. Try a qobject_cast for example

  5. #5
    Join Date
    Sep 2009
    Posts
    19
    Qt products
    Platforms
    Unix/X11

    Default Re: How to drop as QTreeWidgetItem subclass

    Didn't get very far in that casting thing but it seems it doesn't fit my case:
    - I'm using Python
    - QTreeWidgetItem doesn't inherit QObject

  6. #6
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: How to drop as QTreeWidgetItem subclass

    Your table is already storing your CustomItem as a CustomItem; it doesn't get converted to an Item, that's just the OO approach for handling inheritance. The (bad) alternative would be to require everyone to also reimplement Table to accept customized Items. Not a good plan.

    IN C++, you simply cast your Item pointer to a CustomItem, checking to make sure the cast actually worked; it is, after all, already a CustomItem, you just have to tell the runtime to treat it as such. How to cast from base to derived class in Python is left as an exercise, particularly since I'm not a Python programmer. It is certainly possible, however.

  7. #7
    Join Date
    Sep 2009
    Posts
    19
    Qt products
    Platforms
    Unix/X11

    Default Re: How to drop as QTreeWidgetItem subclass

    Well, let's give some more details as it doesn't seem to be very clear.

    I have a QTreeWidget and a QListWidget.

    In the QTreeWidget I have items of 4 different subclasses of QTreeWidgetItem. Clicking on items fires actions depending of the item type.

    What I have now: drag-n-dropping from the QListWidget creates generic QTreeWidgetItem which my program doesn't know what to do with...

    What I want to achieve: drag-n-dropping from the QListWidget creates items of one specific type in the QTreeWidget.

  8. #8
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: How to drop as QTreeWidgetItem subclass

    See my previous reply; the items aren't "created" all over again as generic items. They're the same CustomItems you handed the tree to begin with. You have to cast the tree's base pointer to the type of your derived class in order to access its particular features, but they're still there.

    Python allows you to do the same thing. Read up on casting, derived classes, base classes and type promotion.

  9. #9
    Join Date
    Sep 2009
    Posts
    19
    Qt products
    Platforms
    Unix/X11

    Default Re: How to drop as QTreeWidgetItem subclass

    Quote Originally Posted by SixDegrees View Post
    See my previous reply; the items aren't "created" all over again as generic items. They're the same CustomItems you handed the tree to begin with. You have to cast the tree's base pointer to the type of your derived class in order to access its particular features, but they're still there.
    Well... I must be misunderstanding something...

    Here are the existing tree items
    Qt Code:
    1. <__main__.treeItemSwatch object at 0xa591bac>
    2. <__main__.treeItemSwatch object at 0xa591eac>
    3. <__main__.treeItemGroup object at 0xa64fe6c>
    4. <__main__.treeItemSpacer object at 0xa64feac>
    5. <__main__.treeItemBreak object at 0xa64f06c>
    6. <__main__.treeItemSwatch object at 0xa64f0ec>
    To copy to clipboard, switch view to plain text mode 
    And here are the dropped items...
    Qt Code:
    1. <PyQt4.QtGui.QTreeWidgetItem object at 0xa659bac>
    2. <PyQt4.QtGui.QTreeWidgetItem object at 0xa655d2c>
    3. <PyQt4.QtGui.QTreeWidgetItem object at 0xa655e2c>
    4. <PyQt4.QtGui.QTreeWidgetItem object at 0xa65932c>
    To copy to clipboard, switch view to plain text mode 

    How are these supposed to be "the same CustomItems"? How Qt would guess which of my 4 custom types I want it to be?

    Quote Originally Posted by SixDegrees View Post
    Python allows you to do the same thing. Read up on casting, derived classes, base classes and type promotion.
    Could you point me to some references? As far as I've searched now, I read type casting doesn't exist in Python...

  10. #10
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: How to drop as QTreeWidgetItem subclass

    A TreeItemSwatch for example is based on a TreeWidgetItem? If so, the base class is a TreeWidgetItem. Since a TreeWidget can't possibly know all custom items, it uses the base class. It's up to you to tell it that it is actually a specialised version, and you do this with casting for example. How this is done in Python, I don't know. Did you try TreeItemSwatch(TreeWidgetItem) ?

  11. #11
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: How to drop as QTreeWidgetItem subclass

    I don't know how Python does it, as already mentioned. I know that it does, however; this is possibly the most common feature of all OO languages. It would be impossible to do run time type evaluation without it, and inheritance would lose most of its value.

    I would imagine this would be treated in any Python reference early on.

  12. #12
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: How to drop as QTreeWidgetItem subclass

    How Qt would guess which of my 4 custom types I want it to be?
    It can't - that's the whole point. C++ (not Qt) also cannot know what sort of derived class you're going to drop into the tree, so it stores all derived classes as a pointer to base object. It's YOUR responsibility to know what's in there and cast it to the correct type, or use whatever idiom for doing something similar is available in Python.

Similar Threads

  1. QListWidgetItem subclass - Crash program in drag/drop
    By estanisgeyer in forum Qt Programming
    Replies: 1
    Last Post: 17th April 2009, 09:24
  2. Drag and Drop in a QTreeWidget subclass
    By kishore in forum Qt Programming
    Replies: 2
    Last Post: 14th May 2008, 07:12
  3. Problem emiting signal in QTreeWidgetItem's subclass
    By Shawn in forum Qt Programming
    Replies: 12
    Last Post: 4th September 2007, 12:08
  4. Can I subclass QTreeWidgetItem?
    By Shawn in forum Qt Programming
    Replies: 4
    Last Post: 2nd September 2007, 16:37
  5. Drag & Drop QTreeWidgetItem with editors
    By Mad Max in forum Qt Programming
    Replies: 3
    Last Post: 25th January 2007, 09:09

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.