设置字体、字号等格式属性

设置字体、字号等格式属性

在工具栏上设置文字字体、字号大小、加粗、斜体、下划线以及字体颜色等快捷按钮。

1 、创建fontset.h

#ifndef FONTSET_H
#define FONTSET_H
#include <QtGui>

class FontSet : public QMainWindow
{
    Q_OBJECT

public:
    FontSet(QWidget *parent = 0);

    QLabel *label1;
    QLabel *label2;

    QFontComboBox *fontBox;
    QComboBox *sizeBox;
    QToolButton *boldBtn;
    QToolButton *italicBtn;
    QToolButton *underBtn;
    QToolButton *colorBtn;

    void mergeFormat(QTextCharFormat);

public slots:
    void slotFont(QString);
    void slotSize(QString);
    void slotBold();
    void slotItalic();
    void slotUnder();
    void slotColor();
    void slotCurrentFormatChanged(const QTextCharFormat &fmt);

private :
    QTextEdit *text;
};

#endif // FONTSET_H

2、 创建fontset.cpp

#include "fontset.h"

FontSet::FontSet(QWidget *parent) : QMainWindow(parent)
{
    setWindowTitle(tr("font"));

    QToolBar * toolBar = addToolBar("Font");

    label1 = new QLabel(tr("ZiTi: "));
    fontBox = new QFontComboBox(toolBar);
    fontBox->setFontFilters(QFontComboBox::ScalableFonts);
    toolBar->addWidget(label1);
    toolBar->addWidget(fontBox);

    label2 = new QLabel(tr("number: "));
    sizeBox = new QComboBox(toolBar);
    toolBar->addWidget(label2);
    toolBar->addWidget(sizeBox);

    QFontDatabase db;
    foreach(int size,db.standardSizes())
    {
        sizeBox->addItem(QString::number(size));
    }

    toolBar->addSeparator();

    boldBtn = new QToolButton;
    boldBtn->setIcon(QIcon(":/images/bold.png"));
    boldBtn->setCheckable(true);
    toolBar->addWidget(boldBtn);

    italicBtn = new QToolButton;
    italicBtn->setIcon(QIcon(":/images/italic.png"));
    italicBtn->setCheckable(true);
    toolBar->addWidget(italicBtn);

    underBtn = new QToolButton;
    underBtn->setIcon(QIcon(":/images/underline.png"));
    underBtn->setCheckable(true);
    toolBar->addWidget(underBtn);


    toolBar->addSeparator();
    colorBtn = new QToolButton;
    colorBtn->setIcon(QIcon(":/images/color.png"));
    toolBar->addWidget(colorBtn);

    text = new QTextEdit(this);
    text->setFocus();
    setCentralWidget(text);

    connect(fontBox,SIGNAL(activated(QString)),this,SLOT(slotFont(QString)));
    connect(sizeBox,SIGNAL(activated(QString)),this,SLOT(slotSize(QString)));
    connect(boldBtn,SIGNAL(clicked()),this,SLOT(slotBold()));
    connect(italicBtn,SIGNAL(clicked()),this,SLOT(slotItalic()));
    connect(underBtn,SIGNAL(clicked()),this,SLOT(slotUnder()));
    connect(colorBtn,SIGNAL(clicked()),this,SLOT(slotColor()));

    connect(text,SIGNAL(currentCharFormatChanged(const QTextCharFormat&)),this,SLOT(slotCurrentFormatChanged(const QTextCharFormat&)));
}

void FontSet::slotFont(QString f)
{
    QTextCharFormat fmt;
    fmt.setFontFamily(f);
    mergeFormat(fmt);
}

void FontSet::slotSize(QString num)
{
    QTextCharFormat fmt;
    fmt.setFontPointSize(num.toFloat());
    mergeFormat(fmt);
}

void FontSet::slotBold()
{
    QTextCharFormat fmt;
    fmt.setFontWeight(boldBtn->isChecked()? QFont::Bold : QFont::Normal);
    mergeFormat(fmt);
}

void FontSet::slotItalic()
{
    QTextCharFormat fmt;
    fmt.setFontItalic(italicBtn->isChecked());
    mergeFormat(fmt);
}

