Function indexAt in QTreeView doesn't work correct.



  • I've got same QWidget based class which contains some QTreeView in it's *.ui file. The model class for view is based on QAbstractItemModel. I install event filter to handle contextMenu event of QTreeView.

    @m_ui.treeView_Network->installEventFilter(this);@

    Then I implement it:
    @bool TargetsWidget::eventFilter( QObject * object, QEvent * event ) {

    if (event->type() == QEvent::ContextMenu) {
    QContextMenuEvent * contextEvent = static_cast<QContextMenuEvent *>(event);

    QMenu menu(m_ui.treeView_Network);

    QAction * actionUpdate = new QAction(object);
    actionUpdate-> setText(tr("Update"));

    menu.addAction(actionUpdate);

    //connect(actionUpdate, SIGNAL(triggered()), this, SLOT(onExpandAll()));
    QModelIndex ndx = m_ui.treeView_Network->indexAt(contextEvent->pos());

    if (ndx.isValid())
    menu.exec(contextEvent->globalPos());
    else return false;

    return true;
    } else {
    // standard event processing
    return QObject::eventFilter(object, event);
    }
    }@

    But @QModelIndex ndx = m_ui.treeView_Network->indexAt(contextEvent->pos());@ doesn’t always return true index. If I have for example one item in tree with 3 children and click on header(0 column) indexAt row is 0 and column is 0. But when I click on last child of item it returns(1,-1) and QModelIndex::isValid() returns false, but it’s wrong there is item there. I checked out @contextEvent>pos()@ it returns right coordinates in coordinate system of QTreeView.



  • I found that QML has the same bug in itemAt function, and it's not already solved. So maybe this bug influence on all qt, not only QML. I'm using MSVS 2010 wit qt version 4.8.4.



  • Check with "mapToParent()":http://qt-project.org/doc/qt-4.8/qwidget.html#mapToParent / "mapFromParent()":http://qt-project.org/doc/qt-4.8/qwidget.html#mapFromParent and other mapTo functions.

    eg

    @QModelIndex ndx = m_ui.treeView_Network->indexAt(mapToParent(contextEvent->pos()));@

    or

    @QModelIndex ndx = m_ui.treeView_Network->indexAt(mapFromParent(contextEvent->pos()));@

    Written brain to terminal not tested



  • I tried to do this. But have no effect. When click on header ndx is valid, when click on last item it's invalid.



  • It will be helpful if you can share some more code so that we can reproduce the same effect. A small test example is sufficient.


Log in to reply
 

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