-1

A QLayout always tries to use the whole available space of the QWidget. But, sometimes, you want it to keep it's minimal size and center itself on the widget...because you feel like it looks better (if the layout only contains items that does not make sense to be displayed bigger than their default size: QLabel, QPushButton and stuffs like that).

I had this problem hundreds of times in the past, and I'm always adding stupid QSpacerItems everywhere to fix that. I'm wondering if there could be a better solution.

I isolated the problem as an illustration:

#include <QApplication>
#include <QDialog>
#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QSpacerItem>

class MainFrame : public QDialog
{
public:
    MainFrame() : QDialog(NULL)
    {
        QHBoxLayout* theLayout = new QHBoxLayout();
        setLayout( theLayout );

        // don't want to add spacers all the time!
        //theLayout->addSpacerItem( new QSpacerItem( 10, 10, QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding ) );
        theLayout->addWidget( new QLabel( "A text", this ) );
        theLayout->addWidget( new QPushButton( "A button", this ) );
        // don't want to add spacers all the time!
        //theLayout->addSpacerItem( new QSpacerItem( 10, 10, QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding ) );
    }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainFrame w;
    w.resize(400,400);
    w.show();

    return a.exec();
}

Without the spacers, here's how it looks like:

enter image description here

I hate this display, on larger QWidget your eyes have to go all around the widget to find the relevant information.

With spacers, here's how it looks like:

enter image description here

I like it much better.

Is there a way to do this without adding the spacers? It's a pain adding them and always having to specify all those arguments (defaults won't do what you want...).

I tried to use QLayout::setSizeConstraint with no success. Does not appear to be meant to do this.

jpo38
  • 20,821
  • 10
  • 70
  • 151
  • So basically, you want your content to be of fixed size (as small as possible) and center that in the window? Depending on what else you want to show in the window, it might be better to use a layout with dynamic sizing (i.e. your first screenshot) but let the window be small per default. The user should be able to make it larger for *his convenience* if he wishes so, otherwise you end up with something like the [famous painfully unresizable windows dialogs](http://www.orclage.com/wp-content/uploads/2013/07/How-to-set-Path-and-Classpath-variables-in-Java-for-Windows.jpg) – leemes Sep 17 '14 at 11:57
  • Did you try `theLayout->addWidget( new QLabel( "A text", this ),0,Qt::AlignHCenter );` not help? – Jablonski Sep 17 '14 at 12:02
  • @leemes: Problem is when you change a QWidget content after a user action. Window was large, displaying lots of information, then, after a user action (close project or whatever) you display less information (like a welcome page, or a QLabel + 2 Open/New buttons or something like that), but the Window remains big (you don't want to change that, it could even be maximized)! Then the few controls are lost on the Window borders instead of being centered to the user's view. – jpo38 Sep 17 '14 at 12:06
  • @Chernobyl: Did not help, it centers the text of the label, but button remains too big and the controls are not centered....it's better but does not end with the display I'm expecting... – jpo38 Sep 17 '14 at 12:07

1 Answers1

4

I would do it in the following way:

[..]
theLayout->addStretch();
theLayout->addWidget( new QLabel( "A text", this ) );
theLayout->addWidget( new QPushButton( "A button", this ) );
theLayout->addStretch();
[..]
vahancho
  • 20,808
  • 3
  • 47
  • 55
  • Great! Thanks and bye bye spacers! I was hoping to have a one line fix (an attribute to be set at the QLayout level directly), but your solution is definitely acceptable. – jpo38 Sep 17 '14 at 12:16
  • 1
    Well `addStretch()` is just a convenience function way to add a spacer. So it's not quite bye bye spacers ;) – lazycoder Sep 17 '14 at 12:28