Results 1 to 3 of 3

Thread: Resize events stop with expanded elided QLabel subclass

  1. #1
    Join Date
    Apr 2010
    Posts
    27
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Resize events stop with expanded elided QLabel subclass

    Hi everyone,

    I've created a subclass of the QLabel to use elided text (http://doc.qt.io/qt-4.8/qfontmetrics.html#elidedText) for long file paths. I did this using the following:


    Qt Code:
    1. // Header //////////////////////////////////////////////
    2. class ElidedLabel : public QLabel
    3. {
    4. Q_OBJECT
    5. Q_PROPERTY(bool elided READ isElided NOTIFY elisionChanged)
    6. Q_PROPERTY(Qt::TextElideMode elideMode READ getElideMode WRITE setElideMode)
    7.  
    8. public:
    9. ElidedLabel(QWidget *parent = 0, Qt::WindowFlags f = 0);
    10. ElidedLabel(const QString &text, Qt::TextElideMode mode = Qt::ElideMiddle, QWidget *parent = 0, Qt::WindowFlags f = 0);
    11. const QString & text() const { return content; }
    12. Qt::TextElideMode & getElideMode() {return m_elideMode;}
    13. bool isElided() const { return m_elided; }
    14.  
    15. protected:
    16. virtual void resizeEvent(QResizeEvent *event);
    17.  
    18. public slots:
    19. void setText(const QString &text);
    20. void setElideMode(const Qt::TextElideMode elideMode){m_elideMode = elideMode;}
    21. QString elideText(QString text, int w);
    22.  
    23. signals:
    24. void elisionChanged(bool elided);
    25.  
    26. private:
    27. bool m_elided;
    28. Qt::TextElideMode m_elideMode;
    29. QString content, content_long;
    30.  
    31. };
    32.  
    33. // Class ///////////////////////////////////////////////////
    34.  
    35. ElidedLabel::ElidedLabel(QWidget *parent, Qt::WindowFlags f)
    36. : QLabel(parent,f)
    37. , m_elided(false)
    38. , m_elideMode(Qt::ElideMiddle)
    39. {
    40.  
    41. }
    42. ElidedLabel::ElidedLabel(const QString &text, Qt::TextElideMode mode, QWidget *parent, Qt::WindowFlags f)
    43. : QLabel(parent,f)
    44. , m_elided(false)
    45. , m_elideMode(mode)
    46. , content(text)
    47. {
    48. }
    49.  
    50.  
    51. void ElidedLabel::resizeEvent(QResizeEvent *event)
    52. {
    53. // FIXME ElidedLabel resize not working properly (not called). On full expansion it will no longer collapse.
    54.  
    55. QLabel::resizeEvent(event);
    56. content = elideText(content_long, event->size().width());
    57. QLabel::setText(content);
    58. }
    59.  
    60.  
    61.  
    62. void ElidedLabel::setText(const QString &text)
    63. {
    64. content_long = text;
    65. content = elideText(content_long, width());
    66. QLabel::setText(content);
    67. }
    68.  
    69. QString ElidedLabel::elideText(QString text, int width)
    70. {
    71. QString elidedText = fontMetrics().elidedText(text, m_elideMode, width);
    72. m_elided = elidedText != text;
    73. return elidedText;
    74. }
    To copy to clipboard, switch view to plain text mode 

    I've got it placed inside a various layouts and widgets using the GUI designer. When I load a file it displays the elided text fine. When I expand and shrink the window it updates appropriately, as long as it is not fully expanded ( no longer elided ). At this point the full text string is displayed, however the widget will no longer receive resize events*. It basically locks out. I think the events are being eaten up the line somewhere, but I am not really sure how to allow them to be received by the ElidedLabel subclass. When I attempt to shrink the window back it actually pushes the whole window across the screen instead (up to the equivalent width of the expansion that was just made).

    Has anybody got any advice on how I should approach this problem?
    Thanks very much in advance for any help!

    Cheers,
    Cotlone
    Last edited by Cotlone; 30th June 2014 at 09:10.

  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: Resize events stop with expanded elided QLabel subclass

    My guess is that you have to override minimumSizeHint() and/or sizeHint() to make the layout aware of your different resizing constrains.
    The base implementation in QLabel doesn't know about your eliding capability.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    Cotlone (1st July 2014)

  4. #3
    Join Date
    Apr 2010
    Posts
    27
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Resize events stop with expanded elided QLabel subclass

    Thanks anda_skoa, I appreciate your fast help!
    That certainly did the trick, I needed to re-implement both of them ( shown below for completeness ). I should have tried that, now I will know for next time!

    Qt Code:
    1. QSize ElidedLabel::minimumSizeHint() const
    2. {
    3. return QSize(QLabel::minimumWidth(), QLabel::minimumHeight());
    4. }
    5.  
    6. QSize ElidedLabel::sizeHint() const
    7. {
    8. return minimumSizeHint();
    9. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    Cotlone

Similar Threads

  1. Subclass QLabel
    By Globulus in forum Newbie
    Replies: 5
    Last Post: 29th July 2013, 22:30
  2. QCalendarWidget - how to stop wheel events processing?
    By googie in forum Qt Programming
    Replies: 2
    Last Post: 3rd July 2013, 23:06
  3. subclass the QLabel
    By ready in forum General Programming
    Replies: 29
    Last Post: 26th March 2011, 12:08
  4. Handling mouse events in QGraphicsWidget subclass
    By Asperamanca in forum Qt Programming
    Replies: 1
    Last Post: 27th October 2010, 17:00
  5. Posting custom events to a subclass of QThread
    By jpn in forum Qt Programming
    Replies: 3
    Last Post: 4th July 2006, 16:49

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.