用一条线替换 QTextEdit 边界框 [英] Replacing QTextEdit bounding box with a line
本文介绍了用一条线替换 QTextEdit 边界框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这个问题是对
This question is a slight modification of the bounding box example. I'm trying to understand how to draw simple shapes. I just want to replace the bounding box with a diagonal line from the upperLeft point to the lowerRight point. However, QLine() does not appear to have a show() method and viewport() has no addItem() method. So, I think the line is not being displayed. Here is my modification of the showBoxes method:
def showLines(self):
while self.boxes:
self.boxes.pop() #self.boxes.pop().deleteLater()
#Qline has no deleteLater(). Does that matter?
viewport = self.edit.viewport()
for start, end, ident in db:
rect = self.edit.getBoundingRect(start, end)
x = rect.x()
y = rect.y()
h = rect.height()
w = rect.width()
line = QLine() #QRubberBand(QRubberBand.Rectangle, viewport)
line.setLine(x,y,x+w,y+h) #box.setGeometry(rect)
# need to show line here?
self.boxes.append(line)
解决方案
One possible solution is to replace QRubberBand
with another widget that draws the diagonal, to create a class that inherits from QWidget
and overwrite the paintEvent
method.
class Window(QWidget):
def __init__(self):
super(Window, self).__init__()
self.edit = TextEditor(self)
layout = QVBoxLayout(self)
layout.addWidget(self.edit)
self.boxes = []
def showBoxes(self):
while self.boxes:
self.boxes.pop().deleteLater()
viewport = self.edit.viewport()
for start, end, ident in db:
rect = self.edit.getBoundingRect(start, end)
box = LineBand(viewport)
box.setGeometry(rect)
box.show()
self.boxes.append(box)
def resizeEvent(self, event):
self.showBoxes()
super().resizeEvent(event)
class LineBand(QWidget):
def paintEvent(self, event):
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
painter.setPen(QPen(Qt.black, 1.8))
painter.drawLine(self.rect().topLeft(), self.rect().bottomRight())
这篇关于用一条线替换 QTextEdit 边界框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文