[Qt 4.8] How to use QGraphicsDropShadowEffect while defining a QStyle ?

  • Hello,

    I am redifining a customized Style for my desktop application in which I would like to draw buttons with shadow effects.
    To do so, I tried to overload the drawPrimitive method in my class which is an extension of QMotifStyle (see the following code).

    @void MyStyle_c::drawPrimitive(PrimitiveElement element,
    const QStyleOption *option,
    QPainter *painter,
    const QWidget *widget) const

      int x, y, width, height;
     option->rect.getRect(&x, &y, &width, &height);
     switch (element) {

    case PE_PanelButtonCommand:
    painter->setRenderHint(QPainter::Antialiasing, true);
    QGraphicsRectItem * item = new QGraphicsRectItem(x,y,width,height);

            QGraphicsDropShadowEffect * effect = new QGraphicsDropShadowEffect ;
             item->setGraphicsEffect(effect); /* NO EFFECT !*/ 


         QMotifStyle::drawPrimitive(element, option, painter, widget);


    It's drawn properly but without any effect. Could you please tell me why ? What am I doing wrong ?

    Thanks you a lot

  • Moderators

    that's not how it works. You set the graphics effect on the item but in the next step you are drawing only the shape (QPainterPath)...
    You should rather set it outside of the style where you actually use the item/widget. The effect is meant to be used as an addition to the painting of the item.

    If you want to do it in the style you would have to do it yourself with QPainter's methods and gradients for example. But this may be tricky since you may get performance issues if you don't optimize the code.

Log in to reply

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