Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. [PyQt5] KeepAspectRatio ans StyleSheet
QtWS25 Last Chance

[PyQt5] KeepAspectRatio ans StyleSheet

Scheduled Pinned Locked Moved Unsolved Qt for Python
7 Posts 3 Posters 4.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    anto1ne
    wrote on last edited by
    #1

    Hi,
    I am designing my own widget and I am looking for a way to keep the aspect ratio of an image I included using setStyleSheet.

    My widget is a QWidget, I set the background color and an image that fills the widget using setStyleSheet and border-image.

    self.setStyleSheet("height: auto;background-color: "+color+" ;border-image:url("+self.img+"); ")
    

    Then a Qlabel is set above the image using setWordWrap(True) and a QVBoxLayout.

    Thank you for your help

    jsulmJ 1 Reply Last reply
    0
    • A anto1ne

      Hi,
      I am designing my own widget and I am looking for a way to keep the aspect ratio of an image I included using setStyleSheet.

      My widget is a QWidget, I set the background color and an image that fills the widget using setStyleSheet and border-image.

      self.setStyleSheet("height: auto;background-color: "+color+" ;border-image:url("+self.img+"); ")
      

      Then a Qlabel is set above the image using setWordWrap(True) and a QVBoxLayout.

      Thank you for your help

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @anto1ne This looks like what you want to do: https://stackoverflow.com/questions/30005540/keeping-the-aspect-ratio-of-a-sub-classed-qwidget-during-resize

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • A Offline
        A Offline
        anto1ne
        wrote on last edited by
        #3

        HI @jsulm

        I am not sure this is what I am looking for.
        I am to keep the aspect ratio of the image inside the widget, not the aspect ratio of the widget.

        Here is more details about my code

        import sys
        from PyQt5.QtWidgets import QLabel, QApplication, QWidget, QVBoxLayout
        from PyQt5.QtCore import Qt
        
        class MyWidget(QWidget):
            
            def __init__(self, text, image, color):
                QWidget.__init__(self)
                
                self.setMinimumSize(100, 100)
                self.img = image
                self.setStyleSheet("height: auto;background-color: "+color+" ;border-image:url("+self.img+"); ")
                vbox = QVBoxLayout()
                self.label = QLabel(self)
                self.label.setText(text)
                self.label.setStyleSheet("font: 18pt; color: white;")
                self.label.setAlignment(Qt.AlignCenter)
                self.label.setWordWrap(True)
                vbox.addWidget(self.label)
                self.setLayout(vbox)
            
            def set_text(self,text):
                "modifier le texte"
                self.label.setText(text)
                
            def set_color(self,color):
                "change la couleur"
                self.setStyleSheet("height: auto;background-color: %s ;" + "border-image:url(%s);" %color %self.img)
                
        
        def window():
            app = QApplication(sys.argv)
            win = MyWidget("Hello","image.svg","green")
            win.show()
            sys.exit(app.exec_())
        
            
        if __name__ == '__main__':
           window()
        
        
        1 Reply Last reply
        0
        • A Offline
          A Offline
          anto1ne
          wrote on last edited by
          #4

          If you can help me, I am really stucked with this :(

          jsulmJ 1 Reply Last reply
          0
          • A anto1ne

            If you can help me, I am really stucked with this :(

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @anto1ne If you keep aspect ratio of the widget then you keep aspect ration of the image in that widget also.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • A Offline
              A Offline
              anto1ne
              wrote on last edited by
              #6

              Hi

              @jsulm , @Denni-0 : thank you for your help

              Maybe, my explanation is not clear.
              I mean that I want to keep the aspect ratio of the picture inside the widget

              Here is an example :

              When I display the widget for the first time

              1.png

              and once I resized the widget

              this is not really sexy, that is why I want the aspect ratio of the picture to remain the same

              2.png

              jsulmJ 1 Reply Last reply
              0
              • A anto1ne

                Hi

                @jsulm , @Denni-0 : thank you for your help

                Maybe, my explanation is not clear.
                I mean that I want to keep the aspect ratio of the picture inside the widget

                Here is an example :

                When I display the widget for the first time

                1.png

                and once I resized the widget

                this is not really sexy, that is why I want the aspect ratio of the picture to remain the same

                2.png

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @anto1ne Take a look at https://stackoverflow.com/questions/8211982/qt-resizing-a-qlabel-containing-a-qpixmap-while-keeping-its-aspect-ratio

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                2

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved