How to retrieve position of a widget which resides in a scrollarea
-
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?
-
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.
-
That worked like charm, thanks a lot, appreciated!