Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QLabel with QPixmap, setScaledContents working?
QtWS25 Last Chance

QLabel with QPixmap, setScaledContents working?

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 3 Posters 10.6k 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.
  • T Offline
    T Offline
    Tink
    wrote on last edited by Tink
    #1

    Hi, i use a QPixmap of jpg image in a QLabel and have setScaledContents as True. But the image doesn't scale. Same problem if i use html <img> tag.

    Does it work for you? I'm using PyQt5, don't know if that has anything to do with it though.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Please show the code you are using.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      T 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        Please show the code you are using.

        T Offline
        T Offline
        Tink
        wrote on last edited by
        #3

        @SGaist here's some test code with a picture i found on the net:

        def show_image(self):
            # self.textEdit.setHtml('<img height="800" width="800" src="img/SGaist.png">test</img>')
        
            testL = QtWidgets.QLabel(self.textEdit)
            testP = QtGui.QPixmap('img/SGaist.png')
        
            # testP2 = testP.scaledToWidth(1000)
            # testL.setPixmap(testP2)
        
            testL.setPixmap(QtGui.QPixmap(testP))
            testL.setScaledContents(1)
            testL.show()
        
            print(testL.hasScaledContents())
            print(testL.size())
        

        The QTextEdit text content uses all available space. The testP image stays at it's original size. The testP2 image does scale. The html image also, i guess the html approach doesn't support auto scaling to 100% like regular CSS.

        SGaistS 1 Reply Last reply
        0
        • T Tink

          @SGaist here's some test code with a picture i found on the net:

          def show_image(self):
              # self.textEdit.setHtml('<img height="800" width="800" src="img/SGaist.png">test</img>')
          
              testL = QtWidgets.QLabel(self.textEdit)
              testP = QtGui.QPixmap('img/SGaist.png')
          
              # testP2 = testP.scaledToWidth(1000)
              # testL.setPixmap(testP2)
          
              testL.setPixmap(QtGui.QPixmap(testP))
              testL.setScaledContents(1)
              testL.show()
          
              print(testL.hasScaledContents())
              print(testL.size())
          

          The QTextEdit text content uses all available space. The testP image stays at it's original size. The testP2 image does scale. The html image also, i guess the html approach doesn't support auto scaling to 100% like regular CSS.

          SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Tink said in QLabel with QPixmap, setScaledContents working?:

          testL.setPixmap(QtGui.QPixmap(testP))
          testL.setScaledContents(1)
          

          I would invert these two lines. It would also be better to use testL.setScaledContents(True) as the original uses a boolean value.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          T 1 Reply Last reply
          1
          • SGaistS SGaist

            @Tink said in QLabel with QPixmap, setScaledContents working?:

            testL.setPixmap(QtGui.QPixmap(testP))
            testL.setScaledContents(1)
            

            I would invert these two lines. It would also be better to use testL.setScaledContents(True) as the original uses a boolean value.

            T Offline
            T Offline
            Tink
            wrote on last edited by
            #5

            @SGaist I've made those changes but the result is the same.

            Does it work in C++ ?

            1 Reply Last reply
            0
            • T Offline
              T Offline
              Tink
              wrote on last edited by
              #6

              Hmm... so what is weird is that when i add a label in Qt Designer, it works as expected with default settings. The difference seems to be in having another parent frame (it doesn't allow placing it in the QtextEdit). Also the relation with other frames and layouts influences it. I can't get it to work from code.

              Now i'm able to create something similar to what i had in mind by hiding and showing a couple of frames by using the QLabel from Qt Designer However it would be nice to know what is missing when doing this from code and in relation to other frames/widgets.

              Guess i can mark it as solved?

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Can you show a minimal complete python example that shows that behavior ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                T 2 Replies Last reply
                0
                • SGaistS SGaist

                  Can you show a minimal complete python example that shows that behavior ?

                  T Offline
                  T Offline
                  Tink
                  wrote on last edited by
                  #8

                  @SGaist not at this time.

                  Can you have a pixmap in a label that scales, in a textedit with PyQt?

                  1 Reply Last reply
                  0
                  • SGaistS SGaist

                    Can you show a minimal complete python example that shows that behavior ?

                    T Offline
                    T Offline
                    Tink
                    wrote on last edited by
                    #9

                    @SGaist check this:

                    import sys
                    from PyQt5.QtWidgets import *
                    from PyQt5.QtGui import *
                    
                    
                    class MainW(QMainWindow):
                    
                        def __init__(self):
                            super().__init__()
                            self.initUI()
                    
                        def initUI(self):
                            label_style = 'background-color:white; border:2 solid blue'
                    
                            f1 = QFrame()
                            f1.setFixedSize(100, 100)
                            f1.setMaximumSize(9999, 9999)
                            f1.setStyleSheet('background-color:red; border:2 solid black')
                    
                            f2 = QWidget()
                            f2.setMinimumSize(100, 100)
                            f2.setMaximumSize(9999, 9999)
                            f2.setStyleSheet('background-color:green; border:2 solid black')
                    
                            te = QTextEdit()
                            te.setMaximumSize(9999, 9999)
                            te.setStyleSheet('background-color:grey; border:2 solid black')
                    
                            img = QPixmap('small_image.jpg')
                    
                            lbl_1 = QLabel()
                            lbl_1.setStyleSheet(label_style)
                            lbl_1.setScaledContents(True)
                            lbl_1.setPixmap(img)
                    
                            lbl_2 = QLabel()
                            lbl_2.setStyleSheet(label_style)
                            lbl_2.setScaledContents(True)
                            lbl_2.setFixedWidth(900)
                            lbl_2.setPixmap(img)
                    
                            lbl_3 = QLabel()
                            lbl_3.setStyleSheet(label_style)
                            lbl_3.setScaledContents(True)
                            lbl_3.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
                            lbl_3.setPixmap(img)
                    
                            lbl_4 = QLabel()
                            lbl_4.setStyleSheet(label_style)
                            lbl_4.setScaledContents(True)
                            lbl_4.setPixmap(img)
                    
                            vbox = QVBoxLayout()
                            vbox.addWidget(f1)
                            lbl_1.setParent(f1)
                            vbox.addWidget(f2)
                            lbl_2.setParent(f2)
                            vbox.addWidget(te)
                            lbl_3.setParent(te)
                            vbox.addWidget(lbl_4)
                    
                            central_widget = QWidget()
                            central_widget.setLayout(vbox)
                            central_widget.setStyleSheet('background-color:yellow')
                            self.setCentralWidget(central_widget)
                            self.resize(1000, 1000)
                            self.show()
                    
                    
                    if __name__ == '__main__':
                        app = QApplication(sys.argv)
                        mw = MainW()
                        sys.exit(app.exec_())
                    

                    It looks like the label itself is not scaling when not directly in a layout. Or am i doing something wrong?

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      This minimal sample

                      if __name__ == '__main__':
                          app = QApplication(sys.argv)
                      
                          label = QLabel()
                          label.setScaledContents(True)
                          label.setPixmap(QPixmap("Path/to/image.extension"))
                          label.show()
                          sys.exit(app.exec_())
                      

                      will show a QLabel with the image in it, if you resize the QLabel, the pixmap will follow.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      T 1 Reply Last reply
                      0
                      • SGaistS SGaist

                        This minimal sample

                        if __name__ == '__main__':
                            app = QApplication(sys.argv)
                        
                            label = QLabel()
                            label.setScaledContents(True)
                            label.setPixmap(QPixmap("Path/to/image.extension"))
                            label.show()
                            sys.exit(app.exec_())
                        

                        will show a QLabel with the image in it, if you resize the QLabel, the pixmap will follow.

                        T Offline
                        T Offline
                        Tink
                        wrote on last edited by
                        #11

                        @SGaist yes indeed. The pixmap in your example scales just as fine as in my examples. As i said it's not that the scaling of the image is failing but that the label is not always expanding when i assumed it would do so.

                        J.HilkJ 1 Reply Last reply
                        0
                        • T Tink

                          @SGaist yes indeed. The pixmap in your example scales just as fine as in my examples. As i said it's not that the scaling of the image is failing but that the label is not always expanding when i assumed it would do so.

                          J.HilkJ Offline
                          J.HilkJ Offline
                          J.Hilk
                          Moderators
                          wrote on last edited by
                          #12

                          @Tink said in QLabel with QPixmap, setScaledContents working?:

                          @SGaist yes indeed. The pixmap in your example scales just as fine as in my examples. As i said it's not that the scaling of the image is failing but that the label is not always expanding when i assumed it would do so.

                          that is right,
                          without a layout or manual resizing, the QLable will, when a pixmap is asigned, scale itself to the content width and height of said pixmap.


                          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                          Q: What's that?
                          A: It's blue light.
                          Q: What does it do?
                          A: It turns blue.

                          T 1 Reply Last reply
                          1
                          • J.HilkJ J.Hilk

                            @Tink said in QLabel with QPixmap, setScaledContents working?:

                            @SGaist yes indeed. The pixmap in your example scales just as fine as in my examples. As i said it's not that the scaling of the image is failing but that the label is not always expanding when i assumed it would do so.

                            that is right,
                            without a layout or manual resizing, the QLable will, when a pixmap is asigned, scale itself to the content width and height of said pixmap.

                            T Offline
                            T Offline
                            Tink
                            wrote on last edited by
                            #13

                            @J.Hilk Thanks for confirming / explaining. I still have much to learn. Started with QtDesigner and got some quick results, but it's not the best way to learn how things like layouts work. And it can be confusing. E.g. in designer i can't give a textedit a layout or child widget (at least i don't know how). Well perhaps it's best for me to recreate the ui from code only and get a better understanding that way.

                            1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              Because it’s not really the purpose of QTextEdit. What exactly is your goal with it ?

                              Interested in AI ? www.idiap.ch
                              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              0
                              • T Offline
                                T Offline
                                Tink
                                wrote on last edited by
                                #15

                                Oh, this started with me trying to use insertHtml to get an auto scaled image in a QTextEdit. Well that didn't work... so i tried a Qlabel. Which i have separate at the moment. But also learned how to make it work as a child widget from code. So yeah, life is good right now.

                                1 Reply Last reply
                                0

                                • Login

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