Results 1 to 2 of 2

Thread: Crash using custom ElidedLabel class in Qt5

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

    Default Crash using custom ElidedLabel class in Qt5

    Hi, I'm porting my application to Qt5. In the project I'm using a custom QLabel providing text elision (http://gedgedev.blogspot.it/2010/12/...els-in-qt.html). While in Qt4 it works fine, in Qt5 is causing a crash:

    http://paste.opensuse.org/91142495

    This is the code of the class:

    ElidedLabel.h
    Qt Code:
    1. class ElidedLabel : public QLabel
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. explicit ElidedLabel(QWidget* parent = 0, Qt::WindowFlags f = 0);
    7. explicit ElidedLabel(const QString& text, QWidget* parent = 0, Qt::WindowFlags f = 0);
    8. explicit ElidedLabel(const QString &txt, Qt::TextElideMode elideMode = Qt::ElideRight, QWidget * parent = 0, Qt::WindowFlags f = 0);
    9.  
    10. //! Set the elide mode used for displaying text.
    11. void setElideMode(Qt::TextElideMode elideMode) {
    12. elideMode_ = elideMode;
    13. updateGeometry();
    14. }
    15.  
    16. //! Get the elide mode currently used to display text.
    17. Qt::TextElideMode elideMode() const { return elideMode_; }
    18.  
    19. // QLabel overrides
    20. void setText(const QString &);
    21.  
    22. protected:
    23. virtual void paintEvent(QPaintEvent* );
    24. virtual void resizeEvent(QResizeEvent* );
    25.  
    26. //! Cache the elided text so as to not recompute it every paint event
    27. void cacheElidedText(int w);
    28.  
    29. private:
    30. Qt::TextElideMode elideMode_;
    31. QString cachedElidedText_;
    32. };
    To copy to clipboard, switch view to plain text mode 

    ElidedLabel.cpp
    Qt Code:
    1. ElidedLabel::ElidedLabel(QWidget* parent, Qt::WindowFlags f):
    2. QLabel(parent, f),
    3. elideMode_(Qt::ElideRight)
    4. {
    5. setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred);
    6. }
    7.  
    8. ElidedLabel::ElidedLabel(const QString& text, QWidget* parent, Qt::WindowFlags f):
    9. QLabel(text, parent, f),
    10. elideMode_(Qt::ElideRight)
    11. {
    12. setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred);
    13. }
    14.  
    15. ElidedLabel::ElidedLabel(const QString& text, Qt::TextElideMode elideMode, QWidget* parent, Qt::WindowFlags f) :
    16. QLabel(text, parent, f),
    17. elideMode_(elideMode)
    18. {
    19. setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred);
    20. }
    21.  
    22. void ElidedLabel::setText(const QString& text)
    23. {
    24. QLabel::setText(text);
    25. cacheElidedText(geometry().width());
    26. setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred);
    27. }
    28.  
    29. void ElidedLabel::cacheElidedText(int w)
    30. {
    31. cachedElidedText_ = fontMetrics().elidedText(text(), elideMode_, w, Qt::TextShowMnemonic);
    32. }
    33.  
    34. void ElidedLabel::paintEvent(QPaintEvent* e)
    35. {
    36. if(elideMode_ == Qt::ElideNone) {
    37. QLabel::paintEvent(e);
    38. } else {
    39. QPainter p(this);
    40. p.drawText(0, 0,
    41. geometry().width(),
    42. geometry().height(),
    43. alignment(),
    44. cachedElidedText_);
    45. }
    46. }
    47.  
    48. void ElidedLabel::resizeEvent(QResizeEvent* e)
    49. {
    50. QLabel::resizeEvent(e);
    51. cacheElidedText(e->size().width());
    52. }
    To copy to clipboard, switch view to plain text mode 

    Demoting widgets to plain QLabel solves the issue. I dunno if it is a bug in Qt5 or anything changed in the api, though the project compiles without warnings.

    Any idea?

    Very thanks.
    Giuseppe CalÃ

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Crash using custom ElidedLabel class in Qt5

    The code looks OK as far as I can tell, i.e. I see no reason why this wouldn't also work on Qt5.

    The crash seems to be in QPainter's constructor, which is very strange.
    Often that means that the problem is elsewhere, e.g. that the crash is a result of memory corruption, etc.

    You could try to install the debug symbols for Qt5 and see if the backtrace gets more interesting details.

    Btw, just to make sure: you are aware that QLabel::setText() is not virtual, right?

    Cheers,
    _

Similar Threads

  1. Replies: 0
    Last Post: 20th February 2012, 05:39
  2. Replies: 7
    Last Post: 18th August 2011, 14:43
  3. Replies: 2
    Last Post: 1st February 2011, 21:52
  4. Qt Creator Crash due to custom plugin?
    By Lykurg in forum Qt Tools
    Replies: 3
    Last Post: 1st February 2011, 15:17
  5. Deleting Class causes app crash
    By been_1990 in forum Qt Programming
    Replies: 8
    Last Post: 29th January 2010, 17:52

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.