PDA

View Full Version : Problem in layout widgets in a grid



franco.amato
19th November 2010, 23:21
Hi to all,
layouts don't want enter in my mind.
I would layout 4 widgets in a grid ( 2 x 2 ), 2 QLabels and 2 QLeds.
I have some problems with the space between widgets and w and h of the layout.
I attach a screenshot 5501.
Basically I layout some buttons horizontally having an height, then I add the gridlayout to the horizontally layout. The problem is that the height of the grid is bigger that the height of the buttons and is not what I would achieve. I would the gridlayout to be height as the buttons and also the horizontal space between widget is too big.
I tried with setHorizontalSpacing, setSpacing without success.
What can I do?
Regards

franco.amato
20th November 2010, 01:40
any reply please?

franco.amato
20th November 2010, 19:53
Well seems an unsolvable problem :(.
I hope to get a reply.
Best

wysota
20th November 2010, 21:52
Two rows of labels with this font are larger than the button. If you want them to fit, you need to decrease their size (and possibly the size of the other two widgets as well). You can also manipulate the spacing and margins of the layout but it won't do much without decreasing sizes of the widgets that are part of the layout.

franco.amato
22nd November 2010, 17:02
Two rows of labels with this font are larger than the button. If you want them to fit, you need to decrease their size (and possibly the size of the other two widgets as well). You can also manipulate the spacing and margins of the layout but it won't do much without decreasing sizes of the widgets that are part of the layout.

Hi Wysota,
I followed your suggestion and moved the grid layout on the left but I still have a space that I can not remove.
I attach this screenshot 5525 showing the space I would remove.

I post some code:


m_focusLed = new QLed(this); // I create a QLed
m_focusLed->setFixedSize(13,13);// set its size to 13, 13
m_encodingLed = new QLed(this); // the same for the other led
m_encodingLed->setFixedSize(13,13);

QLabel* activeLabel = new QLabel(tr("A")); // create a QLabel
activeLabel->setFixedSize(13,10); // set the size
QLabel* encodingLabel = new QLabel(tr("E"));
encodingLabel->setFixedSize(13, 10);

QGridLayout* gl = new QGridLayout(); // create the grid and add the widgets
gl->setAlignment(Qt::AlignLeft);
gl->setContentsMargins(0, 0, 0, 0);
gl->addWidget(activeLabel, 0, 0);
gl->addWidget(m_focusLed, 0, 1);
gl->addWidget(encodingLabel, 1, 0);
gl->addWidget(m_encodingLed, 1, 1);

then I insert the grid layout in horizontal layout so:


/* main layout */
QHBoxLayout* lo = new QHBoxLayout();
lo->setSpacing(0);
lo->addLayout(gl); // <--add the grid layout
/* add more widgets ( buttons ) */
//...

Doing so I got such space and I would delete it.
I hope yo get help.

Best Regards

wysota
22nd November 2010, 17:05
I did not suggest to move the layout anywhere.

franco.amato
22nd November 2010, 17:18
I did not suggest to move the layout anywhere.

Yes I know, you suggested to me to decrease the size, and I did it. I did what you suggested AND I moved the layout on the left size

wysota
22nd November 2010, 18:06
I don't see where exactly you decreased the font size. And your two leds plus the spacing between them are surely larger than the button that determines the height of the row.

franco.amato
22nd November 2010, 18:16
I don't see where exactly you decreased the font size. And your two leds plus the spacing between them are surely larger than the button that determines the height of the row.

Hi Wysota,
the space I would remove is the horizontal space as indicated in the last screenshot I attached. It's located on the right of the leds.

franco.amato
23rd November 2010, 13:33
Hi Wysota,
the space I would remove is the horizontal space as indicated in the last screenshot I attached. It's located on the right of the leds.
So how can I remove such space?
regards

wysota
23rd November 2010, 14:50
I have no idea what you want. You show pictures with some things and write about other things.

franco.amato
23rd November 2010, 16:30
I have no idea what you want. You show pictures with some things and write about other things.

Wysota the last screenshot I attached show the problem.
I attach it again 5526

Regards

wysota
23rd November 2010, 16:41
Is the space caused by the layout or by the widgets in the layout?

franco.amato
23rd November 2010, 16:54
Is the space caused by the layout or by the widgets in the layout?

Wysota I think by the layout, as I set the size of the widgets in the layout

nroberts
23rd November 2010, 17:28
...the horizontal space between widget is too big.
I tried with setHorizontalSpacing, setSpacing without success.

I'm new to Qt so don't know how Qt does it, but every reasonably expressive UI API and sizer system allows you to tell an area within the sizer not to grow. I can't be sure, but my bet is that your grid is being expanded to fill the space given to it by the sizer it is within. You may need to tell THAT sizer that it shouldn't be giving any free space to your grid (or you might have to tell the grid not to take any, depending on how Qt does it). Then the space should be absorbed by a different widget.

Simply setting the size may very well be insufficient for this need. Sizers/layouts/whatever tend to give out space according to how THEY have been told to, letting the widgets given that space to resize as they desire, and then *positioning them within the space given* according to whatever rules have been dictated/defaulted.

It's just a guess, but it's a reasonable one and you're not getting any other answers...so there you go...

BTW, I tend to get quicker, better, possibly ANY answers at stackoverflow.com

franco.amato
23rd November 2010, 17:35
I'm new to Qt so don't know how Qt does it, but every reasonably expressive UI API and sizer system allows you to tell an area within the sizer not to grow. I can't be sure, but my bet is that your grid is being expanded to fill the space given to it by the sizer it is within. You may need to tell THAT sizer that it shouldn't be giving any free space to your grid (or you might have to tell the grid not to take any, depending on how Qt does it). Then the space should be absorbed by a different widget.

Simply setting the size may very well be insufficient for this need. Sizers/layouts/whatever tend to give out space according to how THEY have been told to, letting the widgets given that space to resize as they desire, and then *positioning them within the space given* according to whatever rules have been dictated/defaulted.

It's just a guess, but it's a reasonable one and you're not getting any other answers...so there you go...

BTW, I tend to get quicker, better, possibly ANY answers at stackoverflow.com

Hi,
I think you're right but I have no idea on how to tell to the grid to don't get such space

wysota
23rd November 2010, 18:30
Wysota I think by the layout, as I set the size of the widgets in the layout

Don't think. Check it out. See what size the widget wants to have and what size it indeed has.

franco.amato
23rd November 2010, 20:21
Don't think. Check it out. See what size the widget wants to have and what size it indeed has.

I wrote this code:


m_focusLed = new QLed();
m_focusLed->setFixedSize(13,13);
m_focusLed->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
QSize size = m_focusLed->sizeHint();
qDebug() << size.width() << " " << size.height();


And the result was -1 -1

wysota
23rd November 2010, 20:31
Too bad :)

