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:
Qt Code:
  1. void clone(QGridLayout* original, QGridLayout* target)
  2. {
  3. target->setAlignment(original->alignment());
  4. target->setContentsMargins(target->contentsMargins());
  5. target->setHorizontalSpacing(target->horizontalSpacing());
  6. target->setMargin(target->margin());
  7. target->setOriginCorner(original->originCorner());
  8. target->setSizeConstraint(original->sizeConstraint());
  9. target->setSpacing(original->spacing());
  10. target->setVerticalSpacing(original->verticalSpacing());
  11. }
To copy to clipboard, switch view to plain text mode 

- Add Frame:
Qt Code:
  1. void MainWindow::addUnit()
  2. {
  3. if(unitsPerTab[ui->tabWidget->currentIndex()]-48 == 9)
  4. {
  5. return;
  6. }
  7.  
  8. unitsPerTab[ui->tabWidget->currentIndex()] += 1;
  9.  
  10. QString number = QString(QChar(unitsPerTab[ui->tabWidget->currentIndex()]));
  11. QString prefix = "unit" + number + "_";
  12.  
  13. QFrame* unitFrame = new QFrame;
  14. unitFrame->setGeometry(1,1,1057,61);
  15. unitFrame->setMaximumHeight(61);
  16. unitFrame->setMaximumWidth(1040);
  17. unitFrame->setMinimumHeight(61);
  18. unitFrame->setMinimumWidth(1040);
  19. unitFrame->setAutoFillBackground(true);
  20. unitFrame->setFrameShape(QFrame::Box);
  21. unitFrame->setFrameShadow(QFrame::Raised);
  22. unitFrame->setLineWidth(1);
  23. unitFrame->setObjectName(prefix + "frame");
  24.  
  25. QGridLayout* unitLayout = new QGridLayout();
  26. clone(ui->gen_layout, unitLayout);
  27. unitLayout->setObjectName(prefix + "layout");
  28.  
  29. unitFrame->setLayout(unitLayout);
  30.  
  31. //...Ommitted for brevity...
  32.  
  33. unitLayout->addWidget(unitLabels[0],0,0);
  34. for(int i = 1; i < 10; i++)
  35. {
  36. unitLayout->addWidget(unitLabels[i],0,i);
  37. }
  38.  
  39. unitLayout->addWidget(uSelect,1,0);
  40. unitLayout->addWidget(auth,1,1,Qt::AlignHCenter);
  41. unitLayout->addWidget(spin,1,2,Qt::AlignHCenter);
  42. unitLayout->addWidget(msize,1,3,Qt::AlignHCenter);
  43. unitLayout->addWidget(champ,1,4);
  44. unitLayout->addWidget(banner,1,5);
  45. unitLayout->addWidget(horn,1,6);
  46. unitLayout->addWidget(ubi,1,7,Qt::AlignHCenter);
  47. unitLayout->addWidget(minU,1,8);
  48. unitLayout->addWidget(cost,1,9,Qt::AlignHCenter);
  49.  
  50. QString layName = ui->tabWidget->currentWidget()->objectName() + "_layout";
  51. ui->tabWidget->currentWidget()->findChild<QVBoxLayout*>(layName)->addWidget(unitFrame);
  52. unitFrame->show();
  53. }
To copy to clipboard, switch view to plain text mode 

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