PDA

View Full Version : context menu advice



larry104
3rd October 2006, 23:54
Hi,

I would need some advice how to implement context menus the right way.

Here the situation:

I have a main widget which contains two tableviews each assigned to a model. Right click over a row triggers a different action for each of the two tables.

scenario 1:
subclass tableview and implement
void MyTableView1::contextMenuEvent(QContextMenuEvent* e) and
void MyTableView2::contextMenuEvent(QContextMenuEvent* e)
Check if
QModelIndex index = indexAt(e->pos());
is valid
Problem 1: hard to get from here to the model data to change something.

scenario 2:
Implement contextMenuEvent(QContextMenuEvent* e) for main window and check if
QModelIndex index = table1->indexAt(e->pos());
QModelIndex index = table2->indexAt(e->pos());
are valid.
Problem 2: e->pos() gives me the position releative to the main window. Is there a way to translate these positions relative to the tableViews?

So, in summary what is the correct way to implement context menus: globally or relative to the widget

Thanks.

jpn
4th October 2006, 07:55
Problem 1: hard to get from here to the model data to change something.

Why? You can access the model through QAbstractItemView::model().


model()->setData(index, data, role);




Problem 2: e->pos() gives me the position releative to the main window. Is there a way to translate these positions relative to the tableViews?

Yes, see docs for QWidget::mapFrom*() and QWidget::mapTo*().