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?
Forum Updated to NodeBB v4.3 + New Features

QLabel with QPixmap, setScaledContents working?

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 3 Posters 10.7k Views 2 Watching
  • 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 22 Aug 2018, 09:54 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
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 22 Aug 2018, 18:58 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 22 Aug 2018, 21:08
      0
      • S SGaist
        22 Aug 2018, 18:58

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

        T Offline
        T Offline
        Tink
        wrote on 22 Aug 2018, 21:08 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
        • S SGaist
          22 Aug 2018, 18:58

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

          T Offline
          T Offline
          Tink
          wrote on 23 Aug 2018, 18:48 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
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 23 Aug 2018, 20:34 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 24 Aug 2018, 07:52
            0
            • S SGaist
              23 Aug 2018, 20:34

              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 24 Aug 2018, 07:52 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 1 Reply Last reply 24 Aug 2018, 08:05
              0
              • T Tink
                24 Aug 2018, 07:52

                @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 Online
                J Online
                J.Hilk
                Moderators
                wrote on 24 Aug 2018, 08:05 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 25 Aug 2018, 07:49
                1
                • J J.Hilk
                  24 Aug 2018, 08:05

                  @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 25 Aug 2018, 07:49 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
                  • S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 25 Aug 2018, 08:45 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 25 Aug 2018, 09:27 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

                      15/15

                      25 Aug 2018, 09:27

                      • Login

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