在QTreeWidget中使得QTreeWidgetItem接受右键出现菜单,部分代码

在QTreeWidget中使得QTreeWidgetItem接受右键出现菜单,部分代码

#include
#include
#include "tree.h"

tree::tree(QWidget *parent, Qt::WFlags flags)
: QDialog(parent, flags)
{
ui.setupUi(this);
connect(ui.treeWidget,SIGNAL(itemPressed(QTreeWidgetItem *, int)),
   this,SLOT(SlotItemClicked(QTreeWidgetItem *, int)));
}

tree::~tree()
{

}

void tree::SlotItemClicked(QTreeWidgetItem *item, int column)
{
if (qApp->mouseButtons() == Qt::LeftButton)
{
   return;
}

if (qApp->mouseButtons() == Qt::RightButton)
{
   if (item->childCount() > 0)
   {
    return;
   }
   showMenu();
}

qDebug("run!");
}

void tree::showMenu()
{
QPoint pos;
QMenu menu(ui.treeWidget);
menu.addAction(tr("测试"));
// menu.exec(ui.treeWidget->mapToGlobal(pos));
menu.exec(QCursor::pos());
}

你可能感兴趣的:(Qt)