QGraphicsDropShadowEffect and QResizeEvent



  • Hello,

    I'm using a custom QLabel class that is mainly used to display images/icons and emit Signals/do Stuff with mouseevents.

    My last additon was a shadow effect, using QGraphicsDropShadowEffect when the mouse hovers over the label.

    The problem is, when the effect is applied, it causes a resizeeffent for the whole programm and the min-width is more or less doubled.

    I could stop this with this->blockSignals(true); during the effect creations. But the labels are part of a QPropertyAnimation and as soon as the animation starts the resize is triggered again.

    Is there a way to prevent this? Is QGraphicsDropShadowEffect actually the correct why to archiv my goal - shadow of the label while mouse over - or should I do something else?
    Like, load a different pixmap that has a drawn shadow and swap between those two.

    {
    connect(this, &cLabel::hover, this, &cLabel::shadowEffect);
    }
    //Code excerpt where the effect is applied
    void cLabel::enableShadow(bool enable)
    {
        m_shadowEffect = enable;
        if(enable){
            this->blockSignals(true);
            effect = new QGraphicsDropShadowEffect(this);
            effect->setColor(QColor(110,152,226,255));
            effect->setBlurRadius(10);
            effect->setOffset(5);
            effect->setEnabled(false);
            this->setGraphicsEffect(effect);
            this->blockSignals(false);
        }else{
            effect->deleteLater();
        }
    }
    
    void cLabel::shadowEffect(bool status)
    {
        if(m_shadowEffect){
            if(status)
                effect->setEnabled(true);
            else
                effect->setEnabled(false);
        }
    }
    
    void cLabel::enterEvent(QEvent *event)
    {
        emit hover(true);
        event->ignore();
    }
    
    void cLabel::leaveEvent(QEvent *event)
    {
        emit hover(false);
        
        _Clicked = false;
        reset();
        
        event->ignore();
    }
    


  • I've tried a couple of different things.

    all without success:

    • Applying and handling the DropShadowEffect from the parent widget
    • Disabling the effect before emitting the clicked Signal
    • Delays between signal and execution
    • Blocking all signals before the start of the QPropertyAnimation
    • setting a fixedSize for the MainWindow, user cant change the size, but upon the animations execution the MainWindow gets the new minSize anyway

    But I noticed, that it also happens when I change to a new side of my stackedwidget, but not when I click outside the application.

    I'm now down to a 2nd set of Pixmaps that have a predrawn shadow effect on them. Those I swap on mouse over.

    I'm not happy with it, but it works. If anyone has a better solution please tell me.

    Greetings.



  • Ok,

    I'll mark this as solved.

    I wasn't able to do it with QGraphicsDropShadowEffect, and creating all images twice and including them in the rcs-file is simply a no go.

    Therefore I create a function, that draws a shadow - all 4 sides - around the the QImages, either at startup or during runtime.

    Here it is, in case anyone else needs something like it.

    QImage drawShadow(QImage img)
        {
            //Condition img allready has a transparent border 4 Pixel in this case
    
            //Create new Img with same Size
            QImage imgBGround(img.size(), img.format());
    
            //Fill it with transparency
            QPainter p(&imgBGround);
            QPen pen; pen.setStyle(Qt::NoPen);
            p.setPen(pen);
            p.setRenderHint(QPainter::Antialiasing);
            p.fillRect(QRect(0,0,img.width(), img.height()), QColor(255,255,255,0));
    
            //Draw Rounded Rectangle as Shadow
            QPainterPath path;
            path.addRoundedRect(QRect(0,0,img.width(), img.height()),15,15);
            p.fillPath(path,QColor(110,152,226));
            p.drawPath(path);
    
            //Draw Original Img over background
            p.drawImage(0,0,img);
    
            return imgBGround;
        }
    


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