I have a subclass of QTreeView. I need a custom context menu for specific items in it. To get this I set the context menu policy and connect the signal "customContextMenuRequested" in the constructor of the subclass of QTreeView:
setContextMenuPolicy( Qt::CustomContextMenu );
QObject::connect( this, SIGNAL( customContextMenuRequested( const QPoint & ) ), this, SLOT( onCustomContextMenu( const QPoint & ) ) );
Now, in the slot function "onCustomContextMenu" I get the position for the context menu creation as a QPoint. I would like to get the QStandardItem that is shown on this position. I tried this:
void t_my_tree_view::onCustomContextMenu( const QPoint &point )
{
QModelIndex index = this->indexAt( point );
QStandardItem* item_ptr = m_item_model->item( index.row, index.column() );
}
m_item_model is a pointer to the QStandardItemModel that is the model in this subclass of the QTreeview.
The problem is, that the "item_ptr" that I get is wrong sometimes, or it is NULL. It will be NULL if my model looks like this:
invisibleRootItem
|-item_on_level_1
|-item_on_level_2
|-item_on_level_2
|-item_on_level_2 <-- this is the item where the right click was
|-item_on_level_2
What am I doing wrong? How can I get the item that i rightclick?