void FontSet::slotUnder()
{
    QTextCharFormat fmt;
    fmt.setFontUnderline(underBtn->isChecked());
    mergeFormat(fmt);
}

void FontSet::slotColor()
{
    QColor color = QColorDialog::getColor (Qt::red,this);

    if(color.isValid())
    {
        QTextCharFormat fmt;
        fmt.setForeground(color);
        mergeFormat(fmt);
    }
}

//设置光标的选区,使格式作用于选区内的字符,若没有选区则作用于光标所在处的字符
void FontSet::slotCurrentFormatChanged(const QTextCharFormat &fmt)
{
    fontBox->setCurrentIndex(fontBox->findText(fmt.fontFamily()));
    sizeBox->setCurrentIndex(sizeBox->findText(QString::number(fmt.fontPointSize())));
    boldBtn->setChecked(fmt.font().bold());
    italicBtn->setChecked(fmt.fontItalic());
    underBtn->setChecked(fmt.fontUnderline());
}

void FontSet::mergeFormat(QTextCharFormat format)
{
    QTextCursor cursor = text->textCursor();
    if(!cursor.hasSelection())
    {
        cursor.select(QTextCursor::WordUnderCursor);
    }
    cursor.mergeCharFormat(format);
    text->mergeCurrentCharFormat(format);

}

3、创建main.cpp

#include <QApplication>
#include "fontset.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    FontSet *font = new FontSet;
    font->show();

    return app.exec();
}

创建资源文件fontset.qrc

4、运行
这里写图片描述

5、资源代码

