QGridLayout - Fixed Number of Rows and Columns
-
wrote on 8 Mar 2018, 11:52 last edited by
Is there any way to force a
QGridLayout
to adopt a fixed number of rows and columns of equal size?I'm looking for a way of dragging a
QWidget
onto the layout, but then allowing it to be resized while snapping to the rows, columns to maintain a regular size. -
Is there any way to force a
QGridLayout
to adopt a fixed number of rows and columns of equal size?I'm looking for a way of dragging a
QWidget
onto the layout, but then allowing it to be resized while snapping to the rows, columns to maintain a regular size.Moderatorswrote on 8 Mar 2018, 11:55 last edited by raven-worx 3 Aug 2018, 11:57@webzoid
You could pre-insert fixed sized plain widgets into the grid layout. Those widgets then accept the drop events and place the widget inside them.
I think thats the easiest solution without much "hacking". -
wrote on 8 Mar 2018, 11:58 last edited by
@raven-worx Thanks for your quick reply.
This sounds ok so far, but, what would happen when I want to resize a widget to cover, say, 3 columns and 2 rows? What would happen to those pre-inserted widgets?
I don't mind having to do a bit of hacking - maybe a QWidget or custom QLayout to do the layout logic would be a more elegant solution?
-
@raven-worx Thanks for your quick reply.
This sounds ok so far, but, what would happen when I want to resize a widget to cover, say, 3 columns and 2 rows? What would happen to those pre-inserted widgets?
I don't mind having to do a bit of hacking - maybe a QWidget or custom QLayout to do the layout logic would be a more elegant solution?
@webzoid said in QGridLayout - Fixed Number of Rows and Columns:
This sounds ok so far, but, what would happen when I want to resize a widget to cover, say, 3 columns and 2 rows? What would happen to those pre-inserted widgets?
I can't tell you straight from head, but just evaluate it with a small example. If the behavior is not as desired you will have to go with a custom layout. Which might be hard work though.
-
wrote on 8 Mar 2018, 12:17 last edited by
@raven-worx Sorry, I didn't mean to put you on the spot... haha.
I've actually been thinking about a custom layout and that it may not be so difficult. If I could specify a number of rows/columns then I can calculate height/width of each cell so that when a widget is resized, I can dynamically compute its size based on its column/row spans.
I'm sure its not a trivial exercise mind but it seems a cute way of doing it. Google searches tell me that plenty of people have tried before but ultimately failed - maybe this is my time to shine!
-
@raven-worx Sorry, I didn't mean to put you on the spot... haha.
I've actually been thinking about a custom layout and that it may not be so difficult. If I could specify a number of rows/columns then I can calculate height/width of each cell so that when a widget is resized, I can dynamically compute its size based on its column/row spans.
I'm sure its not a trivial exercise mind but it seems a cute way of doing it. Google searches tell me that plenty of people have tried before but ultimately failed - maybe this is my time to shine!
@webzoid
:)
Difficulty is relative ;) -
wrote on 8 Mar 2018, 15:03 last edited by
Ok, so I'm mocking something up using a
QTableWidget
which, surprisingly, seems to be allowing me to do what I want so far. I can set up a fixed number of rows/columns and I can add widgets to a cell which then can be spanned across cols/rows.Drag and drop seems to be working nicely, just need to figure out the runtime widget resizing and spanning...
-
wrote on 12 Mar 2018, 17:11 last edited by
Is there any way of preventing a
QWidget
which has been bound to aQTableWidget
cell (using thesetCellWidget
function) from being destroyed when callingremoveCellWidget
?I'm wanting to move a
QWidget
to another cell, but, when dragging it to another cell (and subsequently callingsetCellWidget
), the "old" cell still holds a pointer to the widget even though it has been moved to another cell. -
wrote on 13 Mar 2018, 09:39 last edited by
Ok, so using a blood-and-thunder method, I've managed to achieve what I want but basically, its a crappy solution so I'm going to need to look at another way of doing things.
The
QTableWidget
is just far too slow when large numbers of rows/columns are used. -
wrote on 13 Mar 2018, 09:46 last edited by
setCellWidget
is notoriously painfully slow. It's not a problem ofQTableWidget
Are the widget the same type in all cells? -
setCellWidget
is notoriously painfully slow. It's not a problem ofQTableWidget
Are the widget the same type in all cells?wrote on 13 Mar 2018, 09:56 last edited by@VRonin Yeah, I appreciate that its an issue with the underlying implementation - my fumbling has had be digging into the
QAbstractItemView
class and implementing my ownsetIndexWidget
function in a customQTableView
class.Ultimately, the widgets dropped into the cells will have the same base class but the derivatives will be very different.
1/11