2016-01-24 27 views
1

滾動行號,這裏是我的代碼:如何在一個QTextEdit

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顯示了號碼,並增加新的生產線時的數字正在更新。但是當它到達小部件的末尾時,文本會在輸入時繼續滾動,但行號會停止更新,並且行號區域不會滾動!

回答

3

我希望這可以解決您的問題!
每當文本向下滾動時,我就簡單地調用parent().update()。我開始lineNumeration在第一個可見行(行=在QT塊)
最良好的祝願,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() 
+0

太謝謝你了!問題解決了。祝福,願力量與你同在......;) – Alex