QGraphicsDropShadowEffect on form widget



  • Greetings all. I'm having trouble showing a drop shadow on my form widgets. A quick test with a QLabel poses no problem; the shadow shows fine.

    I should mention that the widgets are laid out with my own layout manager - also that I am applying the shadow as I am adding the widgets to the layout. I tried applying shadow before and after adding to layout; no difference.

    Is there anything I should be beware of in order to make this work? Or does shadows only work with "simple" widgets?



  • Ok, it works if adding the shadow in the form widget constructor:

    ui->setupUi(this);
    QGraphicsDropShadowEffect* dropShadowEffect = new QGraphicsDropShadowEffect(this);
    this->setGraphicsEffect(dropShadowEffect);

    But, now all sub widgets inherits the effect. Calling QWidget::setGraphicsEffect(NULL) for subitems does not change anything.

    Suggestions?


  • Lifetime Qt Champion

    Hi,

    Did you try:

    @dropShadowEffect->setEnabled(false);@

    ?



  • Um, I'm not sure how that would work. I'm having one instance of the effect, which is applied only on the top level widget. Disabling it would sort of defeat the purpose.


  • Moderators

    You can explicitly set a background color on the top level widget. This will force the drop shadow effect to treat it as a opaque rectangle (or whatever the shape is) and apply the effect around it leaving children unchanged.



  • I suppose you mean with a stylesheet? Just did this on the top level widget; did not stop the effect from propagating to the children:

    #MediaItemForm {
    background-color:#FFFFFF;
    }


  • Lifetime Qt Champion

    Sorry, I misread your post.

    From the documentation of QWidget::setGraphicsEffect:

    bq. Note: This function will apply the effect on itself and all its children.



  • SGaist: Yes, but how to avoid it? It would be a terrible solution to subclass all child elements to ignore the effect.


  • Moderators

    Hm, that's weird. This is what I'm getting with and without the background:
    !http://img46.imageshack.us/img46/9097/ifgk.jpg(drop shadow example)!


  • Moderators

    [quote author="janfaroe" date="1383368356"]SGaist: Yes, but how to avoid it? It would be a terrible solution to subclass all child elements to ignore the effect.[/quote]

    you can try a method which sets the effect for you (untested):
    @
    setDropShadowEffectOnWidgetOnly(QWidget* w)
    {
    if( ! w ) return;

    w->setGraphicsEffect( new QGraphicsDropShadowEffect );
    foreach( QWidget* child, this->findChildren<QWidget*>() )
         child->setGraphicsEffect(0);   //unset effect for children
    

    }
    @



  • Chris Kawa: That's odd indeed. And you set bg color with style sheet? Could it be one of these things that despite all are platform dependent? Or because I'm using 5.2 beta?

    [quote author="Chris Kawa" date="1383555753"]Hm, that's weird. This is what I'm getting with and without the background:
    !http://img46.imageshack.us/img46/9097/ifgk.jpg(drop shadow example)![/quote]



  • Thanks for the suggestion. However, since I'm rendering quite a few parent widgets, it does not seem terribly efficient? But of course, if anything else fails, I might resort to it.

    [quote author="raven-worx" date="1383557940"][quote author="janfaroe" date="1383368356"]SGaist: Yes, but how to avoid it? It would be a terrible solution to subclass all child elements to ignore the effect.[/quote]

    you can try a method which sets the effect for you (untested):
    @
    setDropShadowEffectOnWidgetOnly(QWidget* w)
    {
    if( ! w ) return;

    w->setGraphicsEffect( new QGraphicsDropShadowEffect );
    foreach( QWidget* child, this->findChildren<QWidget*>() )
         child->setGraphicsEffect(0);   //unset effect for children
    

    }
    @[/quote]


  • Moderators

    [quote author="janfaroe" date="1383558917"]Thanks for the suggestion. However, since I'm rendering quite a few parent widgets, it does not seem terribly efficient?
    [/quote]
    yes most probably.


  • Moderators

    [quote author="janfaroe" date="1383558759"]Chris Kawa: That's odd indeed. And you set bg color with style sheet? Could it be one of these things that despite all are platform dependent? Or because I'm using 5.2 beta?
    [/quote]

    I'm using Qt5.1.1 with both VS2012 and MinGW. This is basically all the code I wrote:
    @
    ui->frame->setStyleSheet("QFrame { background-color: palette(window)}");
    ui->frame->setGraphicsEffect(new QGraphicsDropShadowEffect(this));
    ui->frame_2->setGraphicsEffect(new QGraphicsDropShadowEffect(this));
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.