2010-08-24 36 views
0

在我的一个项目中,我想要一个自动滚动文本框。如何在Qt中创建自动滚动文本框?

我不是在谈论有人添加文本行时滚动的文本框,而是像电影"closing credits"序列。

该文本框将全文文本并向下滚动,而不需要任何用户操作。

是否有任何适合此目的的现有小部件?如果不是,那么最好的方法是什么?

回答

2

的GraphicsView方法是最灵活的一个,我认为,如果你想要的东西花哨。

更简单的方法可能是使用“动画框架”,设置QPropertyAnimation并将其连接到QTextBrowser垂直滚动条的“value”属性。 (看看动画框架的例子)。

1

使用QGraphicsView,QGraphicsScene和QGraphicsTextItem。使用QGraphicsTextItem,您可以使用html格式化您的滚动文字。然后启动一个计时器来移动QGraphicsTextItem。

+0

谢谢。我不应该滚动视口而不是移动'QGraphicsTextItem'? – ereOn 2010-08-24 16:51:14

1

Roku建议使用QGraphicsView是一个不错的选择,但是如果您正在寻找复杂的文本渲染,您可能不希望使用QGraphicsView。

另一种方法是使用QTextDocument的渲染功能(àla QAbstractTextDocumentLayout)来绘制感兴趣的文本区域。然后,滚动就是调用update()来呈现文本区域的新部分。

下面是一些Python(PyQt的),表示你需要做的绘图部分:

# 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()