QGridLayout Alignment problems

  • Hello,

    I try to position some labels, edits and coboboxes with a QGridLayout.

    That is how my code looks like:

      this->layoutMain = new QVBoxLayout;
      this->layoutTop = new QGridLayout;
      this->lAerodromeDeparture = new QLabel(this->sAerodromeDeparture);
      this->layoutTop->addWidget(this->lAerodromeDeparture, 0, 0, 1, 3);
      this->editAerodromeDeparture = new myQLineEdit;
      this->layoutTop->addWidget(this->editAerodromeDeparture, 1, 0, 1, 3);
      this->lCodeDeparture = new QLabel(this->sCodeDeparture);
      this->layoutTop->addWidget(this->lCodeDeparture, 0, 3, 1, 1);
      this->cbCodeDeparture = new QComboBox;
      this->cbCodeDeparture->insertItems(0, this->slAerodromeCodes);
      this->layoutTop->addWidget(this->cbCodeDeparture, 1, 3, 1, 1);
      this->lCoordsDeparture = new QLabel(this->sCoordsDeparture);
      this->layoutTop->addWidget(this->lCoordsDeparture, 2, 0, 1, 3);
      this->editCoordsDepartureV1 = new myQLineEdit;
      this->layoutTop->addWidget(this->editCoordsDepartureV1, 3, 0, 1, 1);
      this->editCoordsDepartureV2 = new myQLineEdit;
      this->layoutTop->addWidget(this->editCoordsDepartureV2, 3, 1, 1, 1);
      this->editCoordsDepartureV3 = new myQLineEdit;
      this->layoutTop->addWidget(this->editCoordsDepartureV3, 3, 2, 1, 1);
      this->editCoordsDepartureH1 = new myQLineEdit;
      this->layoutTop->addWidget(this->editCoordsDepartureH1, 4, 0, 1, 1);
      this->editCoordsDepartureH2 = new myQLineEdit;
      this->layoutTop->addWidget(this->editCoordsDepartureH2, 4, 1, 1, 1);
      this->editCoordsDepartureH3 = new myQLineEdit;
      this->layoutTop->addWidget(this->editCoordsDepartureH3, 4, 2, 1, 1);

    The problem is, that the six little LineEdits (Coords) do not have the same horizontal distance. Instead, the first block (editV1 and editH1 under it) have a larger distance to the second block (editV2 and editH2) than the seond block has to the third block (editV3 and editH3).

    I don't know what causes this behaviour. I did not change any margin settings nor any layout widths/heights.

    This is how it looks like:

    • xxxxxxxxxxxxx label xxxxxxxxxx ---- xx combobox xxx
    • edit ----------------------edit----edit-----
    • edit ----------------------edit----edit-----

    This is how it should look:

    • xxxxxxxxxxxxx label xxxxxxxxxx ---- xx combobox xxx
    • edit --------------edit--------------- edit
    • edit --------------edit--------------- edit

    Can anyone imagine what causes this unexpected behaviour?

    Thank you in anticipation!

    Kind regards,

  • Moderators

    It's because of the size of your widget and the fact you are forcing fixed widths for your QLineEdits.

    An easy fix is to add a spacer at the end of each line. This will compress the lines so the QLineEdits line up properly.

    Kind of like QHBoxLayout::addStretch().

    I am just trying to envision this in my head though it may not fully line up. But that should get you on the right track to fix your spacing issues. It's why you normally don't set fixed widths on items in Qt since everything is meant to be dynamically resized based on real estate of the widget.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.