Important: Please read the Qt Code of Conduct -

[Solved] Showing QMenu in QListView

  • Hello All Qt developers!,

    I've been recently working on widget which presents some items inside QListView widget. Let's say it presents some files from a disk directory. I'm using subclassed and customized QAbstractItemModel to hold data and this works fine. The problem I've be facing is with showing QMenu. I created QMenu and I set contextMenuPolicy to: Qt::CustomContextMenu. So far everything is ok, because parent widget emits QWidget::customContextMenuRequested ( const QPoint & pos ) signal. This works as long as I use mouse (right mouse button - standard Qt solution). The problem appears when I want to show QMenu with "menu" button (application is being developed for embedded system device, which won't have a mouse, but will have "menu" button). I call QMenu::exec() and QMenu appears but at top left corner at QPoint(0,0).

    What I want to achieve is to find out/gather the QPoint for every item that is shown by QListView and with this point show QMenu next to item's name or icon. Do You have any idea how can I get this point from QListView or even more generally from QAbstractItemView classes?

    I figured out solution with method:
    @QModelIndex QAbstractItemView::indexAt ( const QPoint & point )@

    I simply recieved current selected item with: currentIndex() method and then I compared this item with item returned by indexAt(...) method. It's not very elegant, so maybe You know any other simple way?


  • All "Mad Scientiests" don't You have any idea??


  • Hi,

    QAbstractItemView::visualRect ( const QModelIndex & index ) const

    be of any use for you? This seems to return the bounding rect of the index, so you could choose a good popup position from it.

  • Thank You very much!

    I have no idea how I missed this in documentation.


Log in to reply