Sijith
Sijith

Reputation: 3956

Position of a QToolButton does not change when resizing or moving the parent widget

I have a class called Titlebar inherited from QWidget. The following code goes inside the constructor of Titlebar class:

m_queueBtn = new QToolButton;
m_serverToolBar = new QToolBar;
m_serverToolBar->addWidget(m_queueBtn);

QPoint pos = m_queueBtn->pos();

While printing m_queueBtn->pos(), it's always showing the same value instead of the resize or move.

Upvotes: 0

Views: 285

Answers (1)

ekhumoro
ekhumoro

Reputation: 120818

The size-policy of a QToolButton is a Fixed/Fixed by default, so resizing its parent will have no effect. Also, pos() returns coordinates that are relative to its parent widget - so again, moving the parent will have no effect.

If you want to get the global position of a child widget (i.e. relative to the desktop), you can use mapToGlobal:

QPoint pos = m_queueBtn->mapToGlobal(m_queueBtn->pos());

Or to translate child coordinates to a position relative to one of its ancestor widgets, you can use mapTo:

QPoint pos = m_queueBtn->mapTo(ancestor, QPoint(0, 0));

Upvotes: 2

Related Questions