pythonuser-interfacepyqt5qtstylesheetsqmainwindow

Manipulating SVG Data in PyQt QMainWindow setStyleSheet- function


I'm working on a UI in PyQt and I want to add a background-image to the MainWindow. Adding the Picture is not the problem, but if I run my code the image is shown multiple times...

Here is a short codesnipped:

    import sys
    from PyQt5.QtCore import *
    from PyQt5.QtWidgets import *
    from PyQt5.QtGui import *

    class BackgroundIssue(QMainWindow):
        def __init__(self, parent = None):
            super().__init__(parent)

            self.setWindowTitle(f'Background Issue')
            self.setMinimumSize(1000, 800)
            self.setStyleSheet("background-image: url(max.svg);")

    if __name__ == "__main__":
        app = QApplication(sys.argv)
        window = BackgroundIssue()
        window.show()
        sys.exit(app.exec_())

You can see my output here: My Output Window

Does anyone know how to manipulate the picture inside .setStyleSheet to set it to the center of my window?


Solution

  • Okay, I think I've found a solution by myself...

    My problem was that I didn't knew about the CSS part in setStyleSheet.

    How to apply style sheet to a custom widget in PyQt

    So my previous code needs to be changed like this:

        import sys
        from PyQt5.QtCore import *
        from PyQt5.QtWidgets import *
        from PyQt5.QtGui import *
    
        class BackgroundIssue(QMainWindow):
            def __init__(self, parent = None):
                super().__init__(parent)
    
                self.setWindowTitle(f'Background Issue')
                self.setMinimumSize(1000, 800)
                self.setStyleSheet("""  background-image: url(max.png);
                                        background-repeat: no-repeat;
                                        background-position: center center;
                """)
    
        if __name__ == "__main__":
            app = QApplication(sys.argv)
            main_window = BackgroundIssue()
            main_window.show()
            sys.exit(app.exec_())
    

    And the final view:

    centered background image