Results 1 to 3 of 3

Thread: Problems with QGridLayout

  1. #1
    Join Date
    Nov 2007
    Thanked 1 Time in 1 Post
    Qt products
    Unix/X11 Windows

    Default Problems with QGridLayout

    Hi community,
    I am having some issue placing 5 items in a grid layout.
    Each item is an horizontal layout composed by a QComboBox and a QLabel.

    I would like to place the items in this way:

    | H_1 | <==> | H_2 |

    | H_3 | <==> | H_4 |

    | H_5 | <=======|

    The arrows between H1,H2 and H3,H4 indicate that the items should be pushed to the extremes when the grid is resized while H5 should be pushed to the left side.

    I also set the SizeAdjustPolicy of the 5 combo boxes to the AdjustToContents flag to adjust its size fit to fit the content, but seems not having effect in my code.
    The grid layout will be then added to a vertical layout.

    Below the code snippet I wrote:

    Qt Code:
    1. QVBoxLayout *radarBiasVerticalLayout = new QVBoxLayout(stackPageRadarBias_);
    3. // The 5 horizontal layouts
    4. QHBoxLayout *timeBiasHBox = new QHBoxLayout;
    5. QHBoxLayout *rangeBiasHBox = new QHBoxLayout;
    6. QHBoxLayout *rangeGainBiasHBox = new QHBoxLayout;
    7. QHBoxLayout *azimuthBiasHBox = new QHBoxLayout;
    8. QHBoxLayout *eccentricityBiasHBox = new QHBoxLayout;
    10. // The 5 combo boxes
    11. cb_time_bias = new QComboBox; // CB1
    12. cb_time_bias->setSizeAdjustPolicy(QComboBox::AdjustToContents);
    13. cb_range_bias = new QComboBox; // CB2
    14. cb_range_bias->setSizeAdjustPolicy(QComboBox::AdjustToContents);
    15. cb_rangeGain_bias = new QComboBox; // CB3
    16. cb_rangeGain_bias->setSizeAdjustPolicy(QComboBox::AdjustToContents);
    17. cb_azimuth_bias = new QComboBox; // CB4
    18. cb_azimuth_bias->setSizeAdjustPolicy(QComboBox::AdjustToContents);
    19. cb_eccentricity_bias = new QComboBox; // CB5
    20. cb_eccentricity_bias->setSizeAdjustPolicy(QComboBox::AdjustToContents);
    22. // The 5 labels
    23. lbl_time_bias = new QLabel("Set time for all radars"); // label1
    24. lbl_range_bias = new QLabel("Set range for all radars"); // label2
    25. lbl_rangeGain_bias = new QLabel("Set range gain for all radars"); // label3
    26. lbl_azimuth_bias = new QLabel("Set azimuth for all radars"); // label4
    27. lbl_eccentricity_bias = new QLabel("Set eccentricity for all radars"); // label5
    29. timeBiasHBox->addWidget(cb_time_bias);
    30. timeBiasHBox->addWidget(lbl_time_bias);
    32. rangeBiasHBox->addWidget(cb_range_bias);
    33. rangeBiasHBox->addWidget(lbl_range_bias);
    35. rangeGainBiasHBox->addWidget(cb_rangeGain_bias);
    36. rangeGainBiasHBox->addWidget(lbl_rangeGain_bias);
    38. azimuthBiasHBox->addWidget(cb_azimuth_bias);
    39. azimuthBiasHBox->addWidget(lbl_azimuth_bias);
    41. eccentricityBiasHBox->addWidget(cb_eccentricity_bias);
    42. eccentricityBiasHBox->addWidget(lbl_eccentricity_bias);
    44. // Layout the items into the grid
    45. grid->setHorizontalSpacing(50);
    46. grid->addLayout(timeBiasHBox, 0, 0);
    47. grid->addLayout(rangeBiasHBox, 0, 1);
    48. grid->addLayout(rangeGainBiasHBox, 1, 0);
    49. grid->addLayout(azimuthBiasHBox, 1, 1);
    50. grid->addLayout(eccentricityBiasHBox, 2, 0, 2, 1, Qt::AlignLeft);
    52. // Add the grid to the vertical layout
    53. radarBiasVerticalLayout->addLayout(grid);
    To copy to clipboard, switch view to plain text mode 

    The final result of my code is the one in this picturebad.png while it should be like in this other picturegood.png
    I can not set the comboboxes to the correct size to have the correct result.

    I hope to get some help here.
    Thanx in advance,
    Franco Amato

  2. #2
    Join Date
    Jul 2008
    Thanked 73 Times in 71 Posts
    Qt products
    Qt4 Qt5
    Unix/X11 Windows

    Default Re: Problems with QGridLayout

    Hi, I think you can achieve the layout you want by playing with the horizontal stretch factors of the widgets, especially the labels.


  3. The following user says thank you to Ginsengelf for this useful post:

    franco.amato (13th November 2021)

  4. #3
    Join Date
    Jan 2008
    Alameda, CA, USA
    Thanked 863 Times in 850 Posts
    Qt products

    Default Re: Problems with QGridLayout

    I think the problem is that each of the hbox layouts scales its contents independently of all of the others, so when you put them into the grid, nothing lines up.

    I would get rid of all of the horizontal layouts and simply put each item separately into a grid cell. This will guarantee that each column of the grid resizes to the size of the largest item in each column and that all of the columns will align vertically. You can play around with the stretch factors or maximum widths for each column to get them to stretch the way you want.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. The following user says thank you to d_stranz for this useful post:

    franco.amato (13th November 2021)

Similar Threads

  1. I have problems with QListWidget and QGridLayout
    By franco.amato in forum Qt Programming
    Replies: 3
    Last Post: 30th August 2021, 07:37
  2. Having resize problems with QGridLayout
    By di_zou in forum Newbie
    Replies: 0
    Last Post: 10th December 2009, 16:49
  3. QGridLayOut
    By Rakesh_Kumar in forum Qt Programming
    Replies: 2
    Last Post: 9th January 2009, 05:49
  4. Delete a QGridLayout and New QGridLayout at runtime
    By sabeesh in forum Qt Programming
    Replies: 1
    Last Post: 5th November 2007, 14:01
  5. Qt 3.3 QGridLayout
    By ToddAtWSU in forum Qt Programming
    Replies: 2
    Last Post: 23rd February 2007, 18:40

Tags for this Thread


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.