franco.amato
23rd November 2010, 20:36
Too bad :)

Too bad my code or the result? :confused:

I reimplemented the sizeHint method for the QLed so:


QSize QLed::sizeHint() const
{
return QSize(15, 15);
}

But the space is still there so seems is a layout problem that definitively I don't know how to solve

wysota
23rd November 2010, 21:42
The layout is definitely not the problem. Why are you setting fixed sizes on all the widgets?

franco.amato
24th November 2010, 04:51
The layout is definitely not the problem. Why are you setting fixed sizes on all the widgets?

I set a fixed size to avoid that their size become bigger that the buttons

wysota
24th November 2010, 08:41
And you really think this is a good idea to do it this way? I see you didn't learn that much for the last three years...

franco.amato
24th November 2010, 14:16
And you really think this is a good idea to do it this way? I see you didn't learn that much for the last three years...

I don't know if is the best idea, is the only way I know to do it.
Sorry but layouts are not my best, for that I'm asking for some help. I can not stay paused for days to only solve this

wysota
24th November 2010, 18:48
I don't know if is the best idea, is the only way I know to do it.
Sorry but layouts are not my best, for that I'm asking for some help. I can not stay paused for days to only solve this

Then go and learn to use layouts properly instead of staying "paused".

franco.amato
24th November 2010, 18:55
Then go and learn to use layouts properly instead of staying "paused".

Wysota I'm not posting here because I have free time, I'm posting here because all what I tried doesn't solve my problem, and seems the Qt mailing list neither. ( this is sure the last project I'll do using Qt ).
I created and added a colored QWidget instead of the grid layout into the mainlayout and I didn't get such annoying space that I can not remove, so defilitively the problem is the grid layout

