0

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.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
Sijith
  • 3,740
  • 17
  • 61
  • 101
  • Do you actually print the position when you resize the parent or just in the constructor once? – xander Oct 30 '17 at 12:10

1 Answers1

2

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));
ekhumoro
  • 115,249
  • 20
  • 229
  • 336