Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QUiLoader and window position
Qt 6.11 is out! See what's new in the release blog

QUiLoader and window position

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 5.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    d.oberkofler
    wrote on last edited by
    #1

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

    1 Reply Last reply
    0
    • R Offline
      R Offline
      rich
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        d.oberkofler
        wrote on last edited by
        #3

        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() << ")";
        @

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #4

          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.

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • D Offline
            D Offline
            d.oberkofler
            wrote on last edited by
            #5

            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?

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on last edited by
              #6

              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.

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • D Offline
                D Offline
                d.oberkofler
                wrote on last edited by
                #7

                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?

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved