[Qt-interest] Autoscrolling--yet friendly--QTextEdit
Sherif Ghali
sherif.ghali at yahoo.com
Fri Dec 10 01:27:56 CET 2010
Hello,
I would like a QTextEdit instance to behave as follows. As long as the user has
not touched the scroll wheel to read what has scrolled out of the widget,
QTextEdit would auto-scroll to show the last line, but QTextEdit would not
wrestle against the user by insisting to move the cursor to the bottom even
while the user is trying to read something that scrolled out of the screen.
Here is my attempt at a solution. As long as the guard “user_has_scrolled” is
not true, MyQTextEdit scrolls to QTextCursor::End. Once the guard is true,
auto-scroll is disabled.
This doesn’t work. Why? Can you suggest a solution?
Sherif
#include <QtGui>
class MyQTextEdit : public QTextEdit
{
public:
MyQTextEdit(QWidget * parent = 0)
: QTextEdit(parent),
n(0),
user_has_scrolled(false)
{}
protected:
virtual void timerEvent(QTimerEvent * /*event*/)
{
append(QString("Message: %1").arg(n++));
if(!user_has_scrolled)
{
append(QString("----Keeping cursor at end----"));
QTextCursor c = this->textCursor();
c.movePosition(QTextCursor::End);
this->setTextCursor(c);
}
}
virtual void wheelEvent(QWheelEvent * event)
{
append(QString("****User has generated a wheelEvent****"));
user_has_scrolled = true;
QAbstractScrollArea::wheelEvent(event);
}
int n;
bool user_has_scrolled;
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyQTextEdit * myqtextedit = new MyQTextEdit;
myqtextedit->resize(300,80);
QBasicTimer timer;
timer.start(500, myqtextedit);
myqtextedit->show();
return app.exec();
}
More information about the Qt-interest-old
mailing list