C++ 如何在Qt中创建自动滚动文本框?

C++ 如何在Qt中创建自动滚动文本框?,c++,qt,scroll,C++,Qt,Scroll,在我的一个项目中,我希望有一个自动滚动的文本框 我说的不是一个文本框,每当有人添加文本行时就会滚动,而是一个类似电影序列的东西 文本框将被文本填满,并在没有任何用户操作的情况下缓慢向下滚动 是否有任何现有的小部件适合此用途?如果没有,最好的方法是什么?使用QGraphicsView、QGraphicscene和QGraphicsTextItem。使用QGraphicsTextItem,您可以使用html对滚动文本进行良好的格式化。然后启动计时器以移动QGraphicsTextItem。使用QGr

在我的一个项目中,我希望有一个自动滚动的文本框

我说的不是一个文本框,每当有人添加文本行时就会滚动,而是一个类似电影序列的东西

文本框将被文本填满,并在没有任何用户操作的情况下缓慢向下滚动


是否有任何现有的小部件适合此用途?如果没有,最好的方法是什么?

使用QGraphicsView、QGraphicscene和QGraphicsTextItem。使用QGraphicsTextItem,您可以使用html对滚动文本进行良好的格式化。然后启动计时器以移动QGraphicsTextItem。

使用QGraphicsView、QGraphicscene和QGraphicsTextItem。使用QGraphicsTextItem,您可以使用html对滚动文本进行良好的格式化。然后启动计时器以移动QGraphicsTextItem。

Roku建议使用QGraphicsView是一个很好的建议,但是如果您正在寻找复杂的文本呈现,您可能不希望使用QGraphicsView

另一种方法是使用的渲染功能(a la)来绘制感兴趣的文本区域。滚动只需调用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()

Roku建议使用QGraphicsView是一个很好的建议,但是如果您正在寻找复杂的文本呈现,您可能不希望使用QGraphicsView

另一种方法是使用的渲染功能(a la)来绘制感兴趣的文本区域。滚动只需调用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()

如果您想要一些新奇的东西,我认为GraphicsView方法是最灵活的方法


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

如果您想要一些新奇的东西,我认为GraphicsView方法是最灵活的方法


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

谢谢。我是否应该滚动视口而不是移动
QGraphicsTextItem
?谢谢。我是否应该滚动视口而不是移动
QGraphicsTextItem