QTカスタムコンテキストメニュー
-
12-12-2019 - |
質問
ツリービューとWebViewで構成されるQTアプリケーションを作成しています。ツリービューからアイテムをクリックすると、対応するURLがロードされます。うまく機能します。アイテムを右クリックすると、カスタムコンテキストメニューが表示されます.nは新しいWebViewで開きます。これはも機能しています。しかし、私の問題は、TreeViewアイテムを右クリックすると、My Contextメニューが表示され、Pop Upメニューの外側にある場合はその項目の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);
}
}
. 解決
私が知っていることのために、あなたが説明する状況はビューのコンテキストメニューと標準である:右クリックすると、アイテムも選択されます。
別の動作が必要な場合は、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クラスを導き、自分のものを1つ作成する必要があります。
これをずっと前にやった、そして私はこれを出発点として覚えています。私が4つの基本的なマウスイベントすべてを再実装しなければならなかったら、
を守ると、4つの基本的なマウスイベントをすべて再実装しなければならなかった場合は、プレス、リリース、移動、およびダブルクリック。所属していません StackOverflow