Results 1 to 3 of 3

Thread: Difference between Designer-created layout and Code-Created Layout

  1. #1
    Join Date
    Apr 2016
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default 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:
    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

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Difference between Designer-created layout and Code-Created Layout

    Quote Originally Posted by vaftss2 View Post
    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,
    _

  3. #3
    Join Date
    Apr 2016
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default 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!
    Last edited by vaftss2; 13th April 2016 at 07:15.

Similar Threads

  1. Replies: 1
    Last Post: 7th April 2015, 07:05
  2. Replies: 3
    Last Post: 9th August 2012, 23:21
  3. Subclassing a widget created in Qt Designer
    By dmginc in forum Qt Programming
    Replies: 2
    Last Post: 19th January 2011, 06:30
  4. Replies: 5
    Last Post: 7th May 2009, 14:29
  5. Replies: 2
    Last Post: 4th May 2009, 11:57

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.