PDA

View Full Version : customizing widget layout: best practice



stillwaiting
23rd December 2010, 10:04
Dear friends,

I created UI for widget using QtDesigner, and now I want to customize it a little bit before showing to the user. So I'm trying to do this in the widget's constructor:


public:
MyWidget(QWidget *_parent) : QWidget(_parent)
{
ui.setupUi(this);

....
// For example: let's move the checkbox
QPoint p1 = ui.checkBox1->mapToGlobal(QPoint(0, 0));
QPoint p2 = ui.checkBox2->mapToGlobal(QPoint(0, 0));
ui.checkBox->setStyleSheet(QString("margin-left:%1px;").arg(p1.x()-p2.x())); // oops
...
}


But here I face the problem: p1 and p2 are just the same.

So, I'm wondering:

1. Why they are the same?

2. Is it possible to perform such customization in widget's constructor? I know that "QTimer::singleShot(0, this, SLOT(moveMyCheckbox() ))" will solve this, but I'm not sure if it is not a "dirty hack".

3. What is the best way to do such things?

wysota
23rd December 2010, 10:13
1. Why they are the same?
Because the position of a widget without an explicitly set geometry is not determined until the widget is shown for the first time.


2. Is it possible to perform such customization in widget's constructor? I know that "QTimer::singleShot(0, this, SLOT(moveMyCheckbox() ))" will solve this, but I'm not sure if it is not a "dirty hack".
I think what you are trying to do in general is a dirty hack. What exactly is the purpose for trying to set the stylesheet?

stillwaiting
23rd December 2010, 13:58
Hi wysota and thank you for the reply.


What exactly is the purpose for trying to set the stylesheet?

I have groupbox with checkboxes in it and another one checkbox below. And I'm trying to put all checkboxes (include the one which is outside) on the same vertical. Screenshot is attached.

But actually I'm wondering what is the best way to do such kind of things. When I have to do something while constructing the widget, but to do this I must know layouts positions/sizes/child object's positions/sizes etc. This is just a particular case.

Lykurg
23rd December 2010, 14:04
for that better put the single check box also in a box where you set the title "" and use the flat option. If that does not work for you put it in a frame or widget or even only in a layout where you manipulate the left spacing.

stillwaiting
23rd December 2010, 14:49
for that better put the single check box also in a box where you set the title "" and use the flat option

Hmm... sounds like a good idea, thank you :) But can I be sure that the "flat" QGroupBox has the same indents as the "not flat" one?


or even only in a layout where you manipulate the left spacing.

As I understand the situation: groupbox border thickness could be different on different platforms and/or could vary depending on OS settings. So, I think, that constant left margin here could play a bad joke.

wysota
23rd December 2010, 23:30
As I understand the situation: groupbox border thickness could be different on different platforms and/or could vary depending on OS settings. So, I think, that constant left margin here could play a bad joke.

I'd say trying to align the checkboxes is as a bad idea as setting a constant left margin, which is what your stylesheet is trying to do anyway. If the OS style says checkboxes in groups and out of groups should be aligned, they will be aligned. If it says there is no such need, they won't be aligned so don't try and force that. I'm usually against "I know best what is good for my users" approach and I don't see why this situation would differ.