문제

트리보기와 WebView로 구성된 Qt 응용 프로그램을 만드는 것입니다.트리보기에서 항목을 클릭하면 해당 URL을로드해야합니다. 은 잘 작동합니다.항목을 마우스 오른쪽 버튼으로 클릭하면 사용자 정의 컨텍스트 메뉴가 나타납니다. n이 새 WebView에서 열립니다. 이것은 또한 을 일하고 있습니다.그러나 내 문제는 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);
    }

}
.

도움이 되었습니까?

해결책

내가 아는 것의 상황은 컨텍스트 메뉴가있는 표준이며, 마우스 오른쪽 버튼을 클릭하면 항목도 선택됩니다.

다른 동작을 원한다면 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 클래스를 파생시키고 자신의 것을 만들어야합니다.

나는 오래 전에 이것을 끝냈고, 나는 이것을 출발점으로 기억한다.나는 네 가지 기본 마우스 이벤트를 모두 구출 해야하는 경우 이제는 기억이 안요. 내부적으로 관련 될 때 누르고, 릴리스, 이동 및 더블렉트를하십시오.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top