WGridLayout issue (again)
Added by David Gaarenstroom almost 12 years ago
A while ago, I did some experimenting with a WGridLayout, see http://redmine.webtoolkit.eu/boards/2/topics/3349
A couple of days ago I decided to pick up some code I was working on, and I noticed things gotten worser, at the moment I have no idea how I should properly use a WGridLayout with items spanning more than one column with Wt 3.3.0.
Again, I'm trying to create a grid with equally sized cells, achieve something like this:
+-+-+-+-+-+-+-+-+-+-+
| | | [somelabel] | |
+-+-+-+-+-+-+-+-+-+-+
[label] [My lineedit]
+-+-+-+-+-+-+-+-+-+-+
[label] | | | | | | |
+-+-+-+-+-+-+-+-+-+-+
However, every item in the grid seems to get a hidden visibility property and coordinates at --1000000px,--1000000px. All I can see is the grid container.
The code I'm using is:
#include <Wt/WApplication>
#include <Wt/WContainerWidget>
#include <Wt/WGridLayout>
#include <Wt/WLabel>
#include <Wt/WLineEdit>
#include <Wt/WText>
using namespace Wt;
#define COL_SIZE 8
#define ROW_SIZE 20
class GridTest : public WApplication {
public:
GridTest(const Wt::WEnvironment& env);
};
GridTest::GridTest(const Wt::WEnvironment& env)
: Wt::WApplication(env)
{
const int columnCount = 10;
const int rowCount = 5;
setTitle("Test grid");
// Create the root Container
WContainerWidget* container = new WContainerWidget(root());
// Set size for container
container->resize(columnCount*COL_SIZE, rowCount*ROW_SIZE);
// Add a grid layout
WGridLayout* grid = new WGridLayout();
// Disable Grid spacing
grid->setHorizontalSpacing(0);
grid->setVerticalSpacing(0);
// Divide the grid into equally-sized cells with stretch 0
for (int c = 0; c < columnCount; ++c)
grid->setColumnStretch(c, 0);
for (int r = 0; r < rowCount; ++r)
grid->setRowStretch(r, 0);
// Add Some fields to the grid
WLabel* field1 = new WLabel("somelabel");
grid->addWidget(field1, 0, 3, 1, 5);
WLabel* field2 = new WLabel("label");
grid->addWidget(field2, 1, 0, 1, 3);
WLineEdit* field3 = new WLineEdit();
grid->addWidget(field3, 1, 4, 1, 6);
WLabel* field4 = new WLabel("label");
grid->addWidget(field4, 2, 0, 1, 3);
// Use different background colors for each item
WCssDecorationStyle dfield1;
dfield1.setBackgroundColor(WColor("yellow"));
field1->setDecorationStyle(dfield1);
WCssDecorationStyle dfield2;
dfield2.setBackgroundColor(WColor("red"));
field2->setDecorationStyle(dfield2);
WCssDecorationStyle dfield3;
dfield3.setBackgroundColor(WColor("blue"));
field3->setDecorationStyle(dfield3);
WCssDecorationStyle dfield4;
dfield4.setBackgroundColor(WColor("green"));
field4->setDecorationStyle(dfield4);
// Add a background color for "container" to debug its allocated size
WCssDecorationStyle decoration;
decoration.setBackgroundColor(WColor("silver"));
container->setDecorationStyle(decoration);
WCssDecorationStyle even;
even.setBackgroundColor(WColor("white"));
WCssDecorationStyle odd;
odd.setBackgroundColor(WColor("black"));
// Add a small checherboard row and column at the bottom and right side of the grid
for (int r = 0; r < rowCount; ++r) {
if (r == (rowCount - 1)) {
for (int c = 0; c < columnCount; ++c) {
WContainerWidget* square = new WContainerWidget();
square->setDecorationStyle(((c + rowCount - 1) % 2) ? odd : even);
grid->addWidget(square, rowCount - 1, c);
}
}
else {
WContainerWidget* square = new WContainerWidget();
square->setDecorationStyle(((columnCount - 1 + r) % 2) ? odd : even);
grid->addWidget(square, r, columnCount - 1);
}
}
container->setLayout(grid);
}
I've looked at the documentation, but I cannot find what I'm doing wrong here... If there was a "SetColumnWidth(int colNo, const WLength& width)" for a WGidLayout, that might be helpful.
By the way, why has Wt moved to generating
's for grid cells instead of a
?
Replies (3)
RE: WGridLayout issue (again) - Added by David Gaarenstroom almost 12 years ago
I'm attaching a Qt source file that does exactly what I'd like to achieve in Qt. What WGridLayout does not offer compared to QGridLayout is a "setColumnMinimumWidth" and a "setRowMinimumHeight", but a more rigid setColumnWidth and setRowHeight would suffice for me as well...
qgridlayout.cpp (2.87 KB) qgridlayout.cpp | Qt GridLayout example |
RE: WGridLayout issue (again) - Added by Koen Deforche almost 12 years ago
Hey,
Apparently you hit a JavaScript problem, which is caused by (the probably unintentional) widget being added to the grid layout in a cell that is overspanned (namelly the checker-board tile in the second row, column 10 is being overspanned by the lineedit).
I've fixed this in my git copy, but you can in the mean time work around this by not rendering this tile (or rendering the line edit with colspan=5).
Wt's layout managers do not behave exactly like Qt's layout managers do (Qt has much more control over the sizing of an individual cell than Wt, which needs to rely on what a browser offers as ways of measuring the width/height of an item).
Also, although you intend each cell to be exactly the same size, Wt will first satisfy the preferred size of each column/row and then redistribute 'excess width' equally over everything according to the stretches. In this way you do not end up with equally sized columns. If you want a rigid layout than Wt's layout managers may not be what you need; they are really designed for flexible (contents-based) layouts. Rigid layouts are actually easily accomplished using pure CSS, and I would really recommend learning that aspect of web development (in addition to basic HTML). Wt's layout managers are designed to complement that.
The reason why we switched from table to divs is that this was the only way to solve many issues with the layout managers in the previous version, providing a much more consistent behavior (triggering of a bug in the layout code is hopefully the exception!)
Regards,
koen
RE: WGridLayout issue (again) - Added by David Gaarenstroom almost 12 years ago
First of all, this is just an example small enough to demonstrate the problem I have. Apparently I was not defining enough columns in the example. If I increase columnCount to 11 it looks better, but some cells still don't show up at all...
I don't need the Wt's layout manager to be rigid, in fact I'd rather have the Qt behavior where you can only set the minimum height/width of a row/column, but since Wt is using div's now, I assumed that is impossible, as far as I know one cannot create a grid using div's that simultaneously grows a row if one if its cells gets too high and grows a column if one of its cells get too wide. If I'm mistaken, please tell me...
AFAIK, I have to set the size of the WGridLayout to get the desired row/column geometry, I'd rather have it the other way around, setting row heights and column widths, implying the gridlayout and (default) cell geometry. It is possible in HTML, but I don't know if it's possible in Wt?
Anyway, I'm attaching two HTML files illustrating what I'd like to achieve in C using Wt, one using
and one using
to achieve almost the same end result. Since the table-version has grid properties, meaning it automatically grows columns and rows to fit each of their cells, I like that version the most. But I really don't know how to use Wt to create the same end-result now (I managed to get it working with the old
based WGridLayout and a little bit of CSS).
table_example.html (2.19 KB) table_example.html | Grid layout example using <table> | ||
div_example.html (3.3 KB) div_example.html | Grid layout example using <div> |