默认按钮菜单是出现在按钮下方的,现在要实现下面效果 菜单出现在按钮的中央

    menu = new QMenu(this);
    menu->setCursor(Qt::PointingHandCursor);
    menu->setMinimumSize(180,60);
    menu->installEventFilter(this);//此部件在事件过滤器中处理

    no_scan_list = new QAction(QIcon(":/img/more_search.png"), tr("测试"), this);
    menu->addAction(no_scan_list);
    ui->pushButton_more_process->setMenu(menu);

//事件过滤器

bool ceshi::eventFilter(QObject * obj, QEvent *event)
{
    if (event->type() == QEvent::Show && obj == menu)
    {
        QPoint p = ui->pushButton_more_process->mapToGlobal(QPoint(0, 0));//相对应屏幕左上角的坐标
        int cha_y = menu->size().height() - ui->pushButton_more_process->height() * 0.5;
        int cha_x = menu->size().width() - ui->pushButton_more_process->width() * 0.5;
        menu->move(QPoint(p.x() - cha_x, p.y() - cha_y));
        return true;
    }
    return false;
}

 

Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