Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. How to retrieve position of a widget which resides in a scrollarea
Qt 6.12 LTS is out! See what's new in the release blog

How to retrieve position of a widget which resides in a scrollarea

Scheduled Pinned Locked Moved Solved Qt for Python
3 Posts 2 Posters 1.3k Views 1 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.
  • P Offline
    P Offline
    proz goret
    wrote on last edited by
    #1

    I have a basic application where there is a scrollarea full of values such as 0.15, 0.16, 0.17, 0.18 etc.
    You can swipe through the scrollarea using QScroller. I want to use the setSnapPositionX (a function of QScroller class) in order to create snap points on the values, indicating the current value . The function needs a list of points to create snap points. So I want to retrieve the positions of the QLabels inside of the scrollarea(from what I understood, position inside of a scrollarea works sort of different than usual geometry of the widgets) and after retrieving them, I want to set the snap points to those points. The only problem here is I cannot get the positions of the QLabels. When I create a QLabel from the Qt Designer and instantiate it in the code using findChild(), I am able to retrieve the position info of the label, thus, when I compare it using QScroller.finalPosition() function, it is nearly accurate. But I generate the QLabels from the code and when I do so, I could not accomplish to get the widget's position. Here is what I tried:

    for i in range(rangeStart,rangeEnd):
                self.lb1 = QLabel("0.{}  ".format(i))
                self.hl.addWidget(self.lb1)
                self.lb1.setObjectName("lb{}".format(i))
                pList.append(self.lb1)
            for widget in pList:
                print(widget.x())
    

    What I basically do here is creating a label, then adding it to the horizontalLayout then also adding it to a list. Lastly, printing the positions of the labels using a for loop. But it gives only zeros. What am I doing wrong?

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

      Hi and welcome to devnet,

      You are retrieving the information at the wrong time. Until shown, widgets do not have any "physical presence" hence the values you get.

      You should trigger this calculation in the show event of that widget.

      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
      1
      • P Offline
        P Offline
        proz goret
        wrote on last edited by
        #3

        That worked like charm, thanks a lot, appreciated!

        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
        • Unsolved