Offset for QHelpEvent pos() function



  • Why in such code i get the wrong item ?

    @if (event->type() == QEvent::ToolTip) {
    QHelpEvent * helpEvent = static_cast<QHelpEvent *>(event);

    QTableWidgetItem * item = itemAt(helpEvent->pos());

        if (item != NULL)
    

    QToolTip::showText(helpEvent->globalPos(), GET_ADDRESS_BOOK()->getContactName(item->data(Qt::EditRole).toInt()));
    else
    QToolTip::showText(helpEvent->globalPos(), "");
    }

    return QTableWidget::event(event);@

    If I add some offset in size of my vertical end horizontal header, then I have right item. So I modify my code like this:
    @ if (event->type() == QEvent::ToolTip) {
    QHelpEvent * helpEvent = static_cast<QHelpEvent *>(event);

    int y = horizontalHeaderItem(0)->sizeHint().height();
    int x = verticalHeaderItem(0)->sizeHint().width();

    QTableWidgetItem * item = itemAt(helpEvent->pos() - QPoint(x,y));

        if (item != NULL)
    

    QToolTip::showText(helpEvent->globalPos(), GET_ADDRESS_BOOK()->getContactName(item->data(Qt::EditRole).toInt()));
    else
    QToolTip::showText(helpEvent->globalPos(), "");
    }

    return QTableWidget::event(event);@
    Is there any other way to solve this problem ?


  • Moderators

    Have you considered using QTableWidgetItem::setToolTip(...)?



  • You can use mapToGlobal(), mapFromGlobal() or mapTo() to get the right offset.
    Each widget is positioned relativ to its parent.



  • Thanks. mapFromGlobal() is that thing that I searching for.


Log in to reply
 

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