Issue
I have two subclasses. One implements qplaintextedit, the other qtextedit. The qtextedit is being used to show line numbers for the qplaintextedit. My problem is getting the two to scroll at the same time. I have tried self.viewport().scroll(), that does not actually do anything. I have tried connecting the qscrollbars, but the maximum of the qplaintextedit is smaller than the qtextedit's. I have tried I have tried several other solutions to line numbers, and they do not allow you to use stylesheets on them. Is there a way of scrolling both simultaneously, or failing that an approach to line numbers which is styleable? I am using pyside with python 2.7. Here is the code I am using at the moment.
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()
I have tried the code here, but that can not be styled by a style sheet.
Solution
There is a full example that does just what you want.
Translating to Python shouldn't be difficult.
Answered By - Oleh Prypin
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.