[Development] Feature request: several layouts in a single ui file

Yves Bailly yves.bailly at laposte.net
Fri Aug 30 12:16:53 CEST 2013


Greetings all,

Following given advices, I post here a question posted in the forum...

I’m talking here about widgets-based applications, although it might be relevent too for qml-based applications. And 
posting in “general” because I think it’s truely “general”, despite being inspired by mobile (Android) development.

Here’s the point. More and more the computers which run our applications can self-adjust their display according to a 
“portrait” or “landscape” orientation – tablets and mid-end or high-end phones are actual computers. An application run 
in fullscreen should adjust itself, its layout, according to this orientation. On the desktop, one can also imagine an 
application adjusting its layout according to its main window form factor.

For example, an application can show a row of buttons at the top (or bottom) in a portrait layout, but a column of 
buttons at the left (or right) in a landscape layout.

However, when a GUI is designed in QtDesigner, only one layout can be defined. If the application has to adjust itself, 
this layout needs to be manually destroyed then re-created. It would be nice if it was optionally possible to define 
more than one layout in an UI file, then have the main container be allowed (optionally) to use one layout or the other 
according to e.g. an orientation change.

Maybe this could be achieved by splitting the generated setupUi() method. Something like this:

     class Ui_My_Widget
     {
       void setupUi(QWidget* My_Widget)
       {
          /* here create the various controls and set common properties */
       }
       void setupPortrait(QWidget* My_Widget)
       {
         /* apply a layout for portrait orientation */
       }
       void setupLandscape(QWidget* My_Widget)
       {
         /* apply a layout for landscape orientation */
       }

       virtual bool hasOrientationLayouts() override
       { return true; }

       virtual bool setupOrientationLayout(QWidget* My_Widget) override
       {
         if ( My_Widget->width() > My_Widget->height() )
           this->setupLandscape();
         else
           this->setupPortrait();
       }
     };

The hasOrientationLayouts() method would override a virtual method provided in QWidget itself, which would return false 
by default. Then QWidget::resizeEvent() would call setupOrientationLayout() if hasOrientationLayouts() returns true. Of 
course setupOrientationLayout() would also be a virtual method in QWidget, doing nothing by default.

This way QtDesigner can provide two layouts an allows a GUI to automagically adjust itself, or this can be done “by 
hand” more easily.

Pushing the idea even further, some properties of some controls might be different according to the layout – e.g. some 
size policies. Or the size itself may induce some major changes, for example a small window (phone screen) may use tabs, 
while a large window (desktop or big tablet) may display everything in a single view.

Any thoughts?

Regards,

-- 
(o< | Yves Bailly                          | -o)
//\ | Linux Dijon  : http://www.coagul.org | //\
\_/ |                                      | \_/`



More information about the Development mailing list