[NOT SOLVED] Unable to set font of the widget derived from QPlainTextEdit



  • Hi

    I have a widget derived from QPlainTextEdit. (call it LateXEditor) It have a syntax highlighter, code completer and a line numbering widget on its left side.
    The problem is that font of widget is not changed with setFont(). I tried calling setFont in ctor and out of there (after widget is created) but anything not works. The funny thing is that font of sidebar (line numbering area) changes when setFont() for widget is called!
    @
    // latexedit.h
    class LatexTextEdit : public QPlainTextEdit
    {
    ....
    }
    // latexedit.cpp
    LatexTextEdit::LatexTextEdit(QWidget *parent) :
    QPlainTextEdit(parent)
    {
    // ....
    setFont(Configuration::instance()->displayFont);
    qDebug()<<"F:"<<font(); // output is: F: QFont( "Tahoma,13,-1,5,50,0,0,0,0,0" )
    // some other code...
    }
    @
    What I am missing?



  • What's the "Configuration::instance()->displayFont" ? Very strange syntax :-)



  • [quote author="Chuck Gao" date="1309615318"]What's the "Configuration::instance()->displayFont" ? Very strange syntax :-)[/quote]

    instance() is a static method of Configuration class which returns an instance of Configuration (if not exists crates one and loads font from setting file using QSettings.)

    That's not important! I replaced that with this:
    @
    setFont(QFont("Tahoma"));
    @
    and nothing happened.



  • I'm sorry, but what are you expected. With the code, the font was changed and qDebug output is right. So you problem is the display text's font doesn't change?



  • bq. With the code, the font was changed and qDebug output is right. So you problem is the display text’s font doesn’t change?

    yes!



  • ok. I forget why this function can not change the font of your plainTextEdit, but there is still a way to make this happend, try style-sheet like this:
    @
    // this->setFont(QFont("Tahoma"));
    this->setStyleSheet(QString("font: bold italic large "Tahoma""));
    qDebug() << font(); // The output is not Tahoma, but the displaying text is.
    @

    We can check the document and find what's the differences. :-)



  • AFAIK this way I can't obtain current font. I would like to save which font is used and load it again from setting file.



  • So doesn't anybody know what's happening here?
    Should I give more details or codes ?



  • I'm still waiting....



  • @document()->setDefaultFont(myFont);@
    The problem is that you set the font on the widget, but not the content.



  • Not works!
    @
    void LatexTextEdit::updateViewSetting()
    {
    document()->setDefaultFont((Configuration::instance()->displayFont));
    this->setFont((Configuration::instance()->displayFont));
    qDebug()<<"Widget Font:"<<font();
    qDebug()<<"Docement font:"<<document()->defaultFont();
    this->update();
    }
    @
    Output is:
    @
    Widget Font: QFont( "Tahoma,10,-1,5,50,0,0,0,0,0" )
    Docement font: QFont( "Tahoma,10,-1,5,50,0,0,0,0,0" )
    @
    But nothing happens. I tried with new documents too.



  • In that case I'll need to see the code where you insert the text into your widget. If you do it by hand, then it should be in the default font.



  • Ok, I call updateViewSetting in my ctor. This is the ctor:
    @
    LatexTextEdit::LatexTextEdit(QWidget *parent) :
    QPlainTextEdit(parent)
    {
    createWidgets();
    createConnections();
    // setWordWrapMode(QTextOption::QTextOption::NoWrap);
    highlighter = new srchiliteqt::Qt4SyntaxHighlighter(document());
    highlighter->init(qApp->applicationDirPath()+"/latex.lang");
    highlighter->setFormattingStyle(qApp->applicationDirPath()+"/latex.css");
    updateViewSetting();
    updateLineNumberAreaWidth();
    // highlightCurrentLine();
    }
    @
    and this is definition of class:
    @
    class LatexTextEdit : public QPlainTextEdit
    {
    Q_OBJECT
    public:
    explicit LatexTextEdit(QWidget *parent = 0);
    // ....
    void updateViewSetting();
    // ...
    private:
    // void highlightCurrentLine();
    QStringListModel *model;
    QCompleter *completer;
    bool completedAndSelected;
    srchiliteqt::Qt4SyntaxHighlighter highlighter;
    QWidget
    lineNumberArea;
    @



  • I have a guess... I used Qt4SyntaxHighlighter that reads a css file and applies highlighting on the content. I didn't set any font in that css file (just colors). Is this related to syntax highlighter ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.