代码之家  ›  专栏  ›  技术社区  ›  ereOn

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

  •  0
  • ereOn  · 技术社区  · 14 年前

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

    我说的不是一个文本框,每当有人添加一个文本行滚动,而是像电影的东西 "closing credits" 顺序。

    有没有适合这个目的的小部件?如果没有,那最好的办法是什么?

    3 回复  |  直到 14 年前
        1
  •  2
  •   Frank Osterfeld    14 年前

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

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

        2
  •  1
  •   user362638 user362638    14 年前

        3
  •  1
  •   Kaleb Pederson    14 年前

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