界面

Qt creator 创建鼠标右键菜单  (不新建类)

步骤

  1.  打开你的界面文件并选中你要添加右键的控件,选择“CustomContextMenu”

    Qt creator 创建鼠标右键菜单  (不新建类)
  2. 右键选择“转到槽...” -> customContextMenuRequested
    Qt creator 创建鼠标右键菜单  (不新建类)
  3. 插入下面代码即可
    void MainWindow::on_listWidget_customContextMenuRequested(const QPoint &pos)
    {
        static QMenu *qMenu = NULL;
        if (qMenu == NULL)
        {
            qMenu = new QMenu(ui->listWidget);
            QAction* closePaneAction = new QAction("&Close",this);
            connect(closePaneAction, SIGNAL(triggered()), this, SLOT(close()));
    
            qMenu->addAction(closePaneAction);
        }
    
        qMenu->exec(QCursor::pos()); //在鼠标点击的位置显示鼠标右键菜单
    }
    

      

 

 

 

相关文章:

  • 2021-11-29
  • 2021-11-15
  • 2022-12-23
  • 2021-12-06
  • 2021-10-23
  • 2021-06-24
猜你喜欢
  • 2021-06-17
  • 2022-03-12
  • 2022-12-23
  • 2022-12-23
  • 2021-07-24
相关资源
相似解决方案