Skip to content Skip to sidebar Skip to footer

Replacing Qtextedit Bounding Box With A Line

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

Solution 1:

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())

enter image description here

Post a Comment for "Replacing Qtextedit Bounding Box With A Line"