Results 1 to 3 of 3

Thread: Adding widget to bottom left corned of existing Widget

  1. #1
    Join Date
    Mar 2016
    Posts
    8
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Adding widget to bottom left corned of existing Widget

    Hi,

    currently I am trying to achieve something like this:
    Untitled.jpg

    So red widget is centralWidget of my QMainWindow and now I want to add another small widget (yellow one) inside it and put it in bottom left corner.

    If I do something like this

    Qt Code:
    1. QHBoxLayout* layout = new QHBoxLayout(this);
    2.  
    3.  
    4. QWidget* red = new QWidget(this);
    5. red->setStyleSheet("background:red;");
    6. red->setMaximumSize(400, 400);
    7.  
    8. layout->addWidget(red);
    9.  
    10. MyCustomWidget* mainWidget = new MyCustomWidget(this);
    11. mainWidget->setLayout(layout);
    12.  
    13. setCentralWidget(mainWidget);
    To copy to clipboard, switch view to plain text mode 

    I see my widget but it's centered vertically and horizontally inside my MyCustomWidget.
    If I try changing "addWidget" line to :

    Qt Code:
    1. layout->addWidget(red, 0, Qt::AlignLeft | Qt::AlignBottom);
    To copy to clipboard, switch view to plain text mode 

    I don't see my widget anymore.

    Could someone tell me why is it happening and how can I fix it?
    Is my approach good?
    Last edited by carobnodrvo; 15th July 2016 at 16:09.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,359
    Thanks
    36
    Thanked 1,533 Times in 1,484 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Adding widget to bottom left corned of existing Widget

    If your red widget does not have any other children than the yellow widget, then the easiest way is to use a QGridLayout, with two rows and two columns.

    The child goes into second row/first colum, a vertical spacer into first row/first column and a horizontal spacer into second row/second column.

    Btw, your code snippet is misleading, because you make your "yellow widget" red.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    carobnodrvo (18th July 2016)

  4. #3
    Join Date
    Mar 2016
    Posts
    8
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Adding widget to bottom left corned of existing Widget

    Ups, you are right (red widget is actually yellow :/)

    At the end I ended up doing it so.

    Thanks!

Similar Threads

  1. Add widget on top of existing QGLWidget
    By scarecr0w132 in forum Qt Programming
    Replies: 0
    Last Post: 6th December 2013, 00:18
  2. Replies: 1
    Last Post: 3rd December 2010, 13:02
  3. Adding custom widget to an existing layout
    By santana in forum Qt Programming
    Replies: 1
    Last Post: 22nd June 2010, 17:29
  4. Replies: 10
    Last Post: 29th May 2010, 18:42
  5. Replies: 7
    Last Post: 15th November 2007, 17:19

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.