显示/隐藏QMenu

时间:2013-10-21 12:15:21

标签: c++ qt qt5 qmenu

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

问题:
展示&当我明确地调用它们时(单击开始按钮),hide工作正常。但是,当我点击开始菜单外面时,它会立即隐藏。请告诉我这背后可能是什么原因:

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();
}

1 个答案:

答案 0 :(得分:1)

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

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