Results 1 to 13 of 13

Thread: Paint event does not Rescale with label maximise

  1. #1
    Join Date
    Jun 2013
    Posts
    58
    Thanks
    26
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Question Paint event does not Rescale with label maximise

    Using qt Creator

    Created window with label
    Uploads image to label
    I set the the label to "expanding" so its size increases as window maximizes
    The image is set to "scaled" -> also increases with label

    Using QPainter I am able to draw points on image & lines between them.
    The problem is Qpainter does not increase as image expands, Hence my
    points then are not placed in the correct location where initially clicked.


    Is there a Setting to change to scale the QPainter?
    Or is there another work around it
    #How?

    Ideas most welcome

    Thanks
    Kind Regards
    Last edited by ebsaith; 21st June 2013 at 07:53. Reason: updated contents

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

    Default Re: Paint event does not Rescale with label maximise

    QPainter has a scale method.

    Cheers,
    _

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

    ebsaith (21st June 2013)

  4. #3
    Join Date
    Jun 2013
    Posts
    58
    Thanks
    26
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: Paint event does not Rescale with label maximise

    Thanks
    #How

    1)How do I get it to scale with Window size
    QPainter::scale(qreal sx, qreal sy) ???

    2) Do Iplace the code in constructor or in the paintevent

    Kind Regards
    Example code, pls if possible

  5. #4
    Join Date
    Feb 2013
    Location
    India
    Posts
    153
    Thanks
    27
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Paint event does not Rescale with label maximise

    You can use resizeEvent and capture the resized rect of label. and set your painter according to that.

  6. The following user says thank you to karankumar1609 for this useful post:

    ebsaith (21st June 2013)

  7. #5
    Join Date
    Jun 2013
    Posts
    58
    Thanks
    26
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: Paint event does not Rescale with label maximise

    Not winning

    Created in header class of myLabel (as QPainter is within this class)
    Qt Code:
    1. void resizeEvent(QResizeEvent *event);
    To copy to clipboard, switch view to plain text mode 
    implementation.cpp:
    Qt Code:
    1. void resizeEvent(QResizeEvent *event)
    2. {
    3. //Taken partially from example Scribble ( http://www.trinitydesktop.org/docs/qt4/widgets-scribble.html )
    4.  
    5. if(width() > fplabel.width() || height() > fplabel.height())
    6. {
    7. int newWidth = qMax(width()+128, fplabel.width());
    8. int newHeight = qMax(height()+128, fplabel.height());
    9. QSize(newWidth, newHeight);
    10. QImage newImage(newSize);
    11. QPainter painter(&newImage);
    12. painter.drawImage(QPoint(0, 0), *image);
    13. *image = newImage;
    14. update();
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

    Image is uploaded in main class

    Not sure how to go about it

    ideas please

  8. #6
    Join Date
    Feb 2013
    Location
    India
    Posts
    153
    Thanks
    27
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Paint event does not Rescale with label maximise

    Save the width and height of the label from resizeEvent and call update, reimplement the paint event and paint on label.....

    also scale the QImageaccording to the width and height before paint, but do not change the original image.


    THATS SO SIMPLE....
    CHEERS

  9. The following user says thank you to karankumar1609 for this useful post:

    ebsaith (21st June 2013)

  10. #7
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Paint event does not Rescale with label maximise

    You don't need resizeEvent().

    All you need is to scale QPainter, also scale the points and lines you store. Here is an working example. I used some of your code from other thread, I hope you recollect
    Qt Code:
    1. class Label : public QLabel
    2. {
    3. QList<QPoint> current_pos;
    4. QSize size;
    5.  
    6. public:
    7. explicit Label(QWidget * parent = 0);
    8.  
    9. protected:
    10. void mouseMoveEvent(QMouseEvent *event);
    11. void paintEvent(QPaintEvent *event);
    12. };
    13.  
    14. Label::Label(QWidget * parent)
    15. : QLabel(parent)
    16. , size(100, 100)
    17. {
    18. QRect rect;
    19. rect.setTopLeft(QPoint(100, 100));
    20. rect.setSize(size);
    21. setGeometry(rect);
    22. }
    23.  
    24. void Label::paintEvent(QPaintEvent *event)
    25. {
    26. QLabel::paintEvent(event);
    27.  
    28. QPainter painter(this);
    29.  
    30. qreal sx = event->rect().width() / (qreal)size.width();
    31. qreal sy = event->rect().height() / (qreal)size.height();
    32.  
    33. painter.scale(sx, sy); // scale the painter
    34.  
    35. QPen pen(Qt::green, 4, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
    36. painter.setPen(pen);
    37.  
    38. foreach(QPoint point, current_pos)
    39. painter.drawPoint(point);
    40. }
    41.  
    42. void Label::mouseMoveEvent(QMouseEvent *event)
    43. {
    44. qreal sx = rect().width() / (qreal)size.width();
    45. qreal sy = rect().height() / (qreal)size.height();
    46.  
    47. QPoint p;
    48. p.setX(event->pos().x() / sx); // Scale to original before storing
    49. p.setY(event->pos().y() / sy);
    50.  
    51. current_pos << p;
    52. this->repaint();
    53. }
    54.  
    55. int main(int argc, char ** argv)
    56. {
    57. QApplication app(argc, argv);
    58.  
    59. Label l;
    60. l.show();
    61.  
    62. return app.exec();
    63. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  11. The following user says thank you to Santosh Reddy for this useful post:

    ebsaith (21st June 2013)

  12. #8
    Join Date
    Jun 2013
    Posts
    58
    Thanks
    26
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Arrow Re: Paint event does not Rescale with label maximise

    (@Santosh -> remembered older thread -> fantastic)
    Thanks That code works, partially though


    new problems arise as code is implemented
    initial line before altering window size is displayed & now scaled accordingly
    however now other functionality that used to work doesnt
    -> Image upload, no longer places image in label // used to work basically user presses btnUpload, image is set to label (mainClass)
    -> line is drawn points are not shown(tested without uploading image) // before added the scaling code, points were shown(paint event)
    -> if I draw a line and then maximise line is scaled, but no future lines are shown!

    Any Ideas?
    Thanks for the help @All &
    Thanks for the welcoming assistance @Santosh Reddy
    Last edited by ebsaith; 21st June 2013 at 13:53. Reason: updated contents

  13. #9
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Paint event does not Rescale with label maximise

    At this point I think you know every thing that is required to make it work. Its just matter of figuring out how to put them togeather.

    BTW where are you painiting, on QLabel or QMainWindow?
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  14. #10
    Join Date
    Jun 2013
    Posts
    58
    Thanks
    26
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: Paint event does not Rescale with label maximise

    Painting in QLabel

    Will try figuring out how to put them together.
    Thanks for all your assistance

    If any ideas(road map) for this problem come to mind
    Give me a shout.

    #define Santosh
    ValuableInput = true;


    Kind Regards

  15. #11
    Join Date
    Jun 2013
    Posts
    58
    Thanks
    26
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Post Re: Paint event does not Rescale with label maximise

    Good Day, 1nAll

    K Thanks to your help, I got most of my re-scaling and painting Event working.

    What I cant get right.
    Once I maximise my window, event re-scales
    But when I create a new point/line it does not start where my mouse pointer is!
    The point is drawn way RIGHT of where the actual mouse is.

    Mouse Pointer (*) -----------------------------> Point Starts Way of the marker
    Any Ideas
    Kind Regards

    Getting to grips with qt -> Awesome qt
    Last edited by ebsaith; 24th June 2013 at 07:44. Reason: updated contents

  16. #12
    Join Date
    Jun 2013
    Posts
    58
    Thanks
    26
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: Paint event does not Rescale with label maximise

    How do I Save the width and height of the label from resizeEvent
    & reimplement the paint event and paint on label.....
    & also scale the QImage according to the width and height before paint,

    Example please
    @karankumar1609
    Last edited by ebsaith; 25th June 2013 at 08:11. Reason: updated contents

  17. #13
    Join Date
    Feb 2013
    Location
    India
    Posts
    153
    Thanks
    27
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Paint event does not Rescale with label maximise

    You can use a local QRect variable which will store the geometry of label on each and every resize event (Well we only need width and height).
    In resize event you can call update() function for the label.
    and yah for scaling image.... you can use the scale function of QImage class.

    well you can do this all via another way... forget everything like resize event , update, and etc..,.,. You can use setPixmap method of QLabel..
    this will do everything like rezise, repaint everything you need.... try both techniques.... this will help you most..


    CHEERS.....

Similar Threads

  1. How to handle mousehover event on a label?
    By moiit in forum Qt Programming
    Replies: 5
    Last Post: 28th January 2011, 03:16
  2. How to paint (QPainter) on a label ?
    By d@nyal in forum Newbie
    Replies: 1
    Last Post: 29th May 2010, 18:39
  3. Timer event & paint event, priority
    By Teuniz in forum Qt Programming
    Replies: 0
    Last Post: 2nd February 2010, 13:33
  4. Paint event function in key press event
    By soumya in forum Qt Programming
    Replies: 6
    Last Post: 2nd February 2010, 12:40
  5. QGraphicsTextItem and paint event
    By prashant in forum Qt Programming
    Replies: 0
    Last Post: 6th November 2009, 07:52

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.