Qt treeView实现右键菜单

构造函数中添加

this->setContextMenuPolicy(Qt::CustomContextMenu);                           //设置treeView支持右键弹出菜单
connect(this,SIGNAL(customContextMenuRequested(const QPoint &)),this, SLOT(slotCustomContextMenu(const QPoint &)));     //连接点击右键信号与槽函数

槽函数

void MainWindow::slotCustomContextMenu(const QPoint &point)                     //槽函数定义
{
    if(ui->treeView->hasFocus())	//使鼠标在treeView里右键才弹出菜单
    {
        QMenu *menu;
        QString qss = "QMenu{color:#E8E8E8;background:#4D4D4D;margin:2px;}\
                QMenu::item{padding:3px 20px 3px 20px;}\
                QMenu::indicator{width:13px;height:13px;}\
                QMenu::item:selected{color:#E8E8E8;border:0px solid #575757;background:#1E90FF;}\
                QMenu::separator{height:1px;background:#757575;}";      //设置样式表
                menu = new QMenu(this);
        menu->setStyleSheet(qss);                                         //给菜单设置样式
        QAction *a1 = new QAction(tr("打开"));
        menu->addAction(a1);                                            //给菜单添加项
        connect(a1, SIGNAL(triggered()), this, SLOT(openFile()));       //连接触发信号与槽函数(槽函数实现打开后的操作)
        QAction *a2 = new QAction(tr("移动"));
        menu->addAction(a2);
        QAction *a3 = new QAction(tr("复制"));
        menu->addAction(a3);
        QAction *a4 = new QAction(tr("删除"));
        menu->addAction(a4);
        QAction *a5 = new QAction(tr("分享"));
        menu->addAction(a5);
        QAction *a6 = new QAction(tr("刷新列表"));
        menu->addAction(a6);
        connect(a6, SIGNAL(triggered()), this, SLOT(updataFileList()));   //连接触发信号与槽函数
        menu->exec(this->mapToGlobal(point));
    }
}
  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!对于在Qt中使用TreeView控件,并添加右键菜单的需求,您可以按照以下步骤进行操作: 1. 创建TreeView控件:在Qt中,您可以使用QTreeView类创建一个TreeView控件。例如,在您的窗口类中添加以下代码: ```cpp QTreeView *treeView = new QTreeView(this); ``` 2. 创建右键菜单:使用QMenu类创建一个右键菜单。例如,在您的窗口类的构造函数中添加以下代码: ```cpp QMenu *menu = new QMenu(this); menu->addAction("Action 1"); menu->addAction("Action 2"); // 添加更多的菜单项... ``` 3. 关联右键菜单TreeView控件:通过在TreeView控件的contextMenuPolicy属性中设置Qt::CustomContextMenu,启用自定义上下文菜单。然后,重写TreeView控件的contextMenuEvent()方法,以显示自定义菜单。在您的窗口类中添加以下代码: ```cpp treeView->setContextMenuPolicy(Qt::CustomContextMenu); connect(treeView, &QTreeView::customContextMenuRequested, this, [=](const QPoint &pos) { QContextMenuEvent event(QContextMenuEvent::Mouse, pos); treeView->contextMenuEvent(&event); }); ``` 4. 在contextMenuEvent()方法中显示右键菜单:重写TreeView控件的contextMenuEvent()方法,并在其中显示自定义菜单。在您的窗口类中添加以下代码: ```cpp void YourWindowClass::contextMenuEvent(QContextMenuEvent *event) { QPoint pos = event->pos(); QModelIndex index = treeView->indexAt(pos); if (index.isValid()) { QMenu *menu = new QMenu(this); menu->addAction("Action 1"); menu->addAction("Action 2"); // 添加更多的菜单项... menu->exec(treeView->viewport()->mapToGlobal(pos)); } } ``` 这样,您就可以在TreeView控件上右键单击,并显示自定义的右键菜单了。根据您的需求,您可以根据TreeView中的选定项的位置,添加相应的操作。 希望对您有所帮助!如果有任何进一步的问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值