Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-16638

QTreeWidget::itemAt() returns incorrect item

    XMLWordPrintable

Details

    • Bug
    • Resolution: Invalid
    • Not Evaluated
    • None
    • 4.7.1
    • None
    • MS VS2005 SP1 with hot fixes, Windows XP SP2 with hot fixes.

    Description

      When using QTreeWidget::itemAt() to get the QTreeWidgetItem at a specific coordinate the itemAt() function returns the wrong item.

      I've attached a screenshot which demonstrates the issue.

      The code looks like this:

      bool VagtDispDlg::eventFilter(QObject *obj, QEvent *event)
      {
        if (event->type() == QEvent::ContextMenu && (obj == IDLB_DUTIES || obj == IDLB_EMPLOYEES))
        {
          QContextMenuEvent *cmevent = static_cast<QContextMenuEvent*>(event);
      
          QTreeWidget *tree = qobject_cast<QTreeWidget*>(obj);
      
          if (tree)
          {
      
            QPoint pos = cmevent->pos();
      
            QTreeWidgetItem *twitem = tree->itemAt(pos);
      
            PdcVagtdispVagt *vitem = dynamic_cast<PdcVagtdispVagt*>(twitem);
      
            if (vitem)
            {
              qDebug() << vitem->data(0, Qt::DisplayRole) << pos;
            }
      
          }
        }
      
        return QObject::eventFilter(obj, event);
      }
      

      As you can see from the screenshot I get data for the third item even though the first is highlighted. It seems as if the actual position of the items in regard to the header isn't taken into consideraton, as I get the data for the first item if I right-click just a pixel inside the QTreeWidget (where the header is).

      If this is the correct behaviour then some note should be made in the documentation, along with a way of how to calculate the correct offset for the position of a given item.

      Attachments

        1. itemAt_example.zip
          5 kB
          Marc-Berco Fuhr
        2. QTreeWidget.PNG
          23 kB
          Marc-Berco Fuhr
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            paeglis Gatis Paeglis
            marcbf Marc-Berco Fuhr
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes