2014-03-14 82 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()