Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QPlainTextEdit set background not working in QGraphicsProxyWidget



  • Hi,
    I'm sorry, I know these 'fix my code' questions are often quite useless, but I am really stuck, my apologies.
    I am trying to set the background-color of a QPlainTextEdit via stylesheet and it doesn't work. Setting the color does work though. The widget gets placed with a QGraphicsProxyWidget in a QGraphicsScene. But from what I know, this shouldn't affect the visual appearance of the widget.

    class Code_NodeInstance_MainWidget(QWidget):
        def __init__(self, parent_node_instance):
            super(Code_NodeInstance_MainWidget, self).__init__()
    
            self.code_text_edits = []
    
            self.code_font = QFont('Courier New', 12)
    
            # UI
            self.setLayout(QVBoxLayout())
    
            self.add_new_script()
    
    
        def add_new_script(self):
            code_text_edit = QPlainTextEdit()
            code_text_edit.setPlainText('test code')
            code_text_edit.setFont(self.code_font)
            code_text_edit.setStyleSheet('background: black; color: grey;')
            self.code_text_edits.append(code_text_edit)
            self.layout().addWidget(code_text_edit)
    

    it just does not set the background, I also tried using

    self.setStyleSheet('''
    QPlainTextEdit {
        background-color: black;
        color: grey;
    }
    ''')
    

    in the constructor, also didn't work.
    I am probably stupidly missing something pretty obvious.
    Thanks for answers!

    edit
    I am very confused. The following two prints:

            print('before: ', self.height())
            self.layout().addWidget(code_text_edit)
            print('after: ', self.height())
    

    print exactly the same also when I can see that the widget expands vertically and I think that shouldn't be.
    You can test this, MRO:

    from PySide2.QtWidgets import QMainWindow, QApplication, QWidget, QVBoxLayout, QPlainTextEdit, QGraphicsView, QGraphicsScene, QGraphicsProxyWidget
    
    class CodeWidget(QWidget):
        def __init__(self):
            super(CodeWidget, self).__init__()
    
            # UI
            self.setLayout(QVBoxLayout())
    
            self.add_new_script()
    
        def add_new_script(self):
            code_text_edit = QPlainTextEdit('test code')
            code_text_edit.setStyleSheet('background: black; color: grey;')
            print('before: ', self.height())
            self.layout().addWidget(code_text_edit)
            print('after: ', self.height())
    
        def mousePressEvent(self, event):
            print('mouse pressed! adding new script')
            self.add_new_script()
    
    
    class MyView(QGraphicsView):
        def __init__(self):
            super(MyView, self).__init__()
    
    
            # create UI
            scene = QGraphicsScene(self)
            scene.setSceneRect(0, 0, 500, 500)
    
            self.setScene(scene)
    
            self.code_proxy = QGraphicsProxyWidget()
            self.code_widget = CodeWidget()
            self.code_proxy.setWidget(self.code_widget)
    
            scene.addItem(self.code_proxy)
            self.code_proxy.setPos(50, 50)
    
    
    class MainWindow(QMainWindow):
        def __init__(self):
            super(MainWindow, self).__init__()
    
            w = MyView()
    
            self.setCentralWidget(w)
    
    
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
    
        mw = MainWindow()
        mw.show()
    
        sys.exit(app.exec_())
    

    just click on the little space between text edit border and CodeWidget border. I don't know if these things are related but maybe my widget is fundamentally wrong configured.


Log in to reply