PDA

View Full Version : QMenu as QItemDelegate



munna
24th July 2009, 17:40
Hi!

I am trying to open a menu when user double clicks on a cell of the table view. The problem is that the menu is appearing at the point (0,0) of the screen and not at the place where the cell is located.

Is there a way by which I can get the coordinates of the cell and move the menu to that location?

Thanks

caduel
24th July 2009, 19:05
see QAbstractItemView::visualRect()

HTH

munna
25th July 2009, 20:32
Thanks Caduel!

I am aware of this function. But the problem is that I have re-implemented QItemDelegate class and in createEditor function I am creating the menu.

In order to show this menu in the right position in need the rect of the cell. But to get this I need to access QAbstractItemView function from the sub -class of QItemDelegate class.

Is there a way by which I can do this? What QWidget is the parent on the editor?

Thanks

faldzip
26th July 2009, 00:08
In the createEditor() method try to convert QStyleOptionViewItem &option to QStyleOptionViewItemV3 or later, because it has a member const QWidget *widget (from Qt source):


class Q_GUI_EXPORT QStyleOptionViewItemV3 : public QStyleOptionViewItemV2
{
public:
enum StyleOptionVersion { Version = 3 };

QLocale locale;
const QWidget *widget;

QStyleOptionViewItemV3();
QStyleOptionViewItemV3(const QStyleOptionViewItemV3 &other)
: QStyleOptionViewItemV2(Version) { *this = other; }
QStyleOptionViewItemV3(const QStyleOptionViewItem &other);
QStyleOptionViewItemV3 &operator = (const QStyleOptionViewItem &other);

protected:
QStyleOptionViewItemV3(int version);
};
There is high possibility that widget is a pointer to your view but it is not sure so first try to cast it with qobject_cast. If it is not the view, then you need to add the pointer to view in your delegate subclass and set it with your view while adding delegate to the view.

wysota
26th July 2009, 15:41
Creating QMenu as the editor is probably not the best idea. It would be better to reimplement contextMenuEvent() in the view or connect to appropriate click events in the view. Then using indexAt() you can determine which item the mouse was over and you can trigger the menu in appropriate point of the screen.