2010-08-24 38 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()