QTreeWidget right click menu

后端 未结 4 1754
离开以前
离开以前 2021-02-03 11:31

I looked around and it seems that the problem is present not only for tree widget but also for other widgets. But in my case, I found a solution, although an incomplete one. I a

4条回答
  •  独厮守ぢ
    2021-02-03 12:12

    First,config QTreeWidget to response(emit signal) right mouse click:

    treeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
    

    Second,connect the signal with your slot "MainWindow::prepareMenu":

    connect(treeWidget,&QTreeWidget::customContextMenuRequested,this,&MainWindow::prepareMenu);
    

    Third,create context menu in the slot:

    void MainWindow::prepareMenu( const QPoint & pos )
    {
    QTreeWidget *tree = treeWid;
    
    QTreeWidgetItem *nd = tree->itemAt( pos );
    
    qDebug()<text(0);
    
    
    QAction *newAct = new QAction(QIcon(":/Resource/warning32.ico"), tr("&New"), this);
    newAct->setStatusTip(tr("new sth"));
    connect(newAct, SIGNAL(triggered()), this, SLOT(newDev()));
    
    
    QMenu menu(this);
    menu.addAction(newAct);
    
    QPoint pt(pos);
    menu.exec( tree->mapToGlobal(pos) );
    }
    

提交回复
热议问题