0

So, I have a QFrame with its layout set as a QGridLayout.

Within this layout I have tiles in rows of 16 which represent something of a palette.

I want this grid of tiles to be separated by lines, like a grid should be. I can do this easily with the tiles' paintEvents.

However, the obvious problem is that between the tiles, the lines are doubled up. When I scale this up for other applications, the difference becomes even more noticeable.

So, is there a way to create a gridline overlay for my QFrame? I have considered converting the whole thing to a view/scene solution, and using drawForeground, however this seems like a completely inappropriate use of the paradigm.

Thanks for any assistance!

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Connor Spangler
  • 805
  • 2
  • 12
  • 29

1 Answers1

1

Put the QFrame into a QGridLayout, then put a custom QWidget with transparent background and paintEvent that paints the grid on top of it (same QGridLayout position).

Or since you already have a QGridLayout, just put the custom QWidget in that, above the tiles, filling the entire grid.


A side note, are you sure you want QFrame there, or if just QWidget would do? Just saying, because with QFrame you get that 1990's look into your UI... If you do want that then go ahead, just saying.

hyde
  • 60,639
  • 21
  • 115
  • 176
  • Thanks for the response! I actually ended up falling rear-end backwards into the solution: create a custom QWidget with the grid drawing paintEvent as you described, and setting its parent as the QFrame. As long as I instantiate it after my tiles, it overlays them perfectly. As for the QFrame aesthetics, I guess I really don't mind – Connor Spangler Mar 24 '20 at 07:05
  • 1
    Changed to your second solution, via grid-filling. Makes the overlay portable with the grid itself, as adding anything above the grid moves the grid's contents but not the overlay with my previous solution. – Connor Spangler Mar 24 '20 at 08:04
  • @ConnorSpangler Yeah, it's usually best to try and get things working with Qt layouts, and resort to other solutions (like overriding resizeEvent) only if you need something special which Qt layouts can't do. – hyde Mar 24 '20 at 11:11