Issue
Here is my code:
import sys
from PySide.QtCore import *
from PySide.QtGui import *
class Editor(QPlainTextEdit):
def __init__(self, parent):
super(Editor, self).__init__()
self.setPlainText( u'apple, banana\norange\nblah blah\n\nOh yeah!....\n'*2 )
self.setParent( parent )
self.setWordWrapMode( QTextOption.NoWrap )
self.setViewportMargins( 50,0,0,0 )
QObject.connect( self, SIGNAL("textChanged()"), self.repainting )
def repainting(self) : self.parent().update()
class WinE(QMainWindow):
def __init__(self, font=QFont( 'Monospace', 12 )):
super(WinE, self).__init__()
self.font = font
self.font.setFixedPitch( True )
self.ce = Editor( self )
self.ce.setFont( self.font )
self.setWindowTitle('Code Editor')
self.textr = QRect( 3, 5, self.ce.childrenRect().x() -12, self.ce.childrenRect().height() )
self.setGeometry( QRect(800, 840, 351, 250) )
self.setCentralWidget( self.ce )
self.show()
def paintEvent(self, event):
qp = QPainter ()
qp.begin ( self )
self.drawLiNums ( qp )
qp.end ()
def drawLiNums(self, qp):
qp.setPen ( QColor(255, 255, 255) )
qp.setFont ( self.font )
qp.drawText ( self.textr, Qt.AlignRight, self.lineNumeration() )
def lineNumeration(self):
return ''.join( [str(n+1) +'\n' for n in range( len(self.ce.toPlainText().splitlines(False)) )] )
def main():
app = QApplication(sys.argv)
ex = WinE()
sys.exit(app.exec_())
if __name__ == '__main__': main()
Editor
shows up with numbers, and the numbers are updating when adding new lines. But when it reaches the end of the widget, the text continues to scroll while typing, but the line numbers stop updating, and there's no scrolling whatsoever on line number area!
Solution
I hope this solves your problem!
I simply called parent().update()
every time the text is scrolled down. And I started the lineNumeration at the first visible line (line=block in qt)
Best wishes, MrP :)
import sys
from PySide.QtCore import *
from PySide.QtGui import *
class Editor(QPlainTextEdit):
def __init__(self, parent):
super(Editor, self).__init__()
self.setPlainText( u'apple, banana\norange\nblah blah\n\nOh yeah!....\n'*6 )
self.setParent( parent )
self.setWordWrapMode( QTextOption.NoWrap )
self.setViewportMargins( 50,0,0,0 )
QObject.connect( self, SIGNAL("textChanged()"), self.repainting )
def repainting(self):
self.parent().update()
def scrollContentsBy(self, *args, **kwargs):
self.parent().update()
return QPlainTextEdit.scrollContentsBy(self, *args, **kwargs)
class WinE(QMainWindow):
def __init__(self, font=QFont( 'Monospace', 12 )):
super(WinE, self).__init__()
self.font = font
self.font.setFixedPitch( True )
self.ce = Editor( self )
self.ce.setFont( self.font )
self.setWindowTitle('Code Editor')
self.textr = QRect( 3, 5, self.ce.childrenRect().x() -12, self.ce.childrenRect().height() )
self.setGeometry( QRect(800, 840, 351, 250) )
self.setCentralWidget( self.ce )
self.show()
def paintEvent(self, event):
qp = QPainter ()
qp.begin ( self )
self.drawLiNums ( qp )
qp.end ()
def drawLiNums(self, qp):
qp.setPen ( QColor(255, 255, 255) )
qp.setFont ( self.font )
qp.drawText ( self.textr, Qt.AlignRight, self.lineNumeration() )
def lineNumeration(self):
offset=self.ce.firstVisibleBlock().firstLineNumber()
return ''.join(str(i)+"\n" for i in range(offset+1, self.ce.blockCount()+1))
def main():
app = QApplication(sys.argv)
ex = WinE()
sys.exit(app.exec_())
if __name__ == '__main__': main()
Answered By - MrP01
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.