2014-03-14 61 views
1

我正在使用QT库的文本编辑器上工作。我为我的主编部件继承QTextEditQt 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,但什么也没找到。任何帮助将不胜感激。谢谢!

+0

看来问题在于“另一个文件”。什么是'getFontSize()'和'getFont()'?他们在做什么? – Tay2510

+0

他们访问存储在文件中的用户设置。我测试了这些功能,并且它们完美地工作。 – Alex

回答

1

This线程可能会有所帮助。 setFont...()函数设置编辑光标后面的格式,但默认格式是免费的。 QT Docs也解释了这种情况。

“...当前样式用于呈现所有标准Qt小部件的内容,可以自由选择使用小部件字体,或者在某些情况下可以忽略它(部分或完全)特别是GTK风格,Mac风格,Windows XP风格和Vista风格等特定风格,对窗口小部件字体进行了特殊修改,以匹配平台本身的外观和风格,因此,不能保证为窗口小部件的字体分配属性改变小部件的外观。“

对您而言,您可以尝试使用setStyleSheet()