tags:

views:

49

answers:

3

In one of my projects, I would like to have an auto-scrolling textbox.

I'm not talking of a textbox that scrolls whenever someone adds a text line, but of something which is like a movie "closing credits" sequence.

The textbox would be fullfiled with text and down-scroll slowly without any user action.

Is there any existing widget that would fit this purpose ? If not, what would be the best way to achieve that ?

+1  A: 

Use QGraphicsView, QGraphicsScene and QGraphicsTextItem. With QGraphicsTextItem you can use html to format your scrolling text nicely. Then start a timer to move the QGraphicsTextItem.

Roku
Thanks. Shouldn't I scroll the viewport instead of moving the `QGraphicsTextItem` ?
ereOn
+1  A: 

Roku's suggestion to use QGraphicsView is a good one, but in the event that you're looking for complex text rendering, you may not wish to use QGraphicsView.

Another way to do it is to use QTextDocument's rendering capabilities (à la QAbstractTextDocumentLayout) in order to draw the text region of interest. Scrolling is then simply a matter of calling update() to render a new portion of the text region.

Here's some Python (PyQt) that represents the drawing portion of what you need to do:

# stored within your widget
doc = QTextDocument(self)
doc.setHtml(yourText) # set your text
doc.setTextWidth(self.width()) # as wide as your current widget
ctx = QAbstractTextDocumentLayout.PaintContext()
dl = doc.documentLayout()

# and within your paint event
painter.save()
# you're probably going to draw over the entire widget, but if not
# painter.translate(areaInWhichToDrawRect);
painter.setClipRect(areaInWhichToDrawRect.translated(-areaInWhichToDrawRect.topLeft()))
# by changing the drawing area for each update you emulate scrolling
ctx.clip = theNextAreaToDraw()
dl.draw(painter, ctx)
painter.restore()
Kaleb Pederson
+2  A: 

The GraphicsView approach is the most flexible one I think, if you want something fancy.

An easier approach might be using the "Animation Framework", setting up a QPropertyAnimation and to connect it to the "value" property of a QTextBrowser's vertical scrollbar. (Have a look at the Animation Framework examples).

Frank