Difference ui->tableWidget->mapFromGlobal(QCursor::pos()); and on_tableWidget_customContextMenuRequested (const QPoint &pos)

  • Hello.
    I have tableWidget and i installed on EventFilter.
    I'm interested in the place where clicked, does there item?

    When I observed these events I noticed such a thing.
    For Example:

    QPoint eventPos = ui->tableWidget->mapFromGlobal(QCursor::pos());
    QPoint contextMenuPos = //What it will be

    To check whether I really had an item or not I had to change my eventPos.

    eventPos.setX(eventPos.x() - 15);
    eventPos.setY(eventPos.y() - 22);

    15 and 22 are derived from contextMenuPos.x () - eventPos.x () and contextMenuPos.y () - eventPos.y () ;

    All of this works well.
    my question: But should I be sure it will always work?

    bool dlgAutomaticSend::eventFilter(QObject *obj, QEvent *event){
        if (obj == ui->tableWidget->viewport()) {
            if (event->type() == QEvent::MouseButtonPress) {
                LastWidget = TemplateWidget;
                QPoint pos = ui->tableWidget->mapFromGlobal(QCursor::pos());
                pos.setX(pos.x() - 15);
                pos.setY(pos.y() - 22);
                auto &&item = ui->tableWidgetTemplate->itemAt(pos);
                ui->btnEdit->setEnabled(!(item) ? false : true);
                ui->btnRemove->setEnabled(!(item) ? false : true);
        return QDialog::eventFilter(obj, event);

  • Lifetime Qt Champion


    The customContextMenuRequested provides the position over the specific widget that emitted it.

  • Qt Champions 2017

    I was wondering where the 22 and 15 comes

    can you try
    QPoint pos = ui->tableWidget->mapFromGlobal(QCursor::pos());
    QPoint pos = ui->tableWidget->viewport()->mapFromGlobal(QCursor::pos());

  • @mrjj said in Difference ui->tableWidget->mapFromGlobal(QCursor::pos()); and on_tableWidget_customContextMenuRequested (const QPoint &pos):

    QPoint pos = ui->tableWidget->viewport()->mapFromGlobal(QCursor::pos());

    its work :) thank.
    when i click on the same point QPoint pos = ui->tableWidget->mapFromGlobal(QCursor::pos()) and customcontextmenu point is different. and difference between them X = customPOS.x - pos.x = 15 and customPOS.y - pos.y = 22.

Log in to reply

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