Results 1 to 3 of 3

Thread: QLabel with shadow effect issue

  1. #1
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QLabel with shadow effect issue

    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:

    shadow.png

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

    shadow2.png

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

    This is the code I'm using:

    Qt Code:
    1. Dialog::Dialog(QWidget *parent) :
    2. QDialog(parent),
    3. ui(new Ui::Dialog)
    4. {
    5. ui->setupUi(this);
    6.  
    7. ui->frame->setBackgroundRole(QPalette::AlternateBase);
    8. ui->frame->setAutoFillBackground(true);
    9.  
    10. QGraphicsDropShadowEffect *coverShadow = new QGraphicsDropShadowEffect(this);
    11. coverShadow->setBlurRadius(10.0);
    12. coverShadow->setColor(palette().color(QPalette::Shadow));
    13. coverShadow->setOffset(0.0);
    14.  
    15. ui->label->setWindowFlags(Qt::FramelessWindowHint);
    16. ui->label->setAttribute(Qt::WA_TranslucentBackground);
    17. ui->label->setGraphicsEffect(coverShadow);
    18. }
    To copy to clipboard, switch view to plain text mode 

    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()...
    Attached Images Attached Images
    Last edited by jiveaxe; 2nd May 2014 at 11:27.
    Giuseppe CalÃ

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QLabel with shadow effect issue

    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()?

  3. #3
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QLabel with shadow effect issue

    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() ) :

    Qt Code:
    1. label->setVisible(false);
    2. label->setVisible(true);
    To copy to clipboard, switch view to plain text mode 


    This is a minimal but complete code:

    Qt Code:
    1. #include <QApplication>
    2. #include <QtGui>
    3.  
    4. class Dialog : public QDialog
    5. {
    6. Q_OBJECT
    7. public:
    8. Dialog(QWidget *parent = 0);
    9. ~Dialog() {}
    10.  
    11. private Q_SLOTS:
    12. void buttonClicked();
    13.  
    14. private:
    15. QLineEdit *m_lineEdit;
    16. QPushButton *m_pushButton;
    17. QLabel *m_label;
    18. };
    19.  
    20. Dialog::Dialog(QWidget *parent)
    21. : QDialog(parent)
    22. , m_lineEdit(new QLineEdit(this))
    23. , m_pushButton(new QPushButton(this))
    24. , m_label(new QLabel(this))
    25. {
    26. QGraphicsDropShadowEffect *shadowEffect = new QGraphicsDropShadowEffect(this);
    27. shadowEffect->setBlurRadius(15.0);
    28. shadowEffect->setColor(palette().color(QPalette::Shadow));
    29. shadowEffect->setOffset(0.0);
    30.  
    31. m_label->setWindowFlags(Qt::FramelessWindowHint);
    32. m_label->setAttribute(Qt::WA_TranslucentBackground);
    33. m_label->setGraphicsEffect(shadowEffect);
    34. m_label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    35.  
    36. QHBoxLayout *layout1 = new QHBoxLayout;
    37. layout1->addWidget(m_lineEdit);
    38. layout1->addWidget(m_pushButton);
    39.  
    40. QFrame *frame = new QFrame(this);
    41. frame->setMinimumSize(QSize(400, 100));
    42. frame->setAutoFillBackground(true);
    43. frame->setBackgroundRole(QPalette::Base);
    44. frame->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    45.  
    46. QGridLayout *frameLayout = new QGridLayout(frame);
    47. frameLayout->addWidget(m_label);
    48. frameLayout->setContentsMargins(15,15,15,15);
    49.  
    50. QVBoxLayout *mainLayout = new QVBoxLayout;
    51. mainLayout->addLayout(layout1);
    52. mainLayout->addWidget(frame);
    53.  
    54. setLayout(mainLayout);
    55.  
    56. m_lineEdit->setText("I'm a QLabel");
    57. m_pushButton->setText("&Change");
    58. m_label->setText("I'm a QLabel");
    59.  
    60. connect(m_pushButton, SIGNAL(clicked()), SLOT(buttonClicked()));
    61. }
    62.  
    63. void Dialog::buttonClicked()
    64. {
    65. if(!m_lineEdit->text().isEmpty()) {
    66. m_label->setText(m_lineEdit->text());
    67. // uncomment the following to "fix" the issue
    68. // m_label->setVisible(false);
    69. // m_label->setVisible(true);
    70. }
    71. }
    72.  
    73. int main(int argc, char * argv[])
    74. {
    75. QApplication app(argc, argv);
    76.  
    77. Dialog dialog;
    78. dialog.show();
    79.  
    80. return app.exec();
    81. }
    82.  
    83. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    Last edited by jiveaxe; 5th May 2014 at 11:00.
    Giuseppe CalÃ

Similar Threads

  1. Replies: 0
    Last Post: 9th August 2012, 03:19
  2. I want QLabel which has text boundary(shadow)
    By xylosper in forum Qt Programming
    Replies: 6
    Last Post: 3rd November 2010, 10:17
  3. QLabel and Mouse pointer Issue
    By newb in forum Qt Programming
    Replies: 3
    Last Post: 24th July 2010, 11:31
  4. QLabel Stylesheet selector issue.
    By Enygma in forum Qt Programming
    Replies: 3
    Last Post: 24th August 2007, 14:52
  5. drawing shadow effect
    By vijay anandh in forum Qt Programming
    Replies: 1
    Last Post: 25th May 2006, 15:41

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.