@Agricola
Hi.
If you look for ideal elements fit, like in form in QTCreator, You don't have to use Layout.
Just put widgets on place, make it ALL proportional etc.
include class I put below, I put example too at the end
It works like a charm, if you will have to use QLayouts, make them MaxMin constraint, but results may be strange. When you use it, better forget about layouts ;)
#ifndef GEOFIT_H
#define GEOFIT_H
#include <QWidget>
#include <QApplication>
#include <QWidgetList>
#include <QScreen>
#include <QDebug>
class geofit : public QObject
{
Q_OBJECT
public:
explicit geofit(QObject* parent=0);
virtual ~geofit();
QString orientation();
QWidget* widget;
void widgetsResizer(QWidget*);
bool fixed;
QScreen* primary_screen=QApplication::primaryScreen();
#if defined(Q_OS_IOS)
QSize viewport_size=QApplication::primaryScreen()->availableVirtualSize();
#else
QSize viewport_size=QApplication::primaryScreen()->availableVirtualSize();
#endif
int sW=viewport_size.width();
int sH=viewport_size.height();
};
#endif // GEOFIT_H
SOURCE:
#include "geofit.h"
geofit::geofit(QObject* parent){
fixed=true; // if you want to have MinMax sizes setted
}
geofit::~geofit()
{
}
QString geofit::orientation()
{
Qt::ScreenOrientation orientation=QApplication::primaryScreen()->orientation();
switch (orientation) {
case Qt::PrimaryOrientation : return "Primary";
case Qt::LandscapeOrientation : return "Landscape";
case Qt::PortraitOrientation : return "Portrait";
case Qt::InvertedLandscapeOrientation : return "Inverted landscape";
case Qt::InvertedPortraitOrientation : return "Inverted portrait";
default: return "Unknown";
}
}
// INTELLIGENT RESIZE FORM TO FIT WITH KEEPING ASPECT RATIO
void geofit::widgetsResizer(QWidget* w){
int x,y,xs,ys;
w->geometry().getCoords(&x,&y,&xs,&ys);
qDebug()<<viewport_size;
qreal sFY=qreal(sH) / qreal(ys-y);
qreal sFX=qreal(sW) / qreal(xs-x);
QList<QWidget *> widgets = w->findChildren<QWidget *>();
for(int j = 0; j < widgets.count(); ++j){
widget=widgets.at(j);
if(widget!=nullptr){
if(widget->objectName()=="centralwidget"){
widget->setFixedSize(sW,sH);
widget->resize(sW,sH);
// qDebug()<<"centr:"<<sW<<sH<<widget->geometry()<<widget->geometry()<<w->sizeHint();
// w->adjustSize(); // uncomment if document is partially displayed
continue;
}
// DO NOT CHANGE ANYTHING IN NEXT 4 LINES
x=widget->x()*(sFX*10000)/10000; // left - top corner X
y=widget->y()*(sFY*10000)/10000; // Y
xs=widget->width()*(sFX*10000)/10000; // width
ys=widget->height()*(sFY*10000)/10000;
// qDebug()<<widget->objectName()<<"!!"<<widget->geometry()<<x<<y<<xs<<ys;// height
if(fixed){
widget->setFixedSize(xs,ys);
} else qInfo()<<widget->objectName()<<" :: NO FIXED POLICY";
widget->setGeometry(x,y,xs,ys);
}
}
// qDebug()<<"W1:"<<w->geometry()<<","<<w->sizeHint();
w->adjustSize();
// qDebug()<<"W2:"<<w->geometry()<<","<<w->sizeHint();
fixed=true;
}
EXAMPLE 1:
....
ui->setupUi(this);
geofit geo;
geo.widgetsResizer(this);
showFullScreen();
....
you can use it external. too:
geofit geo;
a=new a(this)
b=new b(this)
c=new c(this)
geo(a);
geo(b);
geo(c);
Good luck :)