Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Over which Header Section SIGNAL(customContextMenuRequested(qpoint&)) was generated
Qt 6.11 is out! See what's new in the release blog

Over which Header Section SIGNAL(customContextMenuRequested(qpoint&)) was generated

Scheduled Pinned Locked Moved General and Desktop
2 Posts 1 Posters 1.8k 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.
  • P Offline
    P Offline
    prachi1010
    wrote on last edited by
    #1

    Hi All,

    This is my first post.

    Aim: Display custom menu on right clicking horizontal header of QTableView derived class. This menu will be dynamically populated with custom actionWidgets depending on the Section clicked.

    Problem: Not able to get header section index which was right clicked.

    Slot name:
    @
    void myTableViewClass::OnHorzHeaderRightClicked(const QPoint& _qptPosition)
    {
    QHeaderView * pHeaderView = horizontalHeader();

        //Attempt (1).
        QModelIndex qmiSectionIndex = pHeaderView->indexAt(_qptPosition);// Cannot access protected member.
    
        //Attempt (2). followed: <http://lists.trolltech.com/qt-interest/2004-09/thread01077-0.html>
        for (int i = 0; i < pHeaderView ->count(); ++i)
        {
           QRect rect = pHeaderView ->sectionRect(i); // <sectionRect(i) no longer seems to be supported>
           if (rect.contains(ev->pos()))
                {
                       cout << "context event in section " << i << endl;
                        return;
                }
         }
    
         //Attempt (3).
         pHeaderView ->sectionAt(_qptPosition.x()); // <sectionAt() no longer seems to be supported>
    

    }
    @

    I am new at Qt and am not sure if I'm missing some really obvious thing. Kindly help. Thanks in advance

    1 Reply Last reply
    0
    • P Offline
      P Offline
      prachi1010
      wrote on last edited by
      #2

      Hi All,

      I got the section index using the SectionDoubleClicked slot of Horizontal Header class. I could not proceed further with Right click so I switched to Double click instead.

      I am adding the code for the same, hope it proves to be any help to others new to Qt.

      1. In the form class containing the QTableView add:
        @connect(ui->m_pGridView->horizontalHeader(), SIGNAL(sectionDoubleClicked(int)),
        this, SLOT(OnHorzHeaderSectionDblClicked(int)));@

      2. Implement the Slot as:
        @void CGridView_CM::OnHorzHeaderSectionDblClicked(int _iSectionIndex)
        {
        QVariant qvSectionHeader = m_pGridModel->headerData(_iSectionIndex, Qt::Horizontal);
        //TODO: Get screen coordinates of the double click
        //TODO: Show a custom context menu
        }
        @

      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 Extensions
      • Unsolved