PDA

View Full Version : QPlainTextEdit auto scroll



faldzip
31st January 2009, 18:01
Hi!

I made MyTextEdit derived from QPlainTextEdit with added line numbers widget. I also changed style by setting:


setStyle(new QCleanlooksStyle);

I also have a word wrapping on. Now, when Im typing some text on the last line of the document (the last on the bottom) and I reach the end of line, text is wrapped but text edit doesn't want to scroll. Whats more it even doesn't want to extend vertical scrollbar. You cannont scroll with scrollbar or mouse wheel unless you resize the window or type enter to make a new line, because making new lines is scrolling the text edit. It's quite annoying as I'm writing LaTeX document so dont need to make new lines, cause I have pdflatex for that job and line wrapping is all I need.

Any suggestions?
(I'm using Qt 4.4.3 on Vista with msvc2005)

Thanks in advance.

wysota
31st January 2009, 18:12
Could we see some code?

jpn
31st January 2009, 18:35
There's one relevant task-tracker entry: AutoScrolling does not seem to work when using the QPlainTextEdit::appendPlainText method. Give it a try with a recent 4.5 snapshot..

faldzip
31st January 2009, 19:38
Thanks for your answers, I compiled with Qt 4.5.0-beta1 and it's working but... now i have different problem (which did not appear with 4.4.3). The font settings are not propagating to my LineNumberWidget :/
Here's the mytextedit.cpp source code:


#include "mytextedit.h"
#include "linenumberwidget.h"

MyTextEdit::MyTextEdit(QWidget * parent)
: QPlainTextEdit(parent)
, m_lineNumbersWidth(LN_BASE_WIDTH)
{
QFont font;
#if defined(Q_WS_X11)
font.setFamily("Monospace");
#else
font.setFamily("Courier New");
#endif
font.setPointSize(9);
setFont(font);
setViewportMargins(m_lineNumbersWidth, 0, 0, 0);
//setFrameStyle(QFrame::NoFrame);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
m_lineNumberWidget = new LineNumberWidget(this);
font.setPointSize(8); // <- added in build with 4.5.0
m_lineNumberWidget->setFont(font); // <- 4.5.0
QRect vr = viewport()->rect();
m_lineNumberWidget->setGeometry(vr.left() + frameWidth(), vr.top() + frameWidth(), m_lineNumbersWidth, vr.height());
connect(this, SIGNAL(updateRequest(QRect,int)), this, SLOT(slot_updateRequest(QRect,int)));
}

void MyTextEdit::resizeEvent(QResizeEvent * e)
{
QPlainTextEdit::resizeEvent(e);
QRect vr = viewport()->rect();
m_lineNumberWidget->setGeometry(vr.left() + frameWidth(), vr.top() + frameWidth(), m_lineNumbersWidth, vr.height());
}

void MyTextEdit::lineNumberWidgetPaintEvent(QPaintEvent * e)
{
QPainter p(m_lineNumberWidget);
QColor c(240, 240, 240);
p.fillRect(e->rect(), c);
p.setPen(QColor(175, 175, 75));
QRect vr = viewport()->rect();
QFontMetrics fm(p.fontMetrics());
QTextBlock block = firstVisibleBlock();
int top = (int)blockBoundingGeometry(block).translated(conte ntOffset()).top();
int bottom = top;
while (block.isValid() && top <= e->rect().bottom())
{
top = bottom;
bottom = top + (int)blockBoundingRect(block).height();
QTextBlock nextBlock = block.next();
QString number = QString::number(block.blockNumber() + 1);
int offset = (fontMetrics().height() - fm.height())/2; // <- 4.5.0
p.drawText(2, top + offset, m_lineNumbersWidth - 4, fm.height(), Qt::AlignRight, number);
block = nextBlock;
}
}

void MyTextEdit::slot_updateRequest(const QRect & rect, int dy)
{
m_lineNumberWidget->scroll(0, dy);
m_lineNumberWidget->update(0, rect.y(), 36, rect.height());
}


because the minimum font point size in my app is 8 i set that size in my LineNumberWidget and now i'm using an offset to center line numbers vertically. But I was happy with the effect I had in 4.4.3 where font of LineNumberWidget was changing to the same font as it was set in MyTextEdit - the parent widget for LineNumberWidget.
Do I have to tell that child widget explicitly that it should copy parents font settings?

jpn
31st January 2009, 19:52
Probably has something to do with this change: Font and Palette propagation in Qt (http://labs.trolltech.com/blogs/2008/11/16/font-and-palette-propagation-in-qt/)... Does it make any difference if you create the child widget first and then change the font?

Pritpal Bedi
13th January 2010, 18:11
Hi



#include "mytextedit.h"
#include "linenumberwidget.h"


Can you please post code for above .h(s) and how did you implemented LineNumberWidget. And also can you show code for firstVisibleBlock() ?

Thanks
Pritpal Bedi