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 position is not centered
Forum Update on Monday, May 27th 2025

QLabel position is not centered

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 2.7k 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.
  • R Offline
    R Offline
    ranshalit
    wrote on last edited by ranshalit
    #1

    Hello,

    I try to position qlabel (which holds an image) in point (x,y).
    I want to the senter of the image will be in (x,y)
    but when I position qlabel with:
    label->move(x,y)
    I see that the left & up corner of the image is positioned in (x,y).
    How can make it position the center of the label in (x,y) ?
    Thanks for any advise.
    Ran

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

      Hi,

      Just do the math when dealing with that kind of stuff. You know the size of the label so if you'd like to move the centre, update the coordinates while taking into account the size of the label.

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

      R 1 Reply Last reply
      1
      • R Offline
        R Offline
        ranshalit
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          Just do the math when dealing with that kind of stuff. You know the size of the label so if you'd like to move the centre, update the coordinates while taking into account the size of the label.

          R Offline
          R Offline
          ranshalit
          wrote on last edited by
          #4

          @SGaist
          Hi,

          Thanks, that's solved it.
          Yet, I would like to ask one more thing please: on attaching an image to QLable, it seems that the size of qlabel is not the same size as the image, i.e. I need to setMinimumWidth/Height of the QLable, so seems that I am forced to use the knowledge of image size anyway inside code, Right ?

          Thanks

          J.HilkJ 1 Reply Last reply
          0
          • R ranshalit

            @SGaist
            Hi,

            Thanks, that's solved it.
            Yet, I would like to ask one more thing please: on attaching an image to QLable, it seems that the size of qlabel is not the same size as the image, i.e. I need to setMinimumWidth/Height of the QLable, so seems that I am forced to use the knowledge of image size anyway inside code, Right ?

            Thanks

            J.HilkJ Online
            J.HilkJ Online
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            @ranshalit hi, if the QLabel is not restricted in its size than it should scale itself to the Pixmap that you asgin.

            If you set the scaledContents property of the QLabel to true, it it will adjust the Pixmap to fit the QLabels current form, but it will ignore the aspect ratio.

            You can also asign the Pixmap scaled.
            label->setPixmap(p.scaled(label->size(), Qt::KeepAspectRatio);


            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.

            1 Reply Last reply
            1
            • R Offline
              R Offline
              ranshalit
              wrote on last edited by
              #6

              Not sure why but its contents is (6,16) although the image is (20,20).
              It has scaled contents (I've set with setScaledContents(true) and verified with hasScaledContents().
              Note that I am not using Pixmap, but setStyleSheet("Qlabel { image: url(: /resource/img.png); }");

              J.HilkJ 1 Reply Last reply
              0
              • R ranshalit

                Not sure why but its contents is (6,16) although the image is (20,20).
                It has scaled contents (I've set with setScaledContents(true) and verified with hasScaledContents().
                Note that I am not using Pixmap, but setStyleSheet("Qlabel { image: url(: /resource/img.png); }");

                J.HilkJ Online
                J.HilkJ Online
                J.Hilk
                Moderators
                wrote on last edited by J.Hilk
                #7

                @ranshalit If you use QStyleSheet than its a whole different matter.
                setScaledContents actually has no effect, you don't need that line and The QLabel does not adjust its size to the image.

                You'll have to call QLabel::resize() manualy.


                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.

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

                  @ranshalit If you use QStyleSheet than its a whole different matter.
                  setScaledContents actually has no effect, you don't need that line and The QLabel does not adjust its size to the image.

                  You'll have to call QLabel::resize() manualy.

                  R Offline
                  R Offline
                  ranshalit
                  wrote on last edited by
                  #8

                  @J.Hilk
                  Ok, Thanks a lot !

                  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