PDA

View Full Version : QLabel with shadow effect issue



jiveaxe
2nd May 2014, 11:29
Hi, I have a problem when using QGraphicsDropShadowEffect onto a QLabel.

I want to draw a shadow around a label's text. The label's content is not static but changes over time.

When I start the application all seems ok:

10332

but starting from the third change a shadow is drawn all around the label:

10334

Curiously, this does not happen when using the same string with QLabel::setText().

This is the code I'm using:


Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);

ui->frame->setBackgroundRole(QPalette::AlternateBase);
ui->frame->setAutoFillBackground(true);

QGraphicsDropShadowEffect *coverShadow = new QGraphicsDropShadowEffect(this);
coverShadow->setBlurRadius(10.0);
coverShadow->setColor(palette().color(QPalette::Shadow));
coverShadow->setOffset(0.0);

ui->label->setWindowFlags(Qt::FramelessWindowHint);
ui->label->setAttribute(Qt::WA_TranslucentBackground);
ui->label->setGraphicsEffect(coverShadow);
}

Any idea about the problem?

Very thanks

Quick update:

Noticed that resizing the dialog make shadow frame disappear. Unfortunately, no luck with QLabel::update(), QLabel::resize(), QLabel::adjustSize()...

ChrisW67
5th May 2014, 05:04
Curiously, this does not happen when using the same string with QLabel::setText().
How are you changing the text on the label if you are not currently calling setText()?

jiveaxe
5th May 2014, 10:56
No, I'm not saying that. I meant that the error is not triggered feeding QLabel::setText() with the same string.

A very ugly hack I found is the following (after setText() ) :


label->setVisible(false);
label->setVisible(true);


This is a minimal but complete code:


#include <QApplication>
#include <QtGui>

class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = 0);
~Dialog() {}

private Q_SLOTS:
void buttonClicked();

private:
QLineEdit *m_lineEdit;
QPushButton *m_pushButton;
QLabel *m_label;
};

Dialog::Dialog(QWidget *parent)
: QDialog(parent)
, m_lineEdit(new QLineEdit(this))
, m_pushButton(new QPushButton(this))
, m_label(new QLabel(this))
{
QGraphicsDropShadowEffect *shadowEffect = new QGraphicsDropShadowEffect(this);
shadowEffect->setBlurRadius(15.0);
shadowEffect->setColor(palette().color(QPalette::Shadow));
shadowEffect->setOffset(0.0);

m_label->setWindowFlags(Qt::FramelessWindowHint);
m_label->setAttribute(Qt::WA_TranslucentBackground);
m_label->setGraphicsEffect(shadowEffect);
m_label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);

QHBoxLayout *layout1 = new QHBoxLayout;
layout1->addWidget(m_lineEdit);
layout1->addWidget(m_pushButton);

QFrame *frame = new QFrame(this);
frame->setMinimumSize(QSize(400, 100));
frame->setAutoFillBackground(true);
frame->setBackgroundRole(QPalette::Base);
frame->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);

QGridLayout *frameLayout = new QGridLayout(frame);
frameLayout->addWidget(m_label);
frameLayout->setContentsMargins(15,15,15,15);

QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addLayout(layout1);
mainLayout->addWidget(frame);

setLayout(mainLayout);

m_lineEdit->setText("I'm a QLabel");
m_pushButton->setText("&Change");
m_label->setText("I'm a QLabel");

connect(m_pushButton, SIGNAL(clicked()), SLOT(buttonClicked()));
}

void Dialog::buttonClicked()
{
if(!m_lineEdit->text().isEmpty()) {
m_label->setText(m_lineEdit->text());
// uncomment the following to "fix" the issue
// m_label->setVisible(false);
// m_label->setVisible(true);
}
}

int main(int argc, char * argv[])
{
QApplication app(argc, argv);

Dialog dialog;
dialog.show();

return app.exec();
}

#include "main.moc"