0

I have a QPlainTextEdit box in an application that shows information to the user. The application code is huge, so I'll just include the relevant part of the code:

class ProgressBox(QPlainTextEdit):
    """Represents the progress information box."""
    def __init__(self, win):
        """Message box constructor."""
        super().__init__(win)
        self.setReadOnly(True)
        self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
        self.setStyleSheet("""
            border: 1px inset grey; background-color: white; padding: 10px;
            """)

    def contextMenuEvent(self, event):
        menu = QMenu(self)
        menu.setStyleSheet("""
            QMenu {
                border: 1px inset grey;
                background-color: #fff;
                color: #000;
                padding: 0;
            }
            QMenu:selected {
                background-color: #ddf;
                color: #000;
            }
        """)
        clear_action = menu.addAction("Clear")
        # copy_all_action = menu.addAction("Copy Selected")

        action = menu.exec_(self.mapToGlobal(event.pos()))
        if action:
            if action == clear_action:
                self.clear_text()

    def add_text(self, text):
        self.setPlainText(self.toPlainText() + text)
        self.verticalScrollBar().setValue(
            self.verticalScrollBar().maximum()
            )
        self.repaint()
        app.processEvents()

    def charCount(self):
        return len(self.toPlainText())

    def clear_text(self):
        self.setPlainText("")
        self.repaint()

The "progress box" is initiated using (where win = QMainWindow() ):

win.info_box = ProgressBox(self)

And updated using:

win.info_box.add_text("Blah, blah.")

Now, the problem I have is minor, but still annoying. The scrollbar for the QPlaintextEdit widget does not display correctly (it has no "handle"). See the image: Horrible scrollbar

I would like the scrollbar in the red box to look the same as the scrollbar on the QTableWidget above it. Does anyone know how I can do this please?

Heike
  • 24,102
  • 2
  • 31
  • 45
  • 1
    By setting the style sheet of your widget to "border: 1px inset grey; background-color: white; padding: 10px;", the background of all child widgets including the scroll bar will be set to white. To keep the default style for the scroll bar you could try something like `self.setStyleSheet("QPlainTextEdit {border: 1px inset grey; background-color: white; padding: 10px;}")`. – Heike Mar 27 '20 at 09:46
  • Yes, this vastly improves it, thank you! Do you know how I can remove the padding/margin for just the scrollbar? I tried variations of "QPlainTextEdit::QScrollBar {margin: -10px; padding: -10px;}", with no luck. If I remove the padding the scrollbar is at the edge of the box, but so is the text. –  Mar 27 '20 at 10:03
  • If you only want extra space around the text I would just use something like `self.document().setDocumentMargin(10)` instead of trying to adjust the padding in the style sheet – Heike Mar 27 '20 at 10:13
  • If I add that, the scrolbar also has a margin around it. –  Mar 27 '20 at 10:40
  • I meant that you should use `QTextDocument.setDocumentMargin` instead of using "padding: 10px" in the style sheet of `QPlainTextEdit`. That should add space on the left- and right-side of the text but no space around the scrollbar. – Heike Mar 27 '20 at 12:39
  • Can you please explain the exact code and where to enter it? The widget is a QPlainTextEdit which does not have an attribute setDocumentMargin. –  Mar 27 '20 at 13:27
  • `setDocumentMargin` is an attribute of `QTextDocument` not of `QPlainTextEdit`. `QPlainTextEdit.document()` will get the underlying `QTextDocument` of the `QPlainTextEdit`. So in `ProgressBox.__init__` you would need to set `self.document().setDocumentMargin(10)`. – Heike Mar 28 '20 at 07:10
  • I tried that and it didn't work - it resulted in the scrollbar being contained within the margin too. It is okay, I am happy using no margin for the whole widget. Thank you. –  Mar 28 '20 at 11:48

0 Answers0