Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Difference ui->tableWidget->mapFromGlobal(QCursor::pos()); and on_tableWidget_customContextMenuRequested (const QPoint &pos)
Qt 6.12 LTS is out! See what's new in the release blog

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

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 1.5k Views 2 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.
  • Taz742T Offline
    Taz742T Offline
    Taz742
    wrote on last edited by Taz742
    #1

    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);
    
                ui->btnPlus->setEnabled(true);
                ui->btnHistory->setEnabled(false);
            }
        }
    
        return QDialog::eventFilter(obj, event);
    }
    

    Do what you want.

    1 Reply Last reply
    0
    • mrjjM mrjj

      Hi
      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());

      Taz742T Offline
      Taz742T Offline
      Taz742
      wrote on last edited by Taz742
      #4

      @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.

      Do what you want.

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

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

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        2
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #3

          Hi
          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());

          Taz742T 1 Reply Last reply
          1
          • mrjjM mrjj

            Hi
            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());

            Taz742T Offline
            Taz742T Offline
            Taz742
            wrote on last edited by Taz742
            #4

            @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.

            Do what you want.

            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