1
我正在使用QT库的文本编辑器上工作。我为我的主编部件继承QTextEdit
。Qt 5-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,但什么也没找到。任何帮助将不胜感激。谢谢!
看来问题在于“另一个文件”。什么是'getFontSize()'和'getFont()'?他们在做什么? – Tay2510
他们访问存储在文件中的用户设置。我测试了这些功能,并且它们完美地工作。 – Alex