Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Function indexAt in QTreeView doesn't work correct.
Forum Updated to NodeBB v4.3 + New Features

Function indexAt in QTreeView doesn't work correct.

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 4.6k Views 1 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.
  • A Offline
    A Offline
    Anticross
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      Anticross
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Sam
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • A Offline
          A Offline
          Anticross
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • S Offline
            S Offline
            Sam
            wrote on last edited by
            #5

            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.

            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 Extensions
            • Unsolved