View Full Version : Adding complex widget to QTreeView

9th June 2013, 19:10
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?


Santosh Reddy
10th June 2013, 09:13
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
bool QAbstractItemDelegate::editorEvent(QEvent * event, QAbstractItemModel * model, const QStyleOptionViewItem & option, const QModelIndex & index) [virtual]

11th June 2013, 01:34
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!

12th June 2013, 03:14
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!

Santosh Reddy
3rd August 2013, 15:18
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.