Results 1 to 6 of 6

Thread: Drawing on QWidget - strech & resize

  1. #1
    Join Date
    Aug 2006
    Posts
    77
    Thanks
    14
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Drawing on QWidget - strech & resize

    In my application i am drawing directly on QWidget. i have that widget in QMainWindow as
    central widget. If i resize my MainWindow i want that the drawing on the widget is
    resizing properly. Is there i way to do that with layers, like if i add a QDial to QHBoxLayout
    is the widget resizing properly but not my widget with my drawing?

    Thank you for your answer.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Drawing on QWidget - strech & resize

    You have to take the geometry into account. Don't use fixed coordinates but calculate them according to the current width and height. QPainter helps you to achieve that transparently. See Window-Viewport Conversion.
    J-P Nurmi

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

    kemp (22nd January 2007)

  4. #3
    Join Date
    Aug 2006
    Posts
    77
    Thanks
    14
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Drawing on QWidget - strech & resize

    Thank you for the answer. I applied the analog clock example to my code, but there are still
    things that i don't understand.
    the ratio of my drawing is correct but I don't know why my drawing is not centered properly.

    I can reproduce this on an example:

    Qt Code:
    1. #include <QLabel>
    2.  
    3. class Test_Label : public QLabel
    4. {
    5. Q_OBJECT
    6.  
    7. public:
    8. Test_Label(QWidget *parent = 0);
    9.  
    10. protected:
    11. void paintEvent(QPaintEvent *e);
    12. };
    13.  
    14.  
    15.  
    16. #include <QtGui>
    17. #include "test_label.h"
    18.  
    19. Test_Label::Test_Label(QWidget *parent)
    20. : QLabel(parent)
    21. {
    22.  
    23. }
    24.  
    25. void Test_Label::paintEvent(QPaintEvent *e)
    26. {
    27.  
    28. int side = qMin(width(), height());
    29.  
    30. QPainter painter(this);
    31.  
    32. painter.translate(width() / 2, height() / 2);
    33. painter.scale(side / 200.0, side / 200.0);
    34.  
    35. painter.drawRect(0,0,50,70);
    36.  
    37. }
    To copy to clipboard, switch view to plain text mode 


    The rectangle is always positioned in the lower right corner. I don't understand why.
    Could you please help me?

    Thanks.

  5. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Drawing on QWidget - strech & resize

    I'm not sure how do you want it to look like, but here's an example at simplest:
    Qt Code:
    1. void SomeWidget::paintEvent(QPaintEvent* event)
    2. {
    3. QPainter painter(this);
    4. painter.setWindow(QRect(-50,-50,100,100));
    5. painter.drawRect(-25, -25, 50, 50);
    6. }
    To copy to clipboard, switch view to plain text mode 
    PS. I just took a quick look at the Analog Clock example and I find it a bit surprising that it doesn't even use QPainter::setWindow() nor QPainter::setViewport() still if it's linked to the aforementioned "Window-Viewport Conversion" documentation..
    J-P Nurmi

  6. #5
    Join Date
    Aug 2006
    Posts
    77
    Thanks
    14
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Drawing on QWidget - strech & resize

    Thanks for your help. Now the rectangle is in the middle, but when i resize the
    window the rectangle also gets deformed, but i want to keep the ratio between
    the width() and height() always the same and centered. If i use the set window
    my drawing is deformed and if i use:
    Qt Code:
    1. int side = qMin(width(), height());
    2. painter.translate(width() / 2, height() / 2);
    3. painter.scale(side / 200.0, side / 200.0);
    To copy to clipboard, switch view to plain text mode 
    in my code, the drawing dissapear and without setWindow the ratio is correct but
    the drawing is always in lower right corner.

  7. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Drawing on QWidget - strech & resize

    Quote Originally Posted by kemp View Post
    the drawing is always in lower right corner.
    Yes, because the painter is translated so that (0,0) is in the center of the widget.
    J-P Nurmi

Similar Threads

  1. postponing resize event
    By Honestmath in forum Qt Programming
    Replies: 11
    Last Post: 26th February 2006, 00:32
  2. Replies: 4
    Last Post: 17th January 2006, 17:46

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.