wysota
24th November 2010, 19:15
I'm posting here because all what I tried doesn't solve my problem
Instead of "doing things", learn something for a change. You are getting paid for your work and I'm answering your questions for free so please don't tell me what you are and what you are not doing because you have free time. I'm not sure your employer would be happy hearing what problems you are having with the simplest things about his project. If you are not willing to invest your work time (or your free time) to learn things you need to know to be a good software developer then maybe this work is simply not meant for you.

If you can't cope with layouts then reimplement resizeEvent for your widget and position the widgets manually.

BTW. I hope you are not violating QLed's licence.

franco.amato
24th November 2010, 19:31
You are getting paid for your work and I'm answering your questions for free so please don't tell me what you are and what you are not doing because you have free time. I'm not sure your employer would be happy hearing what problems you are having with the simplest things about his project.

I'm not getting paid for this project, my work is another. I'm doing it for free in my free time and I have no employer, as I'am an employer but this is out of topic.


I'm answering your questions for free
Would you get paid to write "Instead of "doing things", learn something for a change"? :D

SixDegrees
24th November 2010, 19:39
Wysota I'm not posting here because I have free time, I'm posting here because all what I tried doesn't solve my problem, and seems the Qt mailing list neither. ( this is sure the last project I'll do using Qt ).
I created and added a colored QWidget instead of the grid layout into the mainlayout and I didn't get such annoying space that I can not remove, so defilitively the problem is the grid layout

Actually, it seems pretty clear to me that the problem is the programmer. Not the layout.

Use the designer. It allows you to randomly tweak every setting widgets and layouts have to offer, completely at random, in any way you like and without having to actually learn anything. Sooner or later you'll stumble across a collection of settings that do what you want, although you likely won't understand why.

Normally, I'd suggest reading the detailed and extensive documentation that comes with Qt free of charge explaining how to use layouts, but countless suggestions to you in the past to spend five minutes reading something have fallen on deaf ears.

If the sort of answer you're looking for involves having someone else write your code for you, I've mentioned before that I'm available for consulting. My current rates start at $150 per hour, with a minimum of 50 hours, although my availability is limited at the moment and I likely won't have time to start any new projects for at least a couple of months.

franco.amato
24th November 2010, 19:53
Actually, it seems pretty clear to me that the problem is the programmer. Not the layout.
Of course is the programmer and not the layout, why do you think I asked for a help.


Use the designer. It allows you to randomly tweak every setting widgets and layouts have to offer, completely at random, in any way you like and without having to actually learn anything. Sooner or later you'll stumble across a collection of settings that do what you want, although you likely won't understand why.
I can not use the designer at this point as all code is already wrote manually.


Normally, I'd suggest reading the detailed and extensive documentation that comes with Qt free of charge explaining how to use layouts, but countless suggestions to you in the past to spend five minutes reading something have fallen on deaf ears.
What do you think? That you are the only that read the documentation? I read all regarding layout in the assistant, I replaced the grid layout with other widgets without any problems. Only with this I'm having problems.


If the sort of answer you're looking for involves having someone else write your code for you, I've mentioned before that I'm available for consulting. My current rates start at $150 per hour, with a minimum of 50 hours, although my availability is limited at the moment and I likely won't have time to start any new projects for at least a couple of months.

Do you think I'll spend 7500$ for a software I'm releasing for free for only remove a space in a layout???
I prefere leaving there the space ;)
Regards

wysota
24th November 2010, 20:20
I can not use the designer at this point as all code is already wrote manually.
Maybe you could find the right settings in Designer, generate the C++ code and paste it to your manually written class? Naaa... that would probably be stupid, right?


What do you think? That you are the only that read the documentation? I read all regarding layout in the assistant,
One can "read" or "read with understanding". Find all your threads, see how many posts they have and compare to other threads in the forum. Then think to which "readers" group do you belong?


I replaced the grid layout with other widgets without any problems.
Replacing a layout with some widget doesn't make you an expert of layouts :)

Only with this I'm having problems.
Franco, let's face it - you are having problems with nearly everything.

franco.amato
24th November 2010, 22:22
I solved my problem...and saved 7500$ SixDegrees :p
Thanx