G'Day everyone,
I've made a subclass of the QLabel widget which automatically adjusts it's font size depending on the amount of space in it's frame. It expands quite well, and in a single pass can adjust to the correct size. When quickly minimising (ie using it upper right window buttons) to a smaller size, the widget visibly re-adjusts incrementally. I understand why this is happening based on my code. The widget cannot resize smaller than the contents so it much go as small as it can, then the content adjusts, and so on.
My question, how can I over come this 'slow' adjustment?
I thought of having the content take a small value when the frame is decreased in size, and then in a second pass could grow to the correct size (after the frame has adjusted). This still seems like a poor approach to me though. I am sure some of you guys must know how this should be handled.
Here is the code for my subclassed QLabel:
{
}
{
// new font size
fontSizeRatio = 0.8;
qreal frameOffset = (1-fontSizeRatio)*0.75;
qreal textHeight = fontMetrics().height();
// (if text height is a bit small for the frame height) or (if text is height is a bit too big for frame height)
if(((1+frameOffset)*textHeight < height()) | (textHeight > (1-frameOffset)*height())){
int newFontSize = -1;
// if the text width ditates a greater height than the label, use that height, otherwise use the label height
if(heightForWidth(fontMetrics().width(text())) > height())
newFontSize = heightForWidth(fontMetrics().width(text()));
else
newFontSize = height();
newFontSize *= fontSizeRatio;
newFont.setPixelSize(newFontSize);
setFont(newFont);
}
}
MyLabel::MyLabel(QWidget *parent):
QLabel(parent)
{
}
void MyLabel::resizeEvent(QResizeEvent *event)
{
// new font size
fontSizeRatio = 0.8;
qreal frameOffset = (1-fontSizeRatio)*0.75;
qreal textHeight = fontMetrics().height();
// (if text height is a bit small for the frame height) or (if text is height is a bit too big for frame height)
if(((1+frameOffset)*textHeight < height()) | (textHeight > (1-frameOffset)*height())){
int newFontSize = -1;
// if the text width ditates a greater height than the label, use that height, otherwise use the label height
if(heightForWidth(fontMetrics().width(text())) > height())
newFontSize = heightForWidth(fontMetrics().width(text()));
else
newFontSize = height();
newFontSize *= fontSizeRatio;
QFont newFont = font();
newFont.setPixelSize(newFontSize);
setFont(newFont);
}
}
To copy to clipboard, switch view to plain text mode
Thanks very much for your help, I really appreciate it 
- Cotlone
Bookmarks