Results 1 to 8 of 8

Thread: Alignment of widgets

  1. #1
    Join Date
    Mar 2011
    Posts
    82
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Alignment of widgets

    I'm trying to make a time picker of sorts, but I'm having massive problems trying to align everything.

    Basically, I have a

    QVBoxLayout that has 3 QHBoxLayout that contain, each, a Label and a ComboBox (except a very particular case of "date range" selection.

    I want to align all the widgets and that's just not happening, no matter what I try.

    I'm not using neither designer or creator. I'm on VS2008 and drawing every widget by hand.

    Any help on this issue, as well as required "extra" info such as code snipets, please feel free to request.

    Qt Code:
    1. TimeRangeSelector::TimeRangeSelector(QWidget *parent)
    2. {
    3. //Main level
    4. Qt::AlignRight;
    5. m_pMainLayout = new QVBoxLayout();
    6. itemCont->setLayout(m_pMainLayout);
    7. itemCont->setMinimumSize(600, 400);
    8.  
    9. //First Level
    10. QHBoxLayout * pHBox1 = new QHBoxLayout();
    11. m_pMainLayout->setContentsMargins(0,0,5,5);
    12. m_pMainLayout->addLayout(pHBox1);
    13. QVRLabel * phBox1_pLabel1 = new QVRLabel("Period: ");
    14. phBox1_pLabel1->setAlignment(Qt::AlignHCenter);
    15. m_phBox1_pComboBox1 = new QVRComboBox();
    16.  
    17. m_phBox1_pComboBox1->setMinimumHeight(30);
    18. m_phBox1_pComboBox1->setMaximumWidth(100);
    19. m_phBox1_pComboBox1->addItem(" Hour");
    20. m_phBox1_pComboBox1->addItem(" Day");
    21. m_phBox1_pComboBox1->addItem(" Week");
    22. m_phBox1_pComboBox1->addItem(" Month");
    23. m_phBox1_pComboBox1->addItem(" Year");
    24. m_phBox1_pComboBox1->addItem(" Date Range");
    25. m_phBox1_pComboBox1->addItem(" Historical");
    26. pHBox1->addWidget(phBox1_pLabel1);
    27. pHBox1->addWidget(m_phBox1_pComboBox1);
    28.  
    29. //Second Level
    30. QHBoxLayout * pHBox2 = new QHBoxLayout();
    31. m_pMainLayout->addLayout(pHBox2);
    32. m_widget1 = new QStackedWidget();
    33. m_widget2 = new QStackedWidget();
    34. QStackedWidget * m_dateRangeFromAux = new QStackedWidget();
    35. m_widget1->setMaximumHeight(30);
    36. m_widget2->setMaximumHeight(30);
    37. m_dateRangeFromAux->setMaximumHeight(50);
    38. generateSecondLevelWidgets(pHBox2, m_widget1, m_widget2, "Hours");
    39. generateSecondLevelWidgets(pHBox2, m_widget1, m_widget2, "Days");
    40. generateSecondLevelWidgets(pHBox2, m_widget1, m_widget2, "Weeks");
    41. generateSecondLevelWidgets(pHBox2, m_widget1, m_widget2, "Months");
    42. generateSecondLevelWidgets(pHBox2, m_widget1, m_widget2, "Years");
    43. QVRLabel * label = new QVRLabel(" From:");
    44. QLineEdit * date = new QLineEdit("MM / DD / YYYY");
    45. QVRTimePicker * timepicker = new QVRTimePicker();
    46. m_widget1->addWidget(label);
    47. m_widget2->addWidget(date);
    48. m_dateRangeFromAux->addWidget(timepicker);
    49. pHBox2->addWidget(m_widget1);
    50. pHBox2->addWidget(m_widget2);
    51. pHBox2->addWidget(m_dateRangeFromAux);
    52.  
    53. // Third Level
    54. QHBoxLayout * pHBox3 = new QHBoxLayout();
    55. m_pMainLayout->addLayout(pHBox3);
    56. m_widget3 = new QStackedWidget();
    57. m_widget4 = new QStackedWidget();
    58. m_widget3->setMaximumHeight(30);
    59. m_widget4->setMaximumHeight(30);
    60. generateThirdLevelWidgets(pHBox3, m_widget3, m_widget4, "Hours");
    61. generateThirdLevelWidgets(pHBox3, m_widget3, m_widget4, "Days");
    62. generateThirdLevelWidgets(pHBox3, m_widget3, m_widget4, "Weeks");
    63. generateThirdLevelWidgets(pHBox3, m_widget3, m_widget4, "Months");
    64. generateThirdLevelWidgets(pHBox3, m_widget3, m_widget4, "Years");
    65. m_widget3->hide();
    66. m_widget4->hide();
    To copy to clipboard, switch view to plain text mode 

    I'm also attaching an IMG of "What I got" vs "What I expected":

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Alignment of widgets

    Why don't you use QGridLayout with two rows and six columns and make the combobox span columns 1-3?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2011
    Posts
    82
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Alignment of widgets

    Honestly? Because it never ocurred to me, haha.

    Now I'm having this particular issue;

    welpe.png

    And it's bugging me crazy.

    Basically I'm doing this;

    Qt Code:
    1. //First Level
    2. m_pPeriodLabel = new QVRLabel("Period: ");
    3. m_pPeriodComboBox = new QVRComboBox();
    4. m_pPeriodComboBox->setMinimumHeight(30);
    5. m_pPeriodComboBox->setMaximumWidth(100);
    6. m_pPeriodComboBox->addItem(" Hour");
    7. m_pPeriodComboBox->addItem(" Day");
    8. m_pPeriodComboBox->addItem(" Week");
    9. m_pPeriodComboBox->addItem(" Month");
    10. m_pPeriodComboBox->addItem(" Year");
    11. m_pPeriodComboBox->addItem(" Date Range");
    12. m_pPeriodComboBox->addItem(" Historical");
    13. m_pMainLayout->addWidget(m_pPeriodLabel, 0, 0);
    14. m_pMainLayout->addWidget(m_pPeriodComboBox, 0, 1);
    To copy to clipboard, switch view to plain text mode 

    and in the OP "generateSecondLevelWidgets" function, now I do this:
    Qt Code:
    1. layout->addWidget(widget1, 1, 0);
    2. layout->addWidget(widget2, 1, 1);
    To copy to clipboard, switch view to plain text mode 
    Layout is the name for the m_pMainLayout pointer instance within that function (it's the same layout)


    EDIT: Can I align a whole Colum to, say, the right?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Alignment of widgets

    If you give me "I have this" and "I want this" images, I will try to help you, otherwise I have no idea what you want.

    For aligning widgets you can use QLayout::setAlignment().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Mar 2011
    Posts
    82
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Alignment of widgets

    I want to "normalize" the behaviour.
    welp.png

    Either result is fine, and would be understandable. However my current situation is the previously posted one (range: being so much more to the left than everything else)

    Basically, I'm trying to set the upper label to the (0,0) grid in the layout. The upper ComboBox to the (0, 1), the bottom Label to (1, 0) and bottom combobox to (1, 1).

    On further inspection, I THINK the problem is actually the first row. I base this on the fact that adding more rows to the layout actuallyt aligns the second, third and so on "properly" (they are aligned. Period) while the First label remains "isolated" from the rest.

  6. #6
    Join Date
    Mar 2011
    Posts
    82
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Alignment of widgets

    So ... no idea on what I might be doing wrong?

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Alignment of widgets

    Can you provide a minimal compilable example reproducing the problem?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Mar 2011
    Posts
    82
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Alignment of widgets

    I solved it. It seems I've been resizing every single cell (to the wanted value) except for the very first one. I mistook resizing each cell for a more "general" property of the QGridLayout.

Similar Threads

  1. Replies: 2
    Last Post: 30th March 2011, 21:20
  2. Replies: 5
    Last Post: 19th April 2010, 00:31
  3. Help with alignment
    By IsleWitch in forum Newbie
    Replies: 1
    Last Post: 20th October 2007, 19:20
  4. Regarding alignment in QTable
    By joseph in forum Qt Programming
    Replies: 4
    Last Post: 5th December 2006, 11:35
  5. How to set the alignment in a TableView?
    By vratojr in forum Qt Programming
    Replies: 1
    Last Post: 9th March 2006, 14:21

Bookmarks

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.