Introduce TextoutputLabel: Resizes automatically with text
此提交包含在:
		@@ -12,6 +12,7 @@ HEADERS += calculationengine.h \
 | 
			
		||||
           entrypushbutton.h \
 | 
			
		||||
           settingsprovider.h \
 | 
			
		||||
           singleinstanceserver.h \
 | 
			
		||||
           textoutputlabel.h \
 | 
			
		||||
           window.h
 | 
			
		||||
SOURCES += calculationengine.cpp \
 | 
			
		||||
           entryprovider.cpp \
 | 
			
		||||
@@ -19,6 +20,7 @@ SOURCES += calculationengine.cpp \
 | 
			
		||||
           main.cpp \
 | 
			
		||||
           settingsprovider.cpp \
 | 
			
		||||
           singleinstanceserver.cpp \
 | 
			
		||||
           textoutputlabel.cpp \
 | 
			
		||||
           window.cpp
 | 
			
		||||
QT += widgets sql network
 | 
			
		||||
QT_CONFIG -= no-pkg-config
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										37
									
								
								textoutputlabel.cpp
									
									
									
									
									
										一般檔案
									
								
							
							
						
						
									
										37
									
								
								textoutputlabel.cpp
									
									
									
									
									
										一般檔案
									
								
							@@ -0,0 +1,37 @@
 | 
			
		||||
#include "textoutputlabel.h"
 | 
			
		||||
 | 
			
		||||
TextoutputLabel::TextoutputLabel()
 | 
			
		||||
{
 | 
			
		||||
	QFont font;
 | 
			
		||||
	font.setPointSize(48);
 | 
			
		||||
	font.setBold(true);
 | 
			
		||||
	this->setFont(font);
 | 
			
		||||
	this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
 | 
			
		||||
	this->setAlignment(Qt::AlignCenter);
 | 
			
		||||
	this->setContextMenuPolicy(Qt::ContextMenuPolicy::CustomContextMenu);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void TextoutputLabel::setText(const QString &text)
 | 
			
		||||
{
 | 
			
		||||
	QLabel::setText(text);
 | 
			
		||||
	QFont currentFont = this->font();
 | 
			
		||||
	int calculatedPointSize = currentFont.pointSize();
 | 
			
		||||
	QFontMetrics fm(currentFont);
 | 
			
		||||
	int contentWidth = this->contentsRect().width() - this->margin();
 | 
			
		||||
	while(calculatedPointSize < 48 && fm.boundingRect(this->text()).width() < contentWidth)
 | 
			
		||||
	{
 | 
			
		||||
		calculatedPointSize += 1;
 | 
			
		||||
		currentFont.setPointSize(calculatedPointSize);
 | 
			
		||||
		fm = QFontMetrics(currentFont);
 | 
			
		||||
	}
 | 
			
		||||
	while(fm.boundingRect(this->text()).width() >= contentWidth)
 | 
			
		||||
	{
 | 
			
		||||
		calculatedPointSize -= 1;
 | 
			
		||||
		currentFont.setPointSize(calculatedPointSize);
 | 
			
		||||
		fm = QFontMetrics(currentFont);
 | 
			
		||||
	}
 | 
			
		||||
	this->setFont(currentFont);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										14
									
								
								textoutputlabel.h
									
									
									
									
									
										一般檔案
									
								
							
							
						
						
									
										14
									
								
								textoutputlabel.h
									
									
									
									
									
										一般檔案
									
								
							@@ -0,0 +1,14 @@
 | 
			
		||||
#ifndef TEXTOUTPUTLABEL_H
 | 
			
		||||
#define TEXTOUTPUTLABEL_H
 | 
			
		||||
#include <QLabel>
 | 
			
		||||
 | 
			
		||||
class TextoutputLabel : public QLabel
 | 
			
		||||
{
 | 
			
		||||
public:
 | 
			
		||||
	TextoutputLabel();
 | 
			
		||||
	virtual void setText(const QString &text);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#endif // TEXTOUTPUTLABEL_H
 | 
			
		||||
		新增問題並參考
	
	封鎖使用者