开发者

QTableWidget - context menu only for specific cells/columns

开发者 https://www.devze.com 2023-03-07 13:40 出处:网络
I am using a QTableWidget for displaying data. I开发者_如何转开发 know that I can use the addAction method to add a context menu. How can I limit the context menu to only specific cells or columns? ad

I am using a QTableWidget for displaying data. I开发者_如何转开发 know that I can use the addAction method to add a context menu. How can I limit the context menu to only specific cells or columns? addAction exists for QActionGroup QGraphicsWidget QMenu QMenuBar QToolBar QWidget. Should I somehow filter oder disable/enable the signal/slots? Work with right click events?

A similar question would be how to get different context menu's for different rows?

Thank you and cheers, Matthias


Another method to create custom context menus is to implement a slot to the QWidget::customContextMenuRequested() signal. There you can query the cell under the position (QTableWidget::itemAt() - watch out for global->widget mapping!), and then build a custom menu using QMenu and QAction.

Also, I'd build the menu(s) beforehand, and than only exec() it in the slot.

Remember that you have to change the QWidget::ContextMenuPolicy property of the widget to Qt::CustomContextMenu!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号