-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmylabel.cpp
77 lines (69 loc) · 1.96 KB
/
mylabel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "mylabel.h"
#include <QtGui>
MyLabel::MyLabel(QWidget *parent) : QLabel(parent), m_minFontSize(10)
{
//QFont f = font();
//f.setFamily("Arial");
//setFont(f);
//setMargin(3);
setAlignment(Qt::AlignCenter);
}
void MyLabel::resizeEvent(QResizeEvent *event)
{
QLabel::resizeEvent(event);
recalcFontSize();
}
void MyLabel::recalcFontSize()
{
/*QFont aFont=font();
// get widget's height
int wd_hgt = size().height();
// get widget's width
int wd_wdt = size().width();
int fontPointSize=0;
aFont.setPointSize(1);
QString widthTestText = text();
widthTestText.append("www");
// calculating loop
for (int i = 1; i < 200; i++)
{
aFont.setPointSize(i);
// create FontMetrics for resized font
QFontMetrics fm(aFont);
// get text height for current font size
int y = fm.height();
// get text width for current font size
int x = fm.width(widthTestText);
// check if text fits widget
if (y > wd_hgt || x > wd_wdt)
{
// saving maximum possible size of font
fontPointSize = i-1;
break;
}
}
if(fontPointSize>0){
aFont.setPointSize(qMax(fontPointSize, m_minFontSize));
setFont(aFont);
}*/
QFont aFont=font();
//aFont.setPointSize(1);
int fontPointSize=1;
for (int i = 1; i < 200; i++)
{
aFont.setPointSize(i);
QFontMetrics fm(aFont);
QRect boundingRect=fm.boundingRect(this->rect(),
Qt::AlignHCenter | Qt::AlignVCenter | Qt::TextWordWrap,
text());
if(boundingRect.width()>this->rect().width() || boundingRect.height()>this->rect().height()){
break;
}else{
fontPointSize=i;
}
}
if(fontPointSize>0){
aFont.setPointSize(qMax(fontPointSize, m_minFontSize));
setFont(aFont);
}
}