Important: Please read the Qt Code of Conduct -

CustomContext menu is not displaying in proper position

  • The context menu in program is not displaying at the position of the is displaying somewhere else...
    Please suggest me what changes i need to do for my program?

    The code is:
    void MainWindow::ProvideContextMenu(const QPoint &pos)
    QTableWidgetItem *item=filesTable->itemAt(pos);

    QString cell=item->text();
            cout<<"EVENT GENERATED..."<<cell.toStdString()<<endl;
    //QString item=filesTable->itemAt(pos);
    QAction *pAddAction= new QAction("add",filesTable);
    QAction *pSplitAction= new QAction("SPLIT",filesTable);
    QMenu *pContextMenu = new QMenu( this);


  • Can you please also post the code showing how you get the position you're using as the functions parameter ? Is it from the event->pos() retrieved in a mousePressEvent(QMouseEvent event) function or from QCursor::pos()... ?

  • @ connect(filesTable,SIGNAL(customContextMenuRequested(const QPoint &)),this,SLOT(ProvideContextMenu(const QPoint &)));
    this is the code which generates the signal

  • Hmm. Have you tried move your ProvideContextMenu(const QPoint &pos) function to the filesTable class implementation. This way you would not have to map the position.

    It's just a guess but maybe your problem is related to this exception stated in "the doc of customContextMenuRequested(..)":

    bq. The position pos is the position of the context menu event that the widget receives. Normally this is in widget coordinates. The exception to this rule is QAbstractScrollArea and its subclasses that map the context menu event to coordinates of the viewport().

    Since QAbstractScrollArea is inherited by QAbstractItemView, QGraphicsView, QMdiArea, QPlainTextEdit, QScrollArea, and QTextEdit

  • hmmmm...:(

  • There are mapping methods available in every QWidget instance...

Log in to reply