Note that there are some explanatory texts on larger screens.

plurals
  1. POQt custom context menu
    text
    copied!<p>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. <strong>It Works Fine</strong>. when am right clicking on the item a custom context menu will appear n it will open it in a new webview. <strong>This is also working</strong>. 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..</p> <p>Here's my coding:</p> <pre><code> QStandardItem *rootItem = new QStandardItem("Google"); QStandardItem *stackItem = new QStandardItem("Stack Overflow"); QStandardItem *yahooItem = new QStandardItem("Yahoo"); rootItem-&gt;appendRow(stackItem); standardModel-&gt;appendRow(rootItem); standardModel-&gt;appendRow(yahooItem); ***// private slot for loading the url if a treeview item is clicked:*** void MainWindow::treeViewClicked(const QModelIndex &amp;index) { str = index.data().toString(); if(!(str.isEmpty()) &amp;&amp; str=="Google") { url = "http://www.google.com"; } else if (!(str.isEmpty()) &amp;&amp; str == "stack Overflow") { url = "http://www.stackoverflow.com"; } else if (!(str.isEmpty()) &amp;&amp; str == "Yahoo") { url = "http://www.yahoo.com"; } WebView *wv = dynamic_cast&lt;WebView *&gt;(ui-&gt;tabWidget-&gt;currentWidget()); wv-&gt;load(QUrl(url)); ui-&gt;tabWidget-&gt;setTabText(ui-&gt;tabWidget-&gt;currentIndex(),str); treeView-&gt;setModel(standardModel); **//Creating custom context menu for QtreeView:** void MainWindow::showContextMenu(const QPoint&amp; point) { QList&lt;QAction *&gt; actions; if(treeView-&gt;indexAt(point).isValid()) { actions.append(m_treeViewAction); } else if(actions.count() &gt; 0) { QMenu::exec(actions, MainWindow::treeView-&gt;mapToGlobal(point)); QModelIndex index = treeView-&gt;indexAt(point); QStandardItem *item = standardModel-&gt;itemFromIndex(index); treeView-&gt;setCurrentIndex(index); treeViewClicked(index); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload