Results 1 to 5 of 5

Thread: Adding complex widget to QTreeView

  1. #1
    Join Date
    Nov 2007
    Posts
    16
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Adding complex widget to QTreeView

    Hello all,

    I've created a QTreeView with a custom model (so I can't use QTreeWidget) and I'd like to use a custom widget I've made for one of the columns. I saw a previous post in the forums that said that the best method is to use a delegate (rather than setIndexWidget() which will be slow for many items). I've written a custom delegate, and by reimplementing the paint event for the delegate, I'm able to get my widget drawn properly in the column. However, when I click on the widget, it doesn't do anything (not getting mouse events?). My custom widget is somewhat similar to a QComboBox, you can click on it and a custom popup will come up. In any case, does anyone know have any ideas about how to get my widget to receive mouse events?

    Thanks!

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Adding complex widget to QTreeView

    However, when I click on the widget, it doesn't do anything (not getting mouse events?).
    What do you want happen? How do you receive mouse events?
    Did you try
    Qt Code:
    1. bool QAbstractItemDelegate::editorEvent(QEvent * event, QAbstractItemModel * model, const QStyleOptionViewItem & option, const QModelIndex & index) [virtual]
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Nov 2007
    Posts
    16
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Adding complex widget to QTreeView

    Yes, I tried subclassing editorEvent and passing the QEvent to QWidget::event(). Imagine that my custom widget is just a QCombobox. I want it when someone clicks on the QCombox that the widget receives the mouseClick event as normal, and the popup window appears. I can't seem to get that to work. Does anyone have any example code on how to do this with a delgate? Thanks!

  4. #4
    Join Date
    Nov 2007
    Posts
    16
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Adding complex widget to QTreeView

    Let me try to explain a little bit more what I am trying to do. Imagine my complex widget is a QWidget subclass that contains some buttons. I create a QStyledItemDelegate with all the functions to properly use the QWidget subclass. I set the delegate on a column of my QTreeView, and it draws the widget fine. However, I'm trying to get the QEvent::MouseButtonPress properly passed to the widget so when the user clicks the button in the QTreeView, the proper code is called. If I implement QStyledItemDelegate::editorEvent(), it gets the QEvent::MouseButtonPress. But I can't figure out how to properly pass that on to my QWidget subclass. Since there are multiple buttons in my QWidget subclass, I want to avoid calculating which button was pressed based upon the coordinates (which I could of course do). I should somehow be able to pass the QEvent to the QWidget subclass so that the buttons just work, right? Any ideas how to do that? Thanks!

  5. #5
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Adding complex widget to QTreeView

    If you are trying use custom widget for normal display (non-editing mode) then you have to use setIndexWidget(), the widget will receive mouse events.

    When using delegate paint to draw widget (like comboBox, pushbutton etc), there is no pushbutton present at all, it is just painting on item, and more over the editorEvent() is for editor widgets, not for display / painting widgets.

    You have to use setIndexWidget() to properly receive mouse / keyboard events.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

Similar Threads

  1. complex search proxy for QTreeView
    By brixton in forum Newbie
    Replies: 1
    Last Post: 19th November 2010, 22:10
  2. Replies: 2
    Last Post: 17th October 2010, 12:16
  3. Replies: 2
    Last Post: 17th June 2010, 20:24
  4. QTreeView adding children to the view
    By Tux-Slack in forum Qt Programming
    Replies: 2
    Last Post: 20th October 2007, 09:28
  5. casting complex widget pointers
    By TheKedge in forum Qt Programming
    Replies: 4
    Last Post: 8th February 2007, 18:52

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.