Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

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

    General and Desktop
    2
    6
    2279
    Loading More Posts
    • 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
      goldrake last edited by

      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 Reply Quote 0
      • G
        goldrake last edited by

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

        1 Reply Last reply Reply Quote 0
        • G
          goldrake last edited by

          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 Reply Quote 0
          • _
            _rmn last edited by

            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 Reply Quote 0
            • G
              goldrake last edited by

              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 Reply Quote 0
              • _
                _rmn last edited by

                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 Reply Quote 0
                • First post
                  Last post