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



  • 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



  • 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
      }
      @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.