QScrollArea With Custom Widgets
Ok let's say you have a program like structured like this:
CObject Objects in the table, like 25
CTable Table for QScrollArea
main where you display QScrollArea
In CTable you do this (assume all objects declared):
Code:
iObjects = 25;
setLayout(lay);
for(int i = 0; i < iObjects; i++){
buttons <<
new QPushButtons
(QString("%1").
arg(i
));
}
}
// ASSUME XPOS AND YPOS ARE CORRECTLY DECLARED
for(int i = 0; i < iObjects; i++){
buttons[i]->move(xpos*150+10, ypos*101+10);
}
}
And in main
Code:
qs->setWidget(ctable);
Code has been shortened for clarity!!!
The idea here is, I am building 25 custom widgets (I used buttons here for simplicity, with a QList), and they have to automatically resize and list themselves (the dimensions are 150 by 100), like as if you would see a test with problems in them. It will go like this:
1 2 3 4 5
6 7 8 9 10
11 12.....
Problem is when I compile this code, it will not show the objects, it will show them all in one spot (so all 25 are in same spot so you only see ONE).
Need help please.
Re: QScrollArea With Custom Widgets
Where are u adding the buttons to the grid layout ??
Re: QScrollArea With Custom Widgets
I'm not... but I am declaring the pushbuttons as parent "this".
but I have tried addWidget on each, but didn't get me very far.
Re: QScrollArea With Custom Widgets
I'd suggest reading about Layout Classes. ;)
Re: QScrollArea With Custom Widgets
Are you creating the buttons directly in the scroll area?
If so, it's not OK. You must create another widget that you will set as the scroll area contents with QScrollArea::setWidget and then create your buttons with this widget as parent. Also, the grid layout must be set for this widget.
Regards
Re: QScrollArea With Custom Widgets
Quote:
Originally Posted by
marcel
You must create another widget that you will set as the scroll area contents with QScrollArea::setWidget and then create your buttons with this widget as parent.
He is doing so:
Code:
qs->setWidget(ctable);
Quote:
Also, the grid layout must be set for this widget.
He is doing that too:
It's just the actual usage of the layout that's missing. :)
Re: QScrollArea With Custom Widgets
I would suggest... you're right.
But see, when I do this:
lay->addWidget(cobject[i]);
and then compile...
All I see is one widget (I believe the others are underneath it).
move I guess doesn't work with QGridLayout, but I tried removing QGridLayout, and it seems thats not the problem. So I'm wondering... is move a useless function that only works when it feels like it?
Re: QScrollArea With Custom Widgets
Use QGridLayout::addWidget() which takes a row and column number. Oh, and you can forget about resizing and moving by hand when a layout is in use. :)
Re: QScrollArea With Custom Widgets
Shouldn't the layout be applied on the viewport and not directly on the scroll area?
Re: QScrollArea With Custom Widgets
Quote:
Originally Posted by
wysota
Shouldn't the layout be applied on the viewport and not directly on the scroll area?
But then it wouldn't make any sense to use a scroll area? His layout is applied on the widget inside the scroll area as I see is correct.
Re: QScrollArea With Custom Widgets
Oh, right. I didn't notice that.
Re: QScrollArea With Custom Widgets
See it's not working. Inside my main loop, I tried addWidget(cobjects[i], i, i); just to see if i can get more than one, but nooooo, still one damn widget in one damn top left corner.
Also, lemme list something, the QScrollArea is there for a reason. Because there will always be vertical scroll, since there may be a high number of objects. Let's say the person maximizes the program the program will go from:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
17 18 19 20
21 22 23 24
to
1 2 3 4 5 6 7 8
9 10 11 12 13 14 15 16
17 18 19 20 21 22 23 24
Understand??
So in other words, I dont even know if I should use grid layout, because I need the positions of the objects to change according to the size of the widget. And I don't see no "ChangeItemPos()"... And eliminating the gridlayout does the same thing, even with no gridlayout move doesnt' work... WHY?
Re: QScrollArea With Custom Widgets
Could you prepare a small compilable example and attach it here?
Re: QScrollArea With Custom Widgets
ok hold on... but btw, i updated my last post. Ima have to do it after lunch.
Re: QScrollArea With Custom Widgets
You should use the flow layout (or whatever it's called) instead of grid layout. It's bundled with Qt as one of its examples.
Re: QScrollArea With Custom Widgets
I tried using a flowlayout,
with Cobjects, it doesn't work, still same problem.
With QPushButtons, it lists them like so:
1 2 3 4 5 6 7 8 9 10 11 12
13 14 15 16 17 18 19 20 21 21 23 24
But, I don't want to scroll horizontally, so flowlayout is useless to me, because it doesn't adjust itself to fit the size of the QScrollArea and window. There should never be a need to use horizontal scroll, it should automatically reorder itself to fit vertical scroll only.
Re: QScrollArea With Custom Widgets
What is a CObject? Obviously you can only put QObjects into layouts. And the flow layout does work for you - it layouts widgets, it doesn't control size of the parent widget - you have to control that yourself. Did you make the widget of the viewport resizable? Maybe it'll adjust itself to the scrollarea then.
1 Attachment(s)
Re: QScrollArea With Custom Widgets
CObject inherits QObject. CObject has like 2 buttons.
I don't understand what you mean but I'm about to upload the test rar i prepared for you to better understand.
I can't believe VBulletin doesn't accept rar, who doesn't use rar... :S.
Also an interesting find:
I tried this:
flow->addWidget(new QPushButton("bla"));
and it crashes
but when I do
flow->addWidget(new CObject(this));
it works fine. Meaning QPushButtons don't work with flowlayouts... odd. Hmm I noticed that it works only if i resize the widget, i guess some sort of size causes flowlayouts to crash.
But mainly the bigger problem is, when you have QPushButtons it seems to have the illusion that its working except ResizeEvent doesn't work AT ALL. However, why the hell does my widget only become 1 widget, why can't i ever see more than one? Why doesn't the 24 widgets I make become visible?
1 Attachment(s)
Re: QScrollArea With Custom Widgets
I guess the thing attached is what you wanted.
Re: QScrollArea With Custom Widgets
Yes that's exactly what I wanted... now i'm gonna try it with my object class.
Nope, it's not working with FlowLayout,.. My class that inherits QWidget, with parent wgt.
So basically, flowlayout only works with QPushButtons, and other widgets, not with widgets with multiple buttons and QLabels.