Qt自定义上下文菜单

Qt自定义上下文菜单,qt,qt-creator,qtreeview,qt-contextmenu,Qt,Qt Creator,Qtreeview,Qt Contextmenu,我正在创建一个由树视图和网络视图组成的Qt应用程序。单击树视图中的项目时,它应加载相应的url工作正常。当我右键单击该项目时,将出现一个自定义上下文菜单,它将在新的Web视图中打开它这也在起作用。但我的问题是,当我右键单击treeview项目时,我的上下文菜单出现,如果我在弹出菜单外单击它,该项目的url就会被加载。如何解决这个问题。。朋友们,帮帮我 这是我的代码: QStandardItem *rootItem = new QStandardItem("Google"

我正在创建一个由树视图和网络视图组成的Qt应用程序。单击树视图中的项目时,它应加载相应的url工作正常。当我右键单击该项目时,将出现一个自定义上下文菜单,它将在新的Web视图中打开它这也在起作用。但我的问题是,当我右键单击treeview项目时,我的上下文菜单出现,如果我在弹出菜单外单击它,该项目的url就会被加载。如何解决这个问题。。朋友们,帮帮我

这是我的代码:

    QStandardItem *rootItem         =  new QStandardItem("Google");
    QStandardItem *stackItem        =  new QStandardItem("Stack Overflow");
    QStandardItem *yahooItem        =  new QStandardItem("Yahoo");

    rootItem->appendRow(stackItem);
    standardModel->appendRow(rootItem);
    standardModel->appendRow(yahooItem);

***// private slot for loading the url if a treeview item is clicked:***

void MainWindow::treeViewClicked(const QModelIndex &index)
{
    str = index.data().toString();

    if(!(str.isEmpty()) && str=="Google")
    {
        url  = "http://www.google.com";
    }

    else if (!(str.isEmpty()) && str == "stack Overflow")
    {
        url = "http://www.stackoverflow.com";
    }

    else if (!(str.isEmpty()) && str == "Yahoo")
    {
        url = "http://www.yahoo.com";
    }

    WebView *wv = dynamic_cast<WebView *>(ui->tabWidget->currentWidget());
    wv->load(QUrl(url));
    ui->tabWidget->setTabText(ui->tabWidget->currentIndex(),str);

    treeView->setModel(standardModel);

**//Creating custom context menu for QtreeView:**

void MainWindow::showContextMenu(const QPoint& point)
{
    QList<QAction *> actions;
    if(treeView->indexAt(point).isValid())
    {
        actions.append(m_treeViewAction);
    }

    else if(actions.count() > 0)
    {
        QMenu::exec(actions, MainWindow::treeView->mapToGlobal(point));
        QModelIndex index = treeView->indexAt(point);
        QStandardItem *item = standardModel->itemFromIndex(index);
        treeView->setCurrentIndex(index);
        treeViewClicked(index);
    }

}
QStandardItem*rootItem=newqstandarditem(“谷歌”);
QStandardItem*stackItem=新的QStandardItem(“堆栈溢出”);
QStandardItem*yahooItem=新的QStandardItem(“雅虎”);
rootItem->appendRow(stackItem);
standardModel->appendRow(根项目);
standardModel->appendRow(yahooItem);
***//用于在单击treeview项目时加载url的专用插槽:***
void主窗口::树视图单击(常量QModelIndex和索引)
{
str=index.data().toString();
如果(!(str.isEmpty())和&str==“谷歌”)
{
url=”http://www.google.com";
}
如果(!(str.isEmpty())和&str==“堆栈溢出”)
{
url=”http://www.stackoverflow.com";
}
如果(!(str.isEmpty())和&str==“Yahoo”)
{
url=”http://www.yahoo.com";
}
WebView*wv=dynamic_cast(ui->tabWidget->currentWidget());
wv->加载(QUrl(url));
ui->tabWidget->setTabText(ui->tabWidget->currentIndex(),str);
treeView->setModel(标准模型);
**//为QtreeView创建自定义关联菜单:**
void主窗口::显示上下文菜单(常量点和点)
{
QList行动;
if(treeView->index(point).isValid())
{
actions.append(m_treevision);
}
else if(actions.count()>0)
{
QMenu::exec(操作,主窗口::treeView->mapToGlobal(点));
QModelIndex index=treeView->index(点);
QStandardItem*item=standardModel->itemFromIndex(索引);
treeView->setCurrentIndex(索引);
树视图单击(索引);
}
}

据我所知,您描述的情况是视图中上下文菜单的标准情况:当您右键单击时,项目也被选中

如果您想要其他行为,则必须实现
mousePressEvent
并实现您想要实现的行为

这里有一个提示:

void MyTreeView::mousePressEvent ( QMouseEvent * event )
{
     if (event->button() == Qt::LeftButton) {
       // set the current item based on event->pos() / deselect if no item
     }
     else if (event->button() == Qt::RightButton) {
       // show context menu for the item / different context menu if no item
     }
}
是的,您必须派生QTreeView类并创建自己的类

我很久以前就这样做了,我记得这是一个起点。我不记得现在我是否必须重新实现所有四个基本的鼠标事件:按下、释放、移动和双击,因为它们是内部相关的