Results 1 to 15 of 15

Thread: [QWidget] How to save proporties?

  1. #1
    Join Date
    Jan 2009
    Posts
    51
    Thanks
    28
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default [QWidget] How to save proporties?

    How can I save proporties of QWidget while resizing?
    For example I want QWidget's height to be always 3/4 of width.
    Thanks in advance!

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: [QWidget] How to save proporties?

    I don't an easy option for that, but you can reimpl QWidget::resizeEvent ( QResizeEvent * event ) and fix the proportion yourself.

  3. #3
    Join Date
    Jan 2009
    Posts
    51
    Thanks
    28
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: [QWidget] How to save proporties?

    Can you give me an example?
    I tried something like this:
    Qt Code:
    1. MyWidget::resizeEvent(QResizeEvent *e){
    2. e->size()->setWidth(...);
    3. e->size()->setHeight(...);
    4. QWidget::resizeEvent(e);
    5. }
    To copy to clipboard, switch view to plain text mode 
    But as I remember there wasn't even any difference...

  4. #4
    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: [QWidget] How to save proporties?

    There is also QWidget::heightForWidth().
    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.


  5. The following user says thank you to wysota for this useful post:

    Macok (28th March 2009)

  6. #5
    Join Date
    Jan 2009
    Posts
    51
    Thanks
    28
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: [QWidget] How to save proporties?

    Thanks, but how should I reimplement that QWidget::heightForWidth()?
    I tried this:
    Qt Code:
    1. int QWidget::heightForWidth(int w) const{
    2. return w*3/(double)4;
    3. }
    To copy to clipboard, switch view to plain text mode 
    What's wrong?

  7. #6
    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: [QWidget] How to save proporties?

    That's not enough. You also have to tell the layout (through size policy) that you want to use heightForWidth. I'm also not sure if you can actually set it on a top level widget (you might have to set it on a child). Also remember this controlls the preferred width/height dependency. If you want to force it, you have to set a size constraint on the widget's layout. So if you don't know how to do that, reimplementing resizeEvent() might be a better (or at least faster to implement) idea. Your current implementation didn't work because QResizeEvent::size() returns a copy of the size object and after you modify the copy you need to set it back on the widget. It will work if you implement it properly.
    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.


  8. #7
    Join Date
    Jan 2009
    Posts
    51
    Thanks
    28
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: [QWidget] How to save proporties?

    Quote Originally Posted by wysota View Post
    Your current implementation didn't work because QResizeEvent::size() returns a copy of the size object and after you modify the copy you need to set it back on the widget. It will work if you implement it properly.
    Thanks, but how can I do it?
    Should I use setFixedPosition() in resizeEvent()?

  9. #8
    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: [QWidget] How to save proporties?

    No, you should use resize() or setGeometry() for example...
    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.


  10. #9
    Join Date
    Jan 2009
    Posts
    51
    Thanks
    28
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: [QWidget] How to save proporties?

    I tried, but it still doesn't work.
    Here's the code for 1:1 proporties:
    Qt Code:
    1. void GLWidget::resizeEvent(QResizeEvent *e){
    2. QResizeEvent *event;
    3. if(e->size().width()<e->size().height()){
    4. event=new QResizeEvent(QSize(e->size().height(), e->size().height()), e->oldSize());
    5. resize(e->size().height(), e->size().height());
    6. }
    7. else{
    8. event=new QResizeEvent(QSize(e->size().width(), e->size().width()), e->oldSize());
    9. resize(e->size().width(), e->size().width());
    10. }
    11. QGLWidget::resizeEvent(event);
    12. }
    To copy to clipboard, switch view to plain text mode 
    If width is bigger than height, i set height to be the same as width and vice versa.
    What's wrong?

  11. #10
    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: [QWidget] How to save proporties?

    Why are you creating a new resize event?

    Please, if you use some method (like resizeEvent) please read its docs first. It's clearly stated the event is delivered after the widget has already been resized so changing the data sent to the base implementation won't do you any good especially that QWidget's resizeEvent() is empty.

    Qt Code:
    1. void ...::resizeEvent(QResizeEvent *re){
    2. if(re->width()==re->height()) return; // avoid infinite loop
    3. int larger = qMax(re->width(), re->height());
    4. resize(larger, larger);
    5. }
    To copy to clipboard, switch view to plain text mode 
    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.


  12. The following user says thank you to wysota for this useful post:

    Macok (29th March 2009)

  13. #11
    Join Date
    Jan 2009
    Posts
    51
    Thanks
    28
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: [QWidget] How to save proporties?

    Thanks, now it's ok.
    Why are you creating a new resize event?
    If I don't callQGLWidget::resizeEvent(...) QGLWidget will not scale the scene displayed in it.


    Finally, this works fine:
    Qt Code:
    1. void GLWidget::resizeEvent(QResizeEvent *re){
    2. if(re->size().width()==re->size().height()) return; // avoid infinite loop
    3. int min = qMin(re->size().width(), re->size().height());
    4. resize(larger, larger);
    5.  
    6. QGLWidget::resizeEvent(new QResizeEvent(QSize(min, min), re->oldSize()));
    7. }
    To copy to clipboard, switch view to plain text mode 
    But I have one more question.
    When width of my window is bigger than height, widget will not be expanded on the whole window, but it'll be placed on the left side.
    I'd like to place widget exactly in the middle of window's width.

    Is there any simple way to reach this effect?
    Maybe something with setGeometry()?
    Thanks in advance.

  14. #12
    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: [QWidget] How to save proporties?

    You have a memory leak. As for the question, you already answered it.
    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.


  15. #13
    Join Date
    Jan 2009
    Posts
    51
    Thanks
    28
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: [QWidget] How to save proporties?

    No, I didn't.
    Maybe you misunderstood my question.
    I have widget with proporties 1:1, but window when this widget is placed has another proporties, so widget is placed on the left side, or top of the window.
    I'd like to place it in the middle, but I don't know how to do it.
    I tried to use spacers but without results.

  16. #14
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [QWidget] How to save proporties?

    setGeometry() or move()...to desired location..shouldnt be hard

  17. #15
    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: [QWidget] How to save proporties?

    Quote Originally Posted by Macok View Post
    No, I didn't.
    Maybe you misunderstood my question.
    Maybe I have.

    I have widget with proporties 1:1, but window when this widget is placed has another proporties, so widget is placed on the left side, or top of the window.
    So your GL widget is not a top-level window, right? In that case you should have used heightForWidth...

    I'd like to place it in the middle, but I don't know how to do it.
    I tried to use spacers but without results.
    Anyway... you don't need spacers although of course you can use them. When you place your widget in a layout, make it a grid layout and when calling addWidget() you can pass an alignment flag. But if you are using Designer to design your window then spacers are an easier way of obtaining the result. Just remember you'll have to decide which direction you want "anchored" to the window - you can't use four spacers or else your GL widget won't resize at all, you have to place two spacers either on the left and right of the widget or above and below it.

    On second thought you might get away with four spacers but you need to set the size policy of your GL widget to Expanding in both directions. If you can accept that then it should work (provided it won't break your aspect ratio).
    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.


  18. The following user says thank you to wysota for this useful post:

    Macok (29th March 2009)

Similar Threads

  1. How to save file with QFileDialog
    By pnikolov in forum Qt Programming
    Replies: 11
    Last Post: 1st June 2012, 10:23
  2. how to save database
    By khalid_se in forum Newbie
    Replies: 7
    Last Post: 10th July 2011, 18:47
  3. Save images to database
    By jnk5y in forum Qt Programming
    Replies: 4
    Last Post: 8th May 2006, 19:56
  4. save animate to gif
    By Dmitry in forum Qt Programming
    Replies: 2
    Last Post: 16th February 2006, 16:35
  5. What does "Save All" actually save?
    By Mariane in forum Newbie
    Replies: 7
    Last Post: 31st January 2006, 13:23

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.