Reputation: 2153
I am trying to find the center coordinates of a widget, but self.height
and self.width
are not the widget sizes, because setting the center to the half of these draws outside of the visible area. How could I set x
and y
to the center of the widget?
class Chooser(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
x = self.width()/2
y = self.height()/2
Upvotes: 1
Views: 1893
Reputation: 120578
The centre of the widget relative to what?
Relative to itself:
c = widget.rect().center()
print(c)
Relative to its parent:
print(widget.mapToParent(c))
Relative to the screen:
print(widget.mapToGlobal(c))
NB: the last two will be the same if the widget has no parent. And see the Window Geometry overview in the Qt Docs for some other important metrics that may be relevant.
Upvotes: 2