Need help with ScrollArea and drawing widgets
-
[I'm working with Qt5.9.2 and python..]
Bear with me, I’m teaching myself here, I’m sure it will show. I’m struggling to understand where I’m going wrong setting up Qt layouts and scroll areas to allow me to add widgets dynamically and fit them properly in a scroll area. My structure here may seem weird, I’m open to changing it if there is a better way, though some of the components will grow in complexity once I get past laying it out properly.
The basic concept I was going for was to add what I was calling a “surface” to a scroll widgets vertical layout. This widget would contain multiple “shapes” (another widget) in different x,y positions and will ultimately vary in width. The number of surfaces and how many shapes per surface will ultimately come from a file that the user will provide, so I will read the file, add the surfaces and shapes to the scroll area. In the end, there will be many different shapes, but I wanted this as simple as possible for now.
Looking at screenshot 1 (funky colors were only for visual aid to me), the first surface is larger than I want it to be, I want it to be sized to the 3 squares. The second surface is not tall enough and still too wide. It’s not shown in the screenshot, but if a surface has more squares than fit in the window, it doesn’t grow horizontally like I want. I want the horizontal scroll to appear and the surface to grow. I did set the widgetResizable property to True for the QScrollArea and that allowed surfaces to grow, but the scroll bars would never appear.
Ideally, each surface should fit to the content within it, so each surface would vary in size and they wouldn’t stretch. When enough surfaces are added, the scroll bars would show vertically. When enough shapes are added to grow the surface horizontally, the horizontal scroll bars would show horizontally.
I’ve spent hours between reading documentation, various blogs, tutorials and forums. I’ve tinkered with many different combinations of size policies for the various widgets. Everything I’ve attempted to change seems to either do nothing, or reduce my surfaces to 12x12 squares.
I’m putting my code on github rather than pasting the code here since there are a few files involved. I hope this is acceptable, and I hope I can get a little direction. And for the hope trifecta, I hope this all made sense!
Screenshots:
http://www.mediafire.com/file/gym5g6ayh0k9lo9/Screenshot1.png
http://www.mediafire.com/file/nyuhgbj5804du6b/Screenshot2.pngCode:
https://github.com/dmroeder/QtTestThanks for any advice!
-
I was probably asking a bit much with my original post (out of ignorance). I thought I'd update...
After a bunch of experimenting, I tried using QGraphicsView and QGraphicsScene, I'll be darned if that didn't product the effect I was after! I realize now that I could probably have reduced my example more and narrowed my focus on the exact problem I was trying to solve.
I dynamically add "surfaces" to my main windows vertical layout. The surface contains the QGraphicsView and QGraphicsSceen. I add the shapes to the scene of each surface. It seems to work perfectly!
I pushed the changes to the original github link if anyone is curious...