Qt设置字体格式和颜色

Qt设置字体格式和颜色_第1张图片

#include "port.h"
#include 
#include 
#include 

port::port(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
	connect(ui.Black,&QRadioButton::clicked,this,&port::slotSetTextFontColor);
	connect(ui.Red, &QRadioButton::clicked, this, &port::slotSetTextFontColor);
	connect(ui.Blue, &QRadioButton::clicked, this, &port::slotSetTextFontColor);
	connect(ui.Underline, &QCheckBox::stateChanged, this, &port::slotSetUnderLine);
	connect(ui.Italic, &QCheckBox::stateChanged, this, &port::slotSetItalic);
	connect(ui.Bold, &QCheckBox::stateChanged, this, &port::slotSetBold);
}

port::~port()
{}

void port::slotSetUnderLine(int state)
{
	QFont font = ui.plainTextEdit->font();
	if (state == 2)
		font.setUnderline(true);
	else
		font.setUnderline(false);
	ui.plainTextEdit->setFont(font);
}

void port::slotSetItalic(int state)
{
	if (state == 2)
	{
		QFont font = ui.plainTextEdit->font();
		font.setItalic(true);
		ui.plainTextEdit->setFont(font);
	}
}

void port::slotSetBold(int state)
{
	if (state == 2)
	{
		QFont font = ui.plainTextEdit->font();
		font.setBold(true);
		ui.plainTextEdit->setFont(font);
	}
}
//设置颜色
void port::slotSetTextFontColor()
{
	QPalette plet = ui.plainTextEdit->palette();
	if (ui.Black->isChecked())
		plet.setColor(QPalette::Text,Qt::black);
	if (ui.Red->isChecked())
		plet.setColor(QPalette::Text,Qt::red);
	if (ui.Blue->isChecked())
		plet.setColor(QPalette::Text,Qt::blue);
	ui.plainTextEdit->setPalette(plet);
}

你可能感兴趣的:(qt)