Question

I am working on a text editor using the QT library. I am subclassing QTextEdit for my main editor widget.

Here is my code:

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;
}

...

When the EditorWidget is created, the font shows up correctly. However, when I enter some text, and then delete it, the widget reverts to the default font.

I don't understand what's going on; I've searched Google and Stack Overflow but found nothing. Any help would be greatly appreciated. Thanks!

Was it helpful?

Solution

This thread might be helpful. The setFont...() functions set the format behind the edit cursor, but the default format is free from it. The QT Docs also explains this situation.

"...The current style, which is used to render the content of all standard Qt widgets, is free to choose to use the widget font, or in some cases, to ignore it (partially, or completely). In particular, certain styles like GTK style, Mac style, Windows XP, and Vista style, apply special modifications to the widget font to match the platform's native look and feel. Because of this, assigning properties to a widget's font is not guaranteed to change the appearance of the widget."

In your case, you may try setStyleSheet() instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top