Qt-如果QWidget已经显示,请不要显示

时间:2015-10-28 07:13:01

标签: c++ qt

我想使用通过单击特定按钮切换的动画。这是我的代码:

源:

    void MainWindow::menuClicked()
    {
    //if animation is shown , try to hide it 
        if (m_menuOpend){
            m_animation = new QPropertyAnimation(widgetAnimate, "geometry");
            m_animation->setDuration(200);
            m_animation->setStartValue(QRect(0, 43, 400, 5*windowsHeight/6));
            m_animation->setEndValue(QRect(-300, 43, 400, 5*windowsHeight/6));
            m_animation->setEasingCurve(QEasingCurve::Linear);
            m_animation->start(QPropertyAnimation::DeleteWhenStopped);
            m_menuOpend = false;
        }else{
            //if widget has not created
            if (!widgetAnimate){
               widgetAnimate = new QWidget (this);
            }
        widgetAnimate->show();
        m_animation = new QPropertyAnimation(widgetAnimate, "geometry");
        m_animation->setDuration(200);
        m_animation->setStartValue(QRect(-300, 43, 400, 5*windowsHeight/6));
        m_animation->setEndValue(QRect(0, 43, 400, 5*windowsHeight/6));
        m_animation->setEasingCurve(QEasingCurve::Linear);
        m_animation->start(QPropertyAnimation::DeleteWhenStopped);    
        m_menuOpend = true;
    } 
}

头:

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private:
        QWidget *widgetAnimate;
};

其实我在

上给出了错误
widgetAnimate->show();

因为它跳过了

if (!widgetAnimate) 

每次都是条件。有什么建议可以避免创建多个小部件吗?

0 个答案:

没有答案