Difference between Designer-created layout and Code-Created Layout
Hello,
I'm fairly new to Qt, and learning it in my spare time. I've looked and looked at the docs for this issue, and if its there, I can't see it.
Essentially, I'm making use of the Designer to create the startup window, and then I am dynamically adding new content to it later.
The setup:
- I have a QFrame with a GridLayout. I have 10 items per row, 2 rows, nicely spaced inside of said Frame.
- I have a button designed to add duplicates of this frame (some minute differences in terms of names and such, but layout is the same)
However, even though I am using the exact same layout type, and the exact same sizes and presets for the items added to it, the result I get is very obviously different in the dynamically created Frame, compared to the Designer Frame.
I even tried creating a clone function to make the layouts exactly the same, but it didn't help.
Image: http://puu.sh/ofa4R/2e57854eb5.png
Code:
- Clone:
Code:
{
target->setAlignment(original->alignment());
target->setContentsMargins(target->contentsMargins());
target->setHorizontalSpacing(target->horizontalSpacing());
target->setMargin(target->margin());
target->setOriginCorner(original->originCorner());
target->setSizeConstraint(original->sizeConstraint());
target->setSpacing(original->spacing());
target->setVerticalSpacing(original->verticalSpacing());
}
- Add Frame:
Code:
void MainWindow::addUnit()
{
if(unitsPerTab[ui->tabWidget->currentIndex()]-48 == 9)
{
return;
}
unitsPerTab[ui->tabWidget->currentIndex()] += 1;
QString prefix
= "unit" + number
+ "_";
unitFrame->setGeometry(1,1,1057,61);
unitFrame->setMaximumHeight(61);
unitFrame->setMaximumWidth(1040);
unitFrame->setMinimumHeight(61);
unitFrame->setMinimumWidth(1040);
unitFrame->setAutoFillBackground(true);
unitFrame
->setFrameShape
(QFrame::Box);
unitFrame
->setFrameShadow
(QFrame::Raised);
unitFrame->setLineWidth(1);
unitFrame->setObjectName(prefix + "frame");
clone(ui->gen_layout, unitLayout);
unitLayout->setObjectName(prefix + "layout");
unitFrame->setLayout(unitLayout);
//...Ommitted for brevity...
unitLayout->addWidget(unitLabels[0],0,0);
for(int i = 1; i < 10; i++)
{
unitLayout->addWidget(unitLabels[i],0,i);
}
unitLayout->addWidget(uSelect,1,0);
unitLayout->addWidget(auth,1,1,Qt::AlignHCenter);
unitLayout->addWidget(spin,1,2,Qt::AlignHCenter);
unitLayout->addWidget(msize,1,3,Qt::AlignHCenter);
unitLayout->addWidget(champ,1,4);
unitLayout->addWidget(banner,1,5);
unitLayout->addWidget(horn,1,6);
unitLayout->addWidget(ubi,1,7,Qt::AlignHCenter);
unitLayout->addWidget(minU,1,8);
unitLayout->addWidget(cost,1,9,Qt::AlignHCenter);
QString layName
= ui
->tabWidget
->currentWidget
()->objectName
() + "_layout";
ui->tabWidget->currentWidget()->findChild<QVBoxLayout*>(layName)->addWidget(unitFrame);
unitFrame->show();
}
So any ideas?
Small PS: I have a ScrollArea with a Vertical Layout, AlignTop. On startup, the Align:Top doesn't work. However, when I add one the above frames to the layout, it all aligns top. Ideas? IMG
Re: Difference between Designer-created layout and Code-Created Layout
Quote:
Originally Posted by
vaftss2
The setup:
- I have a QFrame with a GridLayout. I have 10 items per row, 2 rows, nicely spaced inside of said Frame.
- I have a button designed to add duplicates of this frame (some minute differences in terms of names and such, but layout is the same)
However, even though I am using the exact same layout type, and the exact same sizes and presets for the items added to it, the result I get is very obviously different in the dynamically created Frame, compared to the Designer Frame.
Have you looked at the code that gets generated for the designer form?
Also, if this is exactly the same frame, why don't you simply create it in designer as its own form class and instantiate it whenever you need it?
Cheers,
_
Re: Difference between Designer-created layout and Code-Created Layout
I tried to once, couldn't find it, forgot, and I went looking again after you reminded me. The generated code helped me fix it. Thank you.
As well, yes, that would make a lot of sense. Derp. EDIT: I now see what you mean more clearly. Wow. I did a lot of unnecessary work.
Finally, any ideas as to why my align::top isn't working on startup? As soon as I add a new item to the layout, it aligns to the top no problem, but until then the first item is in the centre
EDIT2: Fixed it with the whole form thing from above. Wow this Qt thing is thought out. Thanks for the memory-jog!