我有两个子类。一个实现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,但无法通过样式表进行样式设置。