PDA

View Full Version : Resize events stop with expanded elided QLabel subclass



Cotlone
30th June 2014, 08:00
Hi everyone,

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




// Header //////////////////////////////////////////////
class ElidedLabel : public QLabel
{
Q_OBJECT
Q_PROPERTY(bool elided READ isElided NOTIFY elisionChanged)
Q_PROPERTY(Qt::TextElideMode elideMode READ getElideMode WRITE setElideMode)

public:
ElidedLabel(QWidget *parent = 0, Qt::WindowFlags f = 0);
ElidedLabel(const QString &text, Qt::TextElideMode mode = Qt::ElideMiddle, QWidget *parent = 0, Qt::WindowFlags f = 0);
const QString & text() const { return content; }
Qt::TextElideMode & getElideMode() {return m_elideMode;}
bool isElided() const { return m_elided; }

protected:
virtual void resizeEvent(QResizeEvent *event);

public slots:
void setText(const QString &text);
void setElideMode(const Qt::TextElideMode elideMode){m_elideMode = elideMode;}
QString elideText(QString text, int w);

signals:
void elisionChanged(bool elided);

private:
bool m_elided;
Qt::TextElideMode m_elideMode;
QString content, content_long;

};

// Class ///////////////////////////////////////////////////

ElidedLabel::ElidedLabel(QWidget *parent, Qt::WindowFlags f)
: QLabel(parent,f)
, m_elided(false)
, m_elideMode(Qt::ElideMiddle)
{

}
ElidedLabel::ElidedLabel(const QString &text, Qt::TextElideMode mode, QWidget *parent, Qt::WindowFlags f)
: QLabel(parent,f)
, m_elided(false)
, m_elideMode(mode)
, content(text)
{
}


void ElidedLabel::resizeEvent(QResizeEvent *event)
{
// FIXME ElidedLabel resize not working properly (not called). On full expansion it will no longer collapse.

QLabel::resizeEvent(event);
content = elideText(content_long, event->size().width());
QLabel::setText(content);
}



void ElidedLabel::setText(const QString &text)
{
content_long = text;
content = elideText(content_long, width());
QLabel::setText(content);
}

QString ElidedLabel::elideText(QString text, int width)
{
QString elidedText = fontMetrics().elidedText(text, m_elideMode, width);
m_elided = elidedText != text;
return elidedText;
}



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

anda_skoa
30th June 2014, 08:24
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,
_

Cotlone
1st July 2014, 02:34
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!




QSize ElidedLabel::minimumSizeHint() const
{
return QSize(QLabel::minimumWidth(), QLabel::minimumHeight());
}

QSize ElidedLabel::sizeHint() const
{
return minimumSizeHint();
}



Cheers,
Cotlone