How to get the position of a widget in pyqt5, while using a layout method

2.3k Views Asked by At

How do i get a PyQt5 widgets coordinates relative to it's parent widget? Specifically when using layout method, in my case i am using the QGridLayout layout method.

I have tried to get the widgets coordinates using the .x() and .y() methods. But both of these methods both return 0, I believe this is because i am using a layout method and that point is referring to the starting corner of the widget, which does not help in my case.

I have also tried using the mapToGlobal() and mapToParent() methods, I Do not believe I am using them correctly though,here are some examples of ways I have tried to use these two methods.

map_it = self.Play_label.mapFromParent(QPoint(1,1))
print ("Label coord: ", map_it)

map_it = self.Play_label.mapToParent(QPoint(1,1))
print ("Label coord: ", map_it)

I have also tried adding this method to the widget itself, in this case i have a class widget with the name Play_label, and i make a method in this class that prints out the mapToParent method. Below i will show an example.

def map_to_parent(self):
    print ("From Widget Coord: ", self.mapFromParent(QPoint(0,0)))

But these all return whatever i put as the QPoint, for example the first two examples will print out PyQt5.QtCore.QPoint(1,1). While the last example will print out PyQt5.QtCore.QPoint(). Why is this am I not using these methods correctly?

Basically what I am trying to do is use the QPropertyAnimation to change the position of the QWidget that i created, which is a QLabel that i set the Pixmap of plus various other functions. Normally you would change the geometry to change the position of a widget, but this does not work in my case of course because I am using a layout method. Here is an example of that code.

self.animation.setDuration(3000)
self.animation.setStartValue(QRect(0,0,200,200))
self.animation.setEndValue(QRect(width_mul, height_mul, 150, 150))
#print ("Value: ", self.animation.currentValue())
#self.animation.valueChanged.connect(self.Play_label.changeImageSize)
#self.animation.setTargetObject(self.Play_label)
self.animation.start()

If someone could please explain to me how to get the coordinates of a widget from the parent widget when using a layout method that would be huge help, or maybe i am using the QPropertyAnimation() method wrong i am really not sure, again all i am trying to do is change the position of the widget using the QPropertyAnimation, I want it to come in from the side and move to where the widget is normally positioned, which i need the coordinates of the Widget for or some other way to move it, any advice will be greatly appreciated, thank you in advanced!

0

There are 0 best solutions below