[SOLVED] QTextEdit: deleting text to document start disable fontSize
-
wrote on 6 Dec 2012, 10:37 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!!!
-
wrote on 6 Dec 2012, 11:28 last edited by
Sorry, i forgot to say that i'm on Ubuntu 12.04
-
wrote on 6 Dec 2012, 14:06 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_OBJECTprivate:
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);
}@ -
wrote on 6 Dec 2012, 14:47 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);
}
@ -
wrote on 6 Dec 2012, 15:07 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);@ -
wrote on 6 Dec 2012, 15:48 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_OBJECTprivate:
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/6