PDA

View Full Version : Alignment of widgets



alitoh
8th August 2011, 16:29
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.



TimeRangeSelector::TimeRangeSelector(QWidget *parent)
{
//Main level
Qt::AlignRight;
m_pMainLayout = new QVBoxLayout();
itemCont->setLayout(m_pMainLayout);
itemCont->setMinimumSize(600, 400);

//First Level
QHBoxLayout * pHBox1 = new QHBoxLayout();
m_pMainLayout->setContentsMargins(0,0,5,5);
m_pMainLayout->addLayout(pHBox1);
QVRLabel * phBox1_pLabel1 = new QVRLabel("Period: ");
phBox1_pLabel1->setAlignment(Qt::AlignHCenter);
m_phBox1_pComboBox1 = new QVRComboBox();

m_phBox1_pComboBox1->setMinimumHeight(30);
m_phBox1_pComboBox1->setMaximumWidth(100);
m_phBox1_pComboBox1->addItem(" Hour");
m_phBox1_pComboBox1->addItem(" Day");
m_phBox1_pComboBox1->addItem(" Week");
m_phBox1_pComboBox1->addItem(" Month");
m_phBox1_pComboBox1->addItem(" Year");
m_phBox1_pComboBox1->addItem(" Date Range");
m_phBox1_pComboBox1->addItem(" Historical");
pHBox1->addWidget(phBox1_pLabel1);
pHBox1->addWidget(m_phBox1_pComboBox1);

//Second Level
QHBoxLayout * pHBox2 = new QHBoxLayout();
m_pMainLayout->addLayout(pHBox2);
m_widget1 = new QStackedWidget();
m_widget2 = new QStackedWidget();
QStackedWidget * m_dateRangeFromAux = new QStackedWidget();
m_widget1->setMaximumHeight(30);
m_widget2->setMaximumHeight(30);
m_dateRangeFromAux->setMaximumHeight(50);
generateSecondLevelWidgets(pHBox2, m_widget1, m_widget2, "Hours");
generateSecondLevelWidgets(pHBox2, m_widget1, m_widget2, "Days");
generateSecondLevelWidgets(pHBox2, m_widget1, m_widget2, "Weeks");
generateSecondLevelWidgets(pHBox2, m_widget1, m_widget2, "Months");
generateSecondLevelWidgets(pHBox2, m_widget1, m_widget2, "Years");
QVRLabel * label = new QVRLabel(" From:");
QLineEdit * date = new QLineEdit("MM / DD / YYYY");
QVRTimePicker * timepicker = new QVRTimePicker();
m_widget1->addWidget(label);
m_widget2->addWidget(date);
m_dateRangeFromAux->addWidget(timepicker);
pHBox2->addWidget(m_widget1);
pHBox2->addWidget(m_widget2);
pHBox2->addWidget(m_dateRangeFromAux);

// Third Level
QHBoxLayout * pHBox3 = new QHBoxLayout();
m_pMainLayout->addLayout(pHBox3);
m_widget3 = new QStackedWidget();
m_widget4 = new QStackedWidget();
m_widget3->setMaximumHeight(30);
m_widget4->setMaximumHeight(30);
generateThirdLevelWidgets(pHBox3, m_widget3, m_widget4, "Hours");
generateThirdLevelWidgets(pHBox3, m_widget3, m_widget4, "Days");
generateThirdLevelWidgets(pHBox3, m_widget3, m_widget4, "Weeks");
generateThirdLevelWidgets(pHBox3, m_widget3, m_widget4, "Months");
generateThirdLevelWidgets(pHBox3, m_widget3, m_widget4, "Years");
m_widget3->hide();
m_widget4->hide();

I'm also attaching an IMG of "What I got" vs "What I expected":
http://img10.imageshack.us/img10/3217/welpj.png

wysota
8th August 2011, 17:24
Why don't you use QGridLayout with two rows and six columns and make the combobox span columns 1-3?

alitoh
8th August 2011, 20:46
Honestly? Because it never ocurred to me, haha.

Now I'm having this particular issue;

6749

And it's bugging me crazy.

Basically I'm doing this;


//First Level
m_pPeriodLabel = new QVRLabel("Period: ");
m_pPeriodComboBox = new QVRComboBox();
m_pPeriodComboBox->setMinimumHeight(30);
m_pPeriodComboBox->setMaximumWidth(100);
m_pPeriodComboBox->addItem(" Hour");
m_pPeriodComboBox->addItem(" Day");
m_pPeriodComboBox->addItem(" Week");
m_pPeriodComboBox->addItem(" Month");
m_pPeriodComboBox->addItem(" Year");
m_pPeriodComboBox->addItem(" Date Range");
m_pPeriodComboBox->addItem(" Historical");
m_pMainLayout->addWidget(m_pPeriodLabel, 0, 0);
m_pMainLayout->addWidget(m_pPeriodComboBox, 0, 1);

and in the OP "generateSecondLevelWidgets" function, now I do this:

layout->addWidget(widget1, 1, 0);
layout->addWidget(widget2, 1, 1);
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?

wysota
8th August 2011, 21:04
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().

alitoh
8th August 2011, 21:26
I want to "normalize" the behaviour.
6750

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.

alitoh
9th August 2011, 13:47
So ... no idea on what I might be doing wrong?

wysota
9th August 2011, 13:57
Can you provide a minimal compilable example reproducing the problem?

alitoh
9th August 2011, 20:33
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.