Results 1 to 9 of 9

Thread: Insert a layout into ScrollArea's layout

  1. #1
    Join Date
    Jul 2015
    Posts
    20
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Insert a layout into ScrollArea's layout

    What I want to achieve is this:
    pic.png

    The second TextEdit needs a spacer in a horizontal layout for this. I've made a layout like this:
    Qt Code:
    1. QHBoxLayout* hLayout = new QHBoxLayout();
    2. hLayout->addItem(new QSpacerItem(16, 64, QSizePolicy::Fixed, QSizePolicy::Fixed));
    3. hLayout->addWidget((QTextEdit*)itemText); //Just fyi: itemText is an object of class that extends QTextEdit.
    To copy to clipboard, switch view to plain text mode 

    and tried to add it to an existing (vertical)layout that is set on the ScrollArea.
    Qt Code:
    1. ui->scrollArea->widget()->layout()->addItem(hLayout);
    To copy to clipboard, switch view to plain text mode 

    But it only adds empty space with the height of spacer. Why doesn't the QTextEdit object appear?

  2. #2
    Join Date
    Sep 2015
    Posts
    1
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Insert a layout into ScrollArea's layout

    You need to use 2 layouts that are nested.

    Take a look at this...


    QHBoxLayout* hLayout = new QHBoxLayout;
    QVBoxLayout* vLayout = new QVBoxLayout;


    QTextEdit* te1 = new QTextEdit("test text", this);
    QTextEdit* te2 = new QTextEdit("test text2", this);

    QSpacerItem* spacer = new QSpacerItem(16,64, QSizePolicy::Fixed, QSizePolicy::Fixed);

    hLayout->addItem(spacer);
    hLayout->addWidget(te2);

    vLayout->addWidget(te1);
    vLayout->addItem(hLayout);

    ui->scrollAreaWidgetContents->setLayout(vLayout);


    this code's output is what you've asked.

  3. #3
    Join Date
    Jul 2015
    Posts
    20
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Insert a layout into ScrollArea's layout

    Yes, it works. But how to add a widget to an existing layout? Without creating a vertical one.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Insert a layout into ScrollArea's layout

    What kind of layout do you currently have on ui->scrollArea->widget()?

    Is your code executed before or after the scroll area has been shown?

    Cheers,
    _

  5. #5
    Join Date
    Jul 2015
    Posts
    20
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Insert a layout into ScrollArea's layout

    After it has been shown. Added the ScrollArea in ui and I want to add multiple widgets into it with code.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Insert a layout into ScrollArea's layout

    Quote Originally Posted by Grzyboo View Post
    After it has been shown. Added the ScrollArea in ui and I want to add multiple widgets into it with code.
    Ok, then make sure you also call show() on the new widgets.

    show() of child widgets is only automatic when they have been added before their parent is shown for the first time.
    After that they are created in hidden state and need to be explicitly shown.

    Cheers,
    _

  7. #7
    Join Date
    Jul 2015
    Posts
    20
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Insert a layout into ScrollArea's layout

    Like this?

    Qt Code:
    1. QLayout *layout = ui->scrollArea->widget()->layout();
    2. QTextEdit* text = new QTextEdit(ui->scrollArea);
    3.  
    4. if(isCode)
    5. {
    6. QHBoxLayout* hLayout = new QHBoxLayout();
    7. hLayout->addItem(new QSpacerItem(16, 64, QSizePolicy::Fixed, QSizePolicy::Fixed));
    8. hLayout->addWidget(text);
    9. layout->addItem(hLayout);
    10. text->show();
    11. }
    12. else
    13. layout->addWidget(text);
    To copy to clipboard, switch view to plain text mode 

    Widgets added after else statement just work fine. But layout inside if doesn't. TextEdits appear properly, but scrolling the area doesn't make them move. Do i have to call show() on all the widgets every time an user uses the scrollbar?

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

    Default Re: Insert a layout into ScrollArea's layout

    [QUOTE=Grzyboo;281638]
    As asked earlier, which type of layout is layout?
    If it is a QVBoxLayout, can you try casting to that and call addLayout(hLayout)?

    Also either do not specify a parent to the text edit or use the correct parent.

    Quote Originally Posted by Grzyboo View Post
    Do i have to call show() on all the widgets every time an user uses the scrollbar?
    No.

    Cheers,
    _

  9. #9
    Join Date
    Jul 2015
    Posts
    20
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Insert a layout into ScrollArea's layout

    Quote Originally Posted by anda_skoa View Post
    If it is a QVBoxLayout, can you try casting to that and call addLayout(hLayout)?
    This. Thanks

Similar Threads

  1. How to autoresize a layout in a scrollarea ?
    By hungpham90 in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 21st October 2011, 05:30
  2. Replies: 6
    Last Post: 13th February 2011, 23:06
  3. Replies: 0
    Last Post: 12th December 2010, 05:09
  4. Replies: 0
    Last Post: 25th May 2009, 10:00
  5. Can I insert expanding layout in Qt Designer?
    By zolookas in forum Newbie
    Replies: 2
    Last Post: 6th October 2008, 18:17

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.