Important: Please read the Qt Code of Conduct -

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->setGeometry(0, 530, 225, 20);

    RefreshTimeLabel = new QLabel(this);
    RefreshTimeLabel->setGeometry(225, 530, 225, 20);
    RefreshTimeLabel->setText("Time between Refreshs:");
    StartButton = new QPushButton(this);
    StartButton->setGeometry(0, 580, 183, 20);
    StopButton = new QPushButton(this);
    StopButton->setGeometry(183, 580, 184, 20);
    ResetButton = new QPushButton(this);
    ResetButton->setGeometry(367, 580, 183, 20);

    A screenshot:


    the window itself is 550px wide, so normaly it should work.

  • Moderators

    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);

    RefreshTimeLabel = new QLabel("Time between Refreshs:", this);
    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);

    and the result (with red borders to see better): ! 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.

  • Moderators

    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 :)

  • yeah, but that 500500px grid is no widget, at least not in its current state.

    But i guess i could make it into one by painting into a seperate widget and adding that to the layout.

  • Moderators

    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.

  • yea, got it working, even though i had to find out how the heck to draw in a child Widget without making it into an own class... (need an array from the main class to control the drawing)

Log in to reply