python - Pyside 同时滚动两个 qtextedits

标签 python qt qt4 pyside

我有两个子类。一个实现 qplaintextedit,另一个实现 qtextedit。 qtextedit 用于显示 qplaintextedit 的行号。我的问题是让两者同时滚动。我尝试过 self.viewport().scroll(),它实际上没有做任何事情。我尝试过连接 qscrollbars,但 qplaintextedit 的最大值小于 qtextedit 的最大值。我已经尝试过其他几种行号解决方案,但它们不允许您在它们上使用样式表。有没有一种方法可以同时滚动两者,或者是否有一种可设置样式的行号方法?我正在使用 pyside 和 python 2.7。这是我目前正在使用的代码。

class NumberBar(QTextEdit):
def __init__(self):
    super(NumberBar,self).__init__()
    self.count = 1
    self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
    self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
    self.width = self.fontMetrics().width(unicode(10000))
    self.setFixedWidth(self.width)
    self.setStyleSheet("background:transparent;border:0px;")
    self.setTextInteractionFlags(Qt.NoTextInteraction)
    self.viewport().setCursor(Qt.ArrowCursor)
    self.curse = QTextCursor(self.document())
    self.curse.insertText(str(1))
def change(self, new):
    if new > self.count:
        self.curse.movePosition(self.curse.End)
        self.curse.insertBlock()
        self.curse.insertText(str(new+1))
        self.count = new
    else:
        self.curse.movePosition(self.curse.End)
        self.curse.select(self.curse.BlockUnderCursor)
        self.curse.removeSelectedText()
        self.count = new
    print self.verticalScrollBar().minimum(),self.verticalScrollBar().maximum()
def onload(self, new):
    while(new > self.count):
        self.curse.movePosition(self.curse.End)
        self.curse.insertBlock()
        self.curse.insertText(str(self.count+1))
        self.count += 1
def updateContents(self, rect, scroll):
    if scroll:
        print scroll
        self.viewport().scroll(0, scroll)
        self.update()
    else:
        self.update()

class Editor(QWidget):
    def __init__(self,text = None,name=None):
        super(Editor,self).__init__()
        self.layout = QHBoxLayout()
        self.layout.setContentsMargins(0,0,0,0)
        self.layout.setSpacing(0)
        self.number_bar = NumberBar()
        self.edit = TextEdit(self.number_bar,text)
        highlighter = highlight.Highlighter(self.edit.document(),name)
        #self.edit.blockCountChanged.connect(self.number_bar.adjustWidth)
        self.edit.updateRequest.connect(self.number_bar.updateContents)
        self.layout.addWidget(self.number_bar)
        self.layout.addWidget(self.edit)
        self.setLayout(self.layout)

class TextEdit(QPlainTextEdit):
    def __init__(self,numbar,text = None):
        super(TextEdit,self).__init__(text)
        sheet = open("sheet.css",'r').read()
        self.setStyleSheet(sheet)
        self.setLineWrapMode(self.NoWrap)
        self.setViewportMargins(0,0,0,0)
        self.document().blockCountChanged.connect(numbar.change)
        numbar.onload(0 if not text else text.count('\n'))
        self.numbar = numbar
        numbar.verticalScrollBar().show()

我已经尝试过代码here ,但不能通过样式表设置样式。

最佳答案

有一个完整的示例,可以满足您的需求。

Code Editor Example

翻译成 Python 应该不难。

关于python - Pyside 同时滚动两个 qtextedits,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11788422/

相关文章:

python - 在两个类之间传递参数

python - 从字符串列表中的拆分字符串创建字典

python - 在 (X,Y,Z) 坐标图上包括时间标签

c++ - QT中如何不断读取UDP包?

qt4 - 如何找到Qt的版本?

python - 如何处理 marshmallow-sqlalchemy 中的嵌套关​​系

qt - qmake 找不到任何源文件或头文件

qt - 如何在QGraphicsItem中绘制一半的饼图

python - 在 Windows 上使用 python 获取 QProcess 的 PID

c++ - Qt 4.6 和 OpenGL : how to capture keyboard presses with three different widgets at once?