显示/隐藏 QMenu

Show/Hide of QMenu

本文关键字:QMenu 隐藏 显示      更新时间:2023-10-16

我通过继承QMenu创建了"开始"菜单。我想使用滑动样式的QPropertyAnimation显示和隐藏它

问题:
当我显式调用它们时,显示和隐藏工作正常(单击开始按钮)。但是当我在开始菜单之外单击时,它会立即隐藏。请告诉我这背后可能是什么原因:

My class is StartMenuUiClass which inherited from QMenu
mptrobj_animation is QPropertyAnimation object
void StartMenuUiClass::show()
{
    this->raise();
    disconnect(mptrobj_animation,SIGNAL(finished()),this,SLOT(this_hide()));
    QMenu::show();
    mptrobj_animation->setDuration(500);
    mptrobj_animation->setStartValue(*mptrobj_startPosition);
    mptrobj_animation->setEndValue(*mptrobj_endPosition);
    mptrobj_animation->start();
}
void StartMenuUiClass::hide()
{
    mptrobj_animation->setDuration(450);
    mptrobj_animation->setStartValue(*mptrobj_endPosition);
    mptrobj_animation->setEndValue(*mptrobj_startPosition);
    connect(mptrobj_animation,SIGNAL(finished()),this,SLOT(this_hide()));
    mptrobj_animation->start();
}
void StartMenuUiClass::this_hide()
{
    this->lower();
    emit work_Done();
    QMenu::hide();
}
我认为

,如果您在菜单小部件外部单击,它只会隐藏或关闭,而不涉及您的StartMenuUiClass::hide()功能。您可以尝试处理QMenu::hideEvent(QHideEvent *event)和/或QWidget::closeEvent(QCloseEvent *event)。像这样:

StartMenuUiClass::closeEvent(QCloseEvent *event) // the same for hideEvent()
{
    this->hide();
    event->accept();
}