qt编程中遇到的bug之error: jump to case label [-fpermissive]

错误信息如下:

C:\Users\q\Desktop\Learn\myitem.cpp:40: error: jump to case label [-fpermissive]

C:\Users\q\Desktop\Learn\myitem.cpp:35: error:   crosses initialization of 'QGraphicsBlurEffect* blurEffect'

这是由对象的生命周期引起的,容易在switch发生此类错误。

switch (event->key())
{
   case Qt::Key_1:
       QGraphicsBlurEffect *blurEffect = new QGraphicsBlurEffect;
       blurEffect->setBlurHints(QGraphicsBlurEffect::QualityHint);
       blurEffect->setBlurRadius(8);
       setGraphicsEffect(blurEffect);
       break;
   case Qt::Key_2:
       QGraphicsColorizeEffect *colorzeEffect = new QGraphicsColorizeEffect;
       colorzeEffect->setColor(Qt::white);
       colorzeEffect->setStrength(0.6);
       setGraphicsEffect(colorzeEffect);
       break;
    case Qt::Key_3:
       QGraphicsDropShadowEffect *drop = new QGraphicsDropShadowEffect;
       drop->setColor(QColor(63,63,63,100));
       drop->setOffset(10);
       setGraphicsEffect(drop);
       break;
}
解决的方法很简单,即为每一个case都加上一个大括号,将case中定义的变量的生命周期限制在大括号内。

猜你喜欢

转载自blog.csdn.net/baidu_38370610/article/details/74874666
今日推荐