Qt 5- QTextEdit恢复默认字体
Qt 5- QTextEdit reverts to default font
我正在使用QT库编写文本编辑器。我为我的主编辑器小部件子类化QTextEdit
。
editorwidget.hpp
#ifndef EDITORWIDGET_H_INCLUDED
#define EDITORWIDGET_H_INCLUDED
#include <QTextEdit>
#include <QFile>
class EditorWidget : public QTextEdit
{
Q_OBJECT
public:
EditorWidget(const QString& filename, QWidget* parent = 0);
~EditorWidget();
public slots:
void saveRequested();
//...
private:
QFile* editorFile;
};
#endif
editorwidget.cpp
#include "editorwidget.hpp"
EditorWidget::EditorWidget(const QString& filename, QWidget* parent)
: QTextEdit(parent)
{
this->setFontPointSize(getFontSize()); // this is in another file
this->setFontFamily(getFont()); // also in another file
// those two functions get the font and font size from the user's settings
this->editorFile = new QFile(filename);
}
EditorWidget::~EditorWidget()
{
if(this->editorFile->isOpen()) this->editorFile->close():
delete editorFile;
}
...
创建EditorWidget时,字体会正确显示。但是,当我输入一些文本,然后删除它时,小部件恢复到默认字体。
我不明白发生了什么事;我搜索了Google和Stack Overflow,但一无所获。任何帮助都将非常感激。谢谢!
这个线程可能会有所帮助。setFont...()
函数设置编辑游标后面的格式,但默认格式不受其影响。QT文档也解释了这种情况。
"…当前的样式用于呈现所有标准Qt小部件的内容,可以自由选择使用小部件字体,或者在某些情况下忽略它(部分地或完全地)。特别是,某些样式,如GTK样式、Mac样式、Windows XP和Vista样式,对小部件字体进行了特殊修改,以匹配平台的本机外观和感觉。因此,为小部件的字体分配属性并不能保证改变小部件的外观。"
在你的情况下,你可以尝试setStyleSheet()
代替。
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 如何使用默认参数等选择模板专业化
- 具有默认模板参数的多态类的模板推导失败
- 在C++中测量postscript字体宽度
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 当函数模板参数是具有默认参数的类模板时,函数模板参数的推导如何执行
- 初始化具有非默认构造函数的std::数组项的更好方法
- 何时提供默认参数作为模板参数
- 是默认情况下分配给char数组常量的值
- 具有默认值的引用获取函数
- 具有默认模板类型的默认构造函数的类型推导
- 如何在Directwrite中获得给定字体的可用OpenType功能
- 当给定默认值时,为什么此模板参数推导失败
- ExtTextOut 文本的持续闪烁,在一段时间后,文本将恢复为默认字体
- 如何在 DirectWrite 中使用默认 UI 字体绘制文本
- Qt4/PyQt4-无法设置QTextDocument的默认字体
- QML:如何检索默认字体对象
- Win32:设置富编辑的默认字体和文本颜色
- QAxObject Excel工作表--为整个Excel工作表设置默认字体
- Qt 5- QTextEdit恢复默认字体