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>
    @



  • Without showing the code you use to load the widget we can't help. You should also remember that the window won't have a position unless it is shown, and that layouts won't move the child widgets until this point too.



  • 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?



  • The position is not predefined, but calculated on calling show(), as it depends on the widgets size and the screen size or the position of the parent window.



  • 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?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.