Results 1 to 6 of 6

Thread: keeping a widget to be square

  1. #1
    Join Date
    Apr 2008
    Posts
    53
    Thanks
    10

    Default keeping a widget to be square

    Hello,

    I have a problem with placing a widget into a horizontalLayout and making it keep an equal aspect ratio (square).

    Here is a rough outline of my code:
    Qt Code:
    1. TargetDisplay::TargetDisplay(QWidget *parent)
    2. : QMainWindow(parent)
    3. {
    4. ui.setupUi(this);
    5. d_plot = new RandomPlot(this);
    6.  
    7. QSizePolicy policy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    8. policy.setHeightForWidth(true);
    9. d_plot->setSizePolicy(policy);
    10.  
    11. ui.horizontalLayout->insertWidget(0, d_plot);
    12. }
    To copy to clipboard, switch view to plain text mode 

    and then in my RandomPlot widget I have:

    Qt Code:
    1. class RandomPlot: public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. RandomPlot(QWidget *parent);
    7. ~RandomPlot();
    8.  
    9. int heightForWidth(int w) const {
    10. return w;
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

    but my widget is not maintaining an equal aspect ratio. Can somebody please tell me what I'm doing wrong? Thank you!

    -James

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: keeping a widget to be square

    This will only work for a widget in a layout. For a top-level window you have to reimplement resizeEvent() and resize the widget to a proper aspect from within.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Apr 2008
    Posts
    53
    Thanks
    10

    Default Re: keeping a widget to be square

    Quote Originally Posted by wysota View Post
    This will only work for a widget in a layout. For a top-level window you have to reimplement resizeEvent() and resize the widget to a proper aspect from within.
    My widget is in HorizontalLayout:

    Qt Code:
    1. ui.horizontalLayout->insertWidget(0, d_plot);
    To copy to clipboard, switch view to plain text mode 

    Can somebody explain what I need to add to my original code? Thanks
    Last edited by jmsbc; 28th May 2009 at 22:07.

  4. #4
    Join Date
    Apr 2008
    Posts
    53
    Thanks
    10

    Default Re: keeping a widget to be square

    Nevermind, I figured it out. Instead of adding d_plot directly to the horizontalLayout, I added it to QwtDynGridLayout first, and then added the layout to the horizontalLayout:

    Qt Code:
    1. gLayout = new QwtDynGridLayout(this);
    2. gLayout->addWidget(d_plot);
    3. ui.horizontalLayout->insertLayout(0, gLayout);
    To copy to clipboard, switch view to plain text mode 

    Now I get a square

  5. #5
    Join Date
    Apr 2006
    Location
    Denmark / Norway
    Posts
    67
    Thanks
    3
    Thanked 12 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: keeping a widget to be square

    It's because the stretch factor on your horizontal layout when adding the widget is by default 0.

    QHBoxLayout *hbox = new QHBoxLayout();
    hbox->addWidget(mywidget, 1);

    If you are using Qt Designer, set the layoutStretch parameters.

  6. #6
    Join Date
    Apr 2006
    Location
    Denmark / Norway
    Posts
    67
    Thanks
    3
    Thanked 12 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: keeping a widget to be square

    As I found this layout manager that keeps a widgets square, I figured it would be wise to share it:
    http://wiki.forum.nokia.com/index.ph...a_widget_in_Qt

    Attached a zip file with source and example, in case it goes missing on the wiki.

    Thanks to the heads at nokia for making this layout manager
    Attached Files Attached Files

Similar Threads

  1. QDockWidget inside another widget in the center?
    By Antebios in forum Qt Programming
    Replies: 1
    Last Post: 16th February 2010, 07:06
  2. Creating a square sized widget in a layout
    By cboles in forum Qt Programming
    Replies: 5
    Last Post: 22nd September 2008, 23:38
  3. Playbutton functionality
    By uchennaanyanwu in forum Qt Programming
    Replies: 5
    Last Post: 31st July 2008, 22:29
  4. How to Open & Close a Widget ?!!
    By Fatla in forum Qt Programming
    Replies: 6
    Last Post: 13th June 2008, 20:39
  5. Tricky problem with ARGB widget / UpdateLayeredWindow
    By nooky59 in forum Qt Programming
    Replies: 3
    Last Post: 21st February 2008, 10:35

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.