Question

Am creating a Qt Application which consists of a tree view and a webview. when a item from a tree view is clicked it should load the corresponding url. It Works Fine. when am right clicking on the item a custom context menu will appear n it will open it in a new webview. This is also working. But my problem is when am Right clicking on the treeview item my context menu comes and if am clicking it outside the pop up menu the url of that item gets loaded. how to solve this.. Help me friends..

Here's my coding:

    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);
    }

}
Was it helpful?

Solution

For what I know, the situation you describe is standard with context menus in views: When you right click, the item is also selected.

If you want another behaviour, you must implement the mousePressEvent and implement the behaviour you want to achieve.

Here is a hint:

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
     }
}

Yes, you must derive the QTreeView class and make one of your own.

I've done this long time ago, and I remember this as the starting point. I don't remember now if I had to reimplement all four basic mouse events: press, release, move and doubleclick, as they are internally related.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top