Label Positioning is off
-
Hi :)
I wanted to have 2 labels, each for half the width of the window with text centered. Below that 3 buttons, each a third of the screens. The Buttons work, but the text is kinda of...
Code is:
@ StartPopulationLabel = new QLabel(this);
StartPopulationLabel->setAlignment(Qt::AlignCenter);
StartPopulationLabel->setGeometry(0, 530, 225, 20);
StartPopulationLabel->setText("Populaton:");RefreshTimeLabel = new QLabel(this); RefreshTimeLabel->setAlignment(Qt::AlignCenter); RefreshTimeLabel->setGeometry(225, 530, 225, 20); RefreshTimeLabel->setText("Time between Refreshs:"); StartButton = new QPushButton(this); StartButton->setGeometry(0, 580, 183, 20); StartButton->setText("Start"); StopButton = new QPushButton(this); StopButton->setGeometry(183, 580, 184, 20); StopButton->setText("Stop"); ResetButton = new QPushButton(this); ResetButton->setGeometry(367, 580, 183, 20); ResetButton->setText("Reset");@
A screenshot:
!http://s14.directupload.net/images/130205/v7wsplbq.png(Screenshot)!
the window itself is 550px wide, so normaly it should work.
-
Why are you calculating those positions by hand in the first place? Imagine some poor fella will have to add another label to that code someday. Ouch. Qt has layouts for that.
This code does what you need:
@
StartPopulationLabel = new QLabel("Populaton:", this);
StartPopulationLabel->setAlignment(Qt::AlignCenter);RefreshTimeLabel = new QLabel("Time between Refreshs:", this); RefreshTimeLabel->setAlignment(Qt::AlignCenter); StartButton = new QPushButton("Start", this); StopButton = new QPushButton("Stop", this); ResetButton = new QPushButton("Reset", this); QGridLayout* layout = new QGridLayout(); layout->addWidget(StartPopulationLabel, 0, 0, 1, 3); layout->addWidget(RefreshTimeLabel, 0, 3, 1, 3); layout->addWidget(StartButton, 1, 0, 1, 2); layout->addWidget(StopButton, 1, 2, 1, 2); layout->addWidget(ResetButton, 1, 4, 1, 2); yourParentWidget->setLayout(layout);
@
and the result (with red borders to see better): !http://img339.imageshack.us/img339/108/layoutsp.jpg(layout example)! -
Problem with layouts( or at least because i haven't used layouts before and because of that no idea how to use them):
above those labels i have a 500px*500px area where i want to paint a grid(this whole thing is going to become a GameofLife), and i haven't found a way to position a layout starting at a fixed position.
-
That's no problem, just use this:
@
layout->addWidget(Your500x500Widget, 0, 0, 1, 6);layout->addWidget(StartPopulationLabel, 1, 0, 1, 3);
layout->addWidget(RefreshTimeLabel, 1, 3, 1, 3);
layout->addWidget(StartButton, 2, 0, 1, 2);
layout->addWidget(StopButton, 2, 2, 1, 2);
layout->addWidget(ResetButton, 2, 4, 1, 2);
@
A tip: learn to use layouts. They are what every UI programmer should know and use. They take care of proper sizing, spacing and handle window resizing. Thanks to them you can concentrate on what your app is doing instead of calculating +/- 1px shifting of 200 ui elements when you decide to add a border somewhere :) -
For the sake of readability and modularity you should probably do it anyway. But if you just want some extra space, layouts have flexible margins. Just use layout->setContentsMargins(9,500,9,9) and you'll get a space for the drawings.