AgileFontSet v2.0 程序说明 迅捷字体设置程序v2.0 - ybmj@vip.163.com(20180830) (1)、程序用途:快捷设置Windows系统字体和桌面图标间距 WinXP、Win7用户可在系统提供的高级外观设置里修改系统字体和桌面图标间距,但是,Win8、Win10系统却没有提供这一功能。熟悉系统的用户虽然可以通过修改注册表、bat、vbs等方式调整系统字体和桌面图标间距,但这些方法都需要注销后重新登录、或重启计算机才能生效。 该程序 AgileFontSet 便是快捷设置Windows系统字体和桌面图标间距的小程序,中文名为“迅捷字体设置程序”。该程序可以加载和保存Win7Preset、Win8xPreset、Win10Preset、UserPreset1 - UserPreset100等多种用户配置。该程序提供窗口界面、命令行两种使用方式。窗口界面简洁快捷、一目了然,普通用户也可轻松使用,并且,设置后无需注销或重启便可立即生效;命令行方式可用于WinPE等需要自动设置系统字体和桌面图标间距的情况。 (2)、使用环境 该程序采用VS2017 WTL10 x86编译(Windows Template Library,http://wtl.sourceforge.net/ ),程序短小精悍、可独立运行,支持32位和64位的WinXp、Vista、Win7、Win8.x、Win10及相应Windows Server版等操作系统。 (3)、窗口界面快捷设置系统字体和桌面图标间距 在Windows中直接运行 AgileFontSet 便可启动窗口界面,启动后会显示系统字体和桌面图标间距的当前值,设置对话框简洁快捷、一目了然,普通用户也可快速方便地设置系统字体和桌面图标间距,并且可以立即生效。在窗口界面,用户可以直观地加载和保存默认配置、Win7Preset、Win8xPreset、Win10Preset、UserPreset1 - UserPreset100等多种用户配置。 (4)、命令行方式设置系统字体和桌面图标间距 在WinPE等需要自动设置系统字体和桌面图标间距的时候,可以使用命令行方式。用户可按以下格式的参数启动程序,所有参数都不分大小写。更改系统字体和桌面图标间距后程序将立即刷新桌面。加方括号[]的为可选参数,加圆括号()的为只能单独使用的可选独立参数,(无参)是不带任何参数。 [path] :指定加载ini配置文件的路径,启动后将显示ini配置文件中的默认配置,注意:含空格的路径必须用双引号包围; [path -xxx] :指定加载ini配置文件的路径path,-xxx 参数选择ini文件中的xxx配置。xxx可以是:Win7Preset、Win8xPreset、Win10Preset、UserPreset1 - UserPreset100 之一; [path -hide] :指定加载ini配置文件的路径path,-hide 参数指定后台加载ini配置文件中的默认配置; [path -xxx -hide] :指定加载ini配置文件的路径path,-xxx 参数选择ini文件中的xxx配置,-hide 参数指定进行后台设置。3个参数组合使用,可以后台加载ini配置文件中的xxx配置; (-?) :显示设置对话框和此帮助信息(当前选项卡),可显示系统字体和桌面图标间距的当前值; (无参) :显示设置对话框(当前选项卡)和此帮助信息,可显示系统字体和桌面图标间距的当前值。 (5)、程序使用实例 # 指定加载ini配置文件"D:\Program Files\User Data\myFontSet.ini",启动后将显示ini配置文件中的默认配置,注意:含空格的路径必须用双引号包围。 AgileFontSet "D:\Program Files\User Data\myFontSet.ini" # 指定加载ini配置文件D:\myFontSet.ini,启动后将选择ini文件中的UserPreset1配置。 AgileFontSet D:\myFontSet.ini -UserPreset1 # 指定后台设置ini配置文件D:\myFontSet.ini中的默认配置。 AgileFontSet D:\myFontSet.ini -hide # 指定后台设置ini配置文件D:\myFontSet.ini中的Win10Preset配置。 AgileFontSet D:\myFontSet.ini -Win10Preset -hide # 显示设置对话框和此帮助信息(当前选项卡),获取系统字体和桌面图标间距的当前值。 AgileFontSet -? # 显示设置对话框(当前选项卡)和此帮助信息,获取系统字体和桌面图标间距的当前值。 AgileFontSet (6)、免责申明 用户可自行斟酌选用该程序,若转载请注明出处。对一切后果,作者不承担任何责任!
在 MATLAB 中设置字体字号可以通过多种方式进行,适用于不同绘图元素的个性化调整。以下是几种常见的设置方法: ### 设置图例字体字号 对于图例(legend),可以使用 `legend` 函数创建图例后,通过句柄对图例进行进一步设置。例如,设置图例的字体名称、字体大小以及字体粗细: ```matlab h = legend('Initialize (x_0, y_0)=(4, 0) \rm{m}', 'Initialize (x_0, y_0)=(0, 8) \rm{m}'); set(h, 'FontName', 'Times New Roman', 'FontSize', 11, 'FontWeight', 'normal')[^1] ``` 该代码将图例文本的字体设为 Times New Roman,字体大小为 11,并且字体样式为正常。 ### 设置坐标轴标签字体字号 如果需要设置 x 轴或 y 轴的标签字体字号,可以在调用 `xlabel` 或 `ylabel` 函数时直接指定相关属性。例如: ```matlab xlabel("x(m)", "FontName", "Times New Roman", "FontSize", 7) ylabel("zc(m) and y(m)", "FontName", "Times New Roman", "FontSize", 7, "LineWidth", 1.5)[^2] ``` 上述代码分别设置了 x 轴和 y 轴标签的字体名称为 Times New Roman,字体大小为 7,并且对于 y 轴标签还额外指定了线宽。 ### 全局设置坐标轴和文本的默认字体 若希望所有后续绘图中的坐标轴标签和文本均采用相同的字体,可以使用全局设置命令: ```matlab set(0, 'defaultAxesFontName', 'Times New Roman'); % 设置坐标轴字体 set(0, 'defaultTextFontName', 'Times New Roman'); % 设置文字字体 ``` 这些命令确保所有新生成的图形对象的坐标轴和文本默认使用 Times New Roman 字体[^3]。 ### 使用 LaTeX 解释器渲染文本并设置字体 当需要更复杂的格式控制时,可以启用 LaTeX 解释器来渲染文本内容。例如,在标题中使用 LaTeX 并设置正体字体: ```matlab title('$\mathrm{y} = \sin(x)$', 'Interpreter', 'latex') ``` 此方式特别适合于科学和技术文档,其中数学表达式需以正体形式显示[^4]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值