Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. mousePressedEvent handler for QTableView
Qt 6.12 LTS is out! See what's new in the release blog

mousePressedEvent handler for QTableView

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 833 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • PerdrixP Offline
    PerdrixP Offline
    Perdrix
    wrote on last edited by
    #1

    I want to handle RMB press events on the viewport and pop a menu when this is done.

    When a menu item is chosen, it should apply to all selected rows in the table view.

    1. I think I need to sub-class QTableView to achieve this? Or is there a "neat" way to handle this in the owning widget?

    2. I want to enable some menu items only if the mouse is over a row of the table (rather than a blank area) - how do I do that?

    3. I don't wish to react to RMB on the header, but I think I can just check horizontalHeader()->underMouse() - is that right?

    Thanks
    David

    JonBJ 1 Reply Last reply
    0
    • PerdrixP Perdrix

      I want to handle RMB press events on the viewport and pop a menu when this is done.

      When a menu item is chosen, it should apply to all selected rows in the table view.

      1. I think I need to sub-class QTableView to achieve this? Or is there a "neat" way to handle this in the owning widget?

      2. I want to enable some menu items only if the mouse is over a row of the table (rather than a blank area) - how do I do that?

      3. I don't wish to react to RMB on the header, but I think I can just check horizontalHeader()->underMouse() - is that right?

      Thanks
      David

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Perdrix

      1. You can usually achieve event handling without subclassing if you go via eventFilter(). But as you add functionality to a QTableView like this it becomes nicer if sub-classed. I would (think ) would be subclassing to override contextMenuEvent().

      2. QTableWidget has an itemAt(). For QTableView what about rowAt(int y) or indexAt(const QPoint &pos)? Check what coordinates the mouse position returns, may have to be mapped to table view, not sure.

      3. Probably yes, or it may come out in the wash from #2.

      1 Reply Last reply
      2
      • PerdrixP Offline
        PerdrixP Offline
        Perdrix
        wrote on last edited by
        #3

        @JonB said in mousePressedEvent handler for QTableView:

        contextMenuEvent()

        That was what was needed!

        Thanks

        JonBJ 1 Reply Last reply
        0
        • PerdrixP Perdrix

          @JonB said in mousePressedEvent handler for QTableView:

          contextMenuEvent()

          That was what was needed!

          Thanks

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @Perdrix
          In addition to being able to subclass and override contextMenuEvent(), there is signal void QWidget::customContextMenuRequested(const QPoint &pos), which does not require subclassing. You choose between event versus signal via setContextMenuPolicy(Qt::ContextMenuPolicy policy) with enum Qt::ContextMenuPolicy.

          1 Reply Last reply
          1
          • PerdrixP Offline
            PerdrixP Offline
            Perdrix
            wrote on last edited by
            #5
            void StackingDlg::on_tableView_customContextMenuRequested(const QPoint& pos)
            

            is what I used - thanks

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt
            • Unsolved