A custom widget and scrollbars, design decision.
-
Hello everyone.
I'm looking for some pointers on how to create a new custom widget. Creating the custom widget itself is not the problem. The point is, at runtime new rows get added to my widget and I want scrollbars to appear when the widget overflows. But I don't know what's the best way to archive it. I also know how to add scrollbars generally. But it looks like they only work when I'm using a layout. That's not really what I'm looking for.
The closest thing I can think of is a table. I want my widget to have a working area, a few fixed labels and many labels that can be added at run time.
This is how my control looks now:
!http://www.garden-soft.de/Control1.png(Current situation)!
As you can see, the new labels are on a position on my widget where it can't be seen. I'd like to have scrollbars to make them visible.
My problem with Layouts: When I add a layout, there is alot of spacing between the labels. I don't want much space. I want them to be always the same size and I want the distance between them to be always the same. Yet, if I use a layout I can't get that under control now matter what I try. With a scrollArea, I need to use some kind of layout. :(
Has anybody an idea or some hints how I could solve this problem? Is there a way to add scrollbars without using a layout? What is the common way to create a "table widget"-like control with scrollbars when needed? -
Hello again.
After giving some thought into my question and browsing the documentation I came up with a solution I'm happy with for now.
ScrollAreas work without layouts pretty well. Some websites on the web say otherwise, but that's wrong. The important part is in the Qt documentation itself: "You can retrieve the child widget using the widget() function. The view can be made to be resizable with the setWidgetResizable() function.":http://qt-project.org/doc/qt-4.8/qscrollarea.html.
I didn't realize the meaning of this at first, but after a good night of rest it came to me. In order to make a normal widget scrollable, you have to set widgetResizable to false! Now I can change the size of my widget at will an if it doesn't fit in the QScrollArea the scrollbars appear.
So far, so good. The next point was, as a custom control, I wanted the widget size to grow with the ScrollArea if the widget is smaller than the new ScrollAreas size. Therefore I needed the resizeEvent that is only availabe if you subclass the QScrollArea. I did that and now I had full controll over the size of the widget and the ScrollArea.TL;DR: Scrollbars appear when the widget of the ScrollArea is bigger than the ScrollArea. For full control, don't subclass QTableWidget but QScrollArea and handle the resizeEvent().
-
[quote author="Andre" date="1351001070"]It looks like in your design, it would be better if the header would not scroll with the contents?[/quote]
I'm thinking about that. And the more I think about it, I come to the same conclusion: a fixed header makes more sense. However, I don't think it's very difficult to change this behaviour. I'm probably going to give the user the choice with an enumeration, so they can choose if they want the header to be fixed or scrollable.
In order to achieve this, I'd put the header within the ScrollSrea if the user wants to scroll, otherwise above the ScrollArea. The only difference would be, that I had to change the parent of my costum control to QWidget instead of QScrollArea and add my own ScrollArea on this widget.Do you think that this is a reasonable approach?
-
[quote author="Andre" date="1351002139"]Could be, yes. Or you just base your whole design on a table. I mean: it sure looks like one...[/quote]
That's right of course. It's not always easy to find the easiest/most efficient way on how to do things. Maybe I'll try both ways and look at what's the better choice. My control is table-like. True. There are rows that can be added, removed and changed. But fields should be able to be merged. I'm not sure how easy that can be done with a table. I don't know how much overhead a table has, that I won't nead, but if it's easier to have a table based control I'll do it that way. This is my second custom control in Qt and I have alot to learn. :)
-
Skalll Thank you very much, I had a really hard to make but failed, and now succeeded by your guide