PDA

View Full Version : QTextEdit and font family



giusepped
4th June 2008, 04:53
I have a basic quetion.
I would change the font family of a QTextEdit by means of the following code:

void Window::chooseFont()
{
bool ok;
QFont initial("Times New Roman", 48);
QFont font = QFontDialog::getFont(&ok, initial, this);

if (ok) {
QTextCharFormat format;
format.setFont(font);
txt->mergeCurrentCharFormat(format);


}

}

The strange thing is that I get changed the text if and only if I make a selection inside txt. i.e. the character format does not change if a continue to edit on the right of the current cursor position.
Any help appreciated

aamer4yu
4th June 2008, 05:20
Why dont u simply use QWidget::setFont ??
txt->setFont(font) ??

giusepped
4th June 2008, 06:22
Simply because the result is the same. I tryed your tip without success.
G

giusepped
4th June 2008, 07:34
In some way I solved.


void Window::chooseFont()
{
bool ok;
QFont initial("Times New Roman", 48);
QFont font = QFontDialog::getFont(&ok, initial, this);

if (ok) {
QTextCharFormat format;
format.setFont(font);
txt->setCurrentFont(font);
txt->setEnabled(true);

}
txt->setWindowState(Qt::WindowActive);
}

Anyway, still the behavior is not perfect.
When I click on the Font button the QFontDialog opens, I select the font, and when I close the Dialog, if I press Tab the focus go on the QTextEdit and I can type with the selected font.
On the other hand, if I close the QFontDialog and I do not press Tab, bu use the mouse to get the focus on QTextEdit, I cannot type with the selected font.
There is a way to understand why?
Regards

patrik08
4th June 2008, 08:15
to handle text on QTextEdit , QTextCursor make the job ...



void FontText()
{
/* get existing font txt = QTextEdit */
QTextCursor c = txt->textCursor();
QTextCharFormat format = c.charFormat();
QFont f = format.font();

bool ok;
QFont fontyour = QFontDialog::getFont(&ok,f,0);
if (ok) {

format.setFont(fontyour);
c.setCharFormat(format);

}
}

giusepped
4th June 2008, 10:04
Did you try your code?
Because the result is the same: if I click insiede the QTextEdit the font selection is lost.

patrik08
4th June 2008, 10:17
Did you try your code?
Because the result is the same: if I click insiede the QTextEdit the font selection is lost.

Cursor chance if you click on other area..
Yes i try the code :) is part from... my GraphicsViewEdit
http://www.qt-apps.org/content/show.php/GraphicsViewEdit+Layer?content=80234

IMO: mi prendi in giro?