fbence
fbence

Reputation: 2153

center coordinate of widget in pyside qtwidget

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

Answers (1)

ekhumoro
ekhumoro

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

Related Questions