Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] QTextEdit: deleting text to document start disable fontSize
Forum Update on Monday, May 27th 2025

[SOLVED] QTextEdit: deleting text to document start disable fontSize

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 2.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    goldrake
    wrote on 6 Dec 2012, 10:37 last edited by
    #1

    Hi all,
    i found a really strange behaviour in QTextEdit.

    I've take the example provided in the SDK (Application Example) and modified to use a QTextEdit instad of a QPlainTextEdit.
    Then in the constructor of the MainWindow i set the fontSize after the creation of the textEdit

    @ textEdit = new QTextEdit;
    textEdit->setFontPointSize(24.0);
    setCentralWidget(textEdit);
    @

    Running the example and starting typing, the font is bigger as expected.
    But when i delete all the text and start typing again, the fontSize is set again to the default value.

    What i'm doing wrong??

    Thanks for any help!!!

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goldrake
      wrote on 6 Dec 2012, 11:28 last edited by
      #2

      Sorry, i forgot to say that i'm on Ubuntu 12.04

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goldrake
        wrote on 6 Dec 2012, 14:06 last edited by
        #3

        Here it is a much simpler example that shows the strange behaviour.
        It is so simple that i'm sure that i'm missing something silly.

        Can anyone help me?

        main.cpp

        @#include <QtGui/QApplication>
        #include "mainwindow.h"

        int main(int argc, char *argv[])
        {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();

        return a.exec();
        }@

        mainwindow.h

        @#ifndef MAINWINDOW_H
        #define MAINWINDOW_H

        #include <QtGui/QMainWindow>
        #include <QTextEdit>

        class MainWindow : public QMainWindow
        {
        Q_OBJECT

        private:
        QTextEdit *m_TextEdit;

        public:
        MainWindow(QWidget *parent = 0);
        };

        #endif // MAINWINDOW_H@

        mainwindow.cpp

        @#include "mainwindow.h"

        MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        {
        m_TextEdit = new QTextEdit;
        m_TextEdit->setFontPointSize(24.0);
        setCentralWidget(m_TextEdit);
        }@

        1 Reply Last reply
        0
        • _ Offline
          _ Offline
          _rmn
          wrote on 6 Dec 2012, 14:47 last edited by
          #4

          Try this:

          @
          #include "mainwindow.h"
          #include <QFont>

          MainWindow::MainWindow(QWidget *parent)
          : QMainWindow(parent)
          {
          m_TextEdit = new QTextEdit;

          QFont font = m_TextEdit->font();
          font.setPointSizeF(24.0);
          m_TextEdit->setFont(font);

          setCentralWidget(m_TextEdit);
          }
          @

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goldrake
            wrote on 6 Dec 2012, 15:07 last edited by
            #5

            SOLVED

            The method setFontPointSize() is useless.
            The true method to set the font size is

            @ QFont font = m_TextEdit->font();
            font.setPointSizeF(24.0);
            m_TextEdit->setFont(font);@

            1 Reply Last reply
            0
            • _ Offline
              _ Offline
              _rmn
              wrote on 6 Dec 2012, 15:48 last edited by
              #6

              method QTextEdit::setFontPointSize(qreal) is not useless at all. This method sets current font size.
              Check difference:

              mainwindow.h
              @
              #ifndef MAINWINDOW_H
              #define MAINWINDOW_H

              #include <QtGui/QMainWindow>
              #include <QtGui/QTextEdit>
              #include <QtGui/QPushButton>

              class MainWindow : public QMainWindow
              {
              Q_OBJECT

              private:
              QTextEdit *m_TextEdit;
              QPushButton *m_IncFontBtn;
              int m_FontSize;

              public slots:
              void increaseFontSizeBySettingNewFont();
              void increaseFontSizeByTextEditMethod();

              public:
              MainWindow(QWidget *parent = 0);
              };

              #endif // MAINWINDOW_H
              @

              mainwindow.cpp
              @
              #include "MainWindow.h"

              #include <QFont>
              #include <QHBoxLayout>

              MainWindow::MainWindow(QWidget *parent)
              : QMainWindow(parent)
              {
              m_TextEdit = new QTextEdit;
              m_IncFontBtn = new QPushButton("increase font size");
              m_FontSize = 9;

              setCentralWidget(new QWidget(this));
              QHBoxLayout* lay = new QHBoxLayout(this->centralWidget());
              lay->addWidget(m_TextEdit);
              lay->addWidget(m_IncFontBtn);

              // change value of this param for connect to different slots
              bool checkincreaseFontSizeBySettingNewFont = true;
              if (checkincreaseFontSizeBySettingNewFont)
              connect(m_IncFontBtn, SIGNAL(clicked()), SLOT(increaseFontSizeBySettingNewFont()));
              else
              connect(m_IncFontBtn, SIGNAL(clicked()), SLOT(increaseFontSizeByTextEditMethod()));
              }

              void MainWindow::increaseFontSizeBySettingNewFont()
              {
              QFont font = m_TextEdit->font();
              font.setPointSize(++m_FontSize);
              m_TextEdit->setFont(font);
              }

              void MainWindow::increaseFontSizeByTextEditMethod()
              {
              m_TextEdit->setFontPointSize(++m_FontSize);
              }
              @

              1 Reply Last reply
              0

              1/6

              6 Dec 2012, 10:37

              • Login

              • Login or register to search.
              1 out of 6
              • First post
                1/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved