0

In QT:I craete a QMenu:

QMenu* popMenu = new QMenu(ui->treeWidget);

and I want to pop it above the current treewidgetitem, but what I only know is:

popMenu->exec(QCursor::pos());

So if I use:

ui->treeWidget->setCurrentItem(treeWidgetItem);//this is necessary for my program and the current item will be used elsewhere.

The menu will pop at the pos of the mouse-clicked pos not at the item. This looks wizard:( How can I pop the menu at the current item’s coordinates?

Al2O3
  • 3,103
  • 4
  • 26
  • 52

1 Answers1

0

I'm sure my solution is quite stupid, but it works: I measured the root QTreeWidgetItem's height as 15, and child QTreeWidgetItem's height as 12. The code:

bool MainWindow::isAtItemPos()
{
    QPoint treeWidgetPos = ui->treeWidget->mapToGlobal(QPoint(0,0));
    QPoint curPos = QCursor::pos();
    int verPos = curPos.ry() - treeWidgetPos.ry();
    int verMax = 15 + 12*rootItem->childCount();
    if(verPos <= verMax)return true;
    else return false;
}

And I use it:

if(isAtItemPos())popMenu->exec(QCursor::pos());

It works :)

Kate Gregory
  • 18,808
  • 8
  • 56
  • 85
Al2O3
  • 3,103
  • 4
  • 26
  • 52