Adding a custom widget into an existing layout.
-
Hi,
I'm new to Qt and especially to it's layout and widget structure. I want to make a small game which requires clickable tiles. I use QPushButtons with stylesheets and I arranged them into a QGridLayout.
Board::Board(int width, int height, QWidget *parent) : QWidget(parent), width(width), height(height) { setParent(parent); QGridLayout *mainLayout = new QGridLayout(this); mainLayout->setSizeConstraint(QLayout::SetFixedSize); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { BoardUnit *bu = new BoardUnit(x, y, this); mainLayout->addWidget(bu, y, x); bu->fadeIn(); } } setLayout(mainLayout); }
This is basically how I initialize my board. It's based on this tutorial. This is leading to a pretty good result.
I'm adding the custom Board widget to my window like this:
Board *b = new Board(8, 5, this); layout()->addWidget(b);
The result is what you see in the screenshot. Now my problem is, that I can't get the custom Board (QWidget) to center properly. Since I got an already existing layout with 2 spacers I've tried to simply add it between those two spacers, without any success. This is what the previous screen looks like in the designer:
So my questions are:
How do I add the Board widget between those already existing spacers?
And is this even the best way to solve my problem?
Is there a possibility to see whats happening in the running window? So like the inspector in Firefox for html/css which shows what objects are where, even if they're invisble to the user? (To get an idea where the spacers are and what they're doing)
I find the whole layout thing a bit confusing, mostly because I switch between the editor which does it's magic behind the scenes and my dynamic code which has to interact with that auto generated stuff.Thanks in advance!
-
Sooo, I got it. I think this is actually the proper way to do it, right? At least it's staying centered now. But the other qestions remain? Is this the way to do it?
static_cast<QGridLayout *>(ui->gridLayout)->addWidget(b, 0, 1, Qt::AlignCenter);
Thanks anyway for reading?
-
@m1212e said in Adding a custom widget into an existing layout.:
static_cast<QGridLayout *>(ui->gridLayout)
Why do you need a cast here?
-
@Christian-Ehrlicher
You're abolutely right :D
It's just an artifact from previous attempts.
Thanks! -
When you won't add your widget by code but directly in the designer you can take a look at promoting widgets.
-
Already did that, but thanks. Unfortunatly I want to add them in my code.
-
@m1212e
Hi
Just as a note. when i want to insert dynamically into a Designer created
ui, i often just add some test widgets and then go to the generated
code (in the setupUI function) and see how its inserted. This is especially useful for complex layouts. -
@m1212e said in Adding a custom widget into an existing layout.:
Is there any way to edit the auto generated code?
You should not as next time you build your project auto generated code will be overwritten :-)
-
@m1212e said in Adding a custom widget into an existing layout.:
That's a good idea, thanks. Is there any way to edit the auto generated code?
Hi
You must copy the code and make it your own.
But its very handy to visually build something and then get the code to create it dynamically.