QUiLoader and window position
-
I'm using QUiLoader to dynamically load (and modify) UI resources.
Everything works fine except for the position of the actual window.
The and x and y coordinates are always returned as 0 and I cannot understand why.In the following example I get x=0 and y=0 instead of 100:
@<ui version="4.0" >
<author></author>
<comment></comment>
<exportmacro></exportmacro>
<class>Dialog</class>
<widget class="QDialog" name="Dialog" >
<property name="geometry" >
<rect>
<x>100</x>
<y>100</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle" >
<string>Dialog</string>
</property>
<widget class="QDialogButtonBox" name="buttonBox" >
<property name="geometry" >
<rect>
<x>290</x>
<y>20</y>
<width>81</width>
<height>241</height>
</rect>
</property>
<property name="orientation" >
<enum>Qt::Vertical</enum>
</property>
<property name="standardButtons" >
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</widget>
<pixmapfunction></pixmapfunction>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>Dialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel" >
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel" >
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>Dialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel" >
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel" >
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>
@ -
This is the Qt testcase i have used to isolate the problem and pos is always returned as (0,0):
@
QFile aFile(":/dialog.ui");
bool aOK = aFile.open(QIODevice::ReadOnly | QIODevice::Text);
Q_ASSERT(aOK);QWidget* aParentWidget = new QWidget(0);
Q_ASSERT(aParentWidget);QUiLoader aLoader;
QWidget* aWidget = aLoader.load(&aFile, aParentWidget);
Q_ASSERT(aWidget);qDebug() << aWidget;
qDebug() << "title=(" << aWidget->windowTitle() << ")";
qDebug() << "pos=(" << aWidget->pos() << ")";
qDebug() << "frameSize=(" << aWidget->frameSize() << ")";
@ -
You must show the widget first to get a valid position. Rich already mentioned this.
Insert in line 11:
@
aWidget->show();
@Be aware that widgets without a parent are shown centered relative to the screen, and a widget with parent centered relative to its parent.
-
I understand but this is exactly my problem.
I would need to get the predefined position before showing the widget. In my use case I actually never show it but rather create anther one based on the first one.
Is there any workaround? -
So I guess the only workaround to get the actual position from the UI File would be to "manually" parse it the XML representation. Am I right?