Set (individual) size for widgets in gridlayout
-
I have a QGridLayout with multiple labels I want to show images in. It should look like this:
So far I didn't find a convenient way to set the different label sizes. I tried to use the addWidget method and specified the rows and columns the labels should be spanning.grid->addWidget(labelLeft, 0, 0, 3, 4); grid->addWidget(labelMid, 0, 5, 10, 6); grid->addWidget(labelRight, 0, 11, 3, 4); grid->addWidget(labelBot, 11, 6, 3, 4);
I thought in this example the vertical screen place would be split in 13 rows with equal height, of which the big label should be using 10 and the bottom one 3. That however is not the case; the big label merely uses a little more than half the vertical space.
How to proceed here? Can someone point me in the right direction? I don't have to use a gridlayout just thought it was the most intuitive. I want to later use the free space in the bottom corners to display different data.
-
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 :)