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
顯示了號碼,並增加新的生產線時的數字正在更新。但是當它到達小部件的末尾時,文本會在輸入時繼續滾動,但行號會停止更新,並且行號區域不會滾動!
太謝謝你了!問題解決了。祝福,願力量與你同在......;) – Alex