Results 1 to 15 of 15

Thread: Paint Event does not correspond to coordinates!

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Paint Event does not correspond to coordinates!

    But what is the purpose of all that code? You can get the label width and height by calling width() and height(), you don't need any "windowSize" for that. ev->rect().width() is going to return the width of the exposed rect which can be anything ranging from 1 to the width of your label. Hence your code makes no sense. Your drawing code needs to draw within QRect(0,0, width(), height()).
    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.


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

    Default Re: Paint Event does not correspond to coordinates!

    Please give me a simple example on how to resize my paintEvent
    So when a user clicks a point it displays a point(working)
    & when window is resized that point is adjusted accordingly

    I'm stuck, tried many plausible theories to no avail though
    Assistance please

    example code appreciated

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

    Default Re: Paint Event does not correspond to coordinates!

    I have no idea what you mean by "resize my paintEvent".

    Resizing the window should not influence your drawing code in any way. Your canvas is simply larger. If you mean that you wish to scale the contents then use QPainter::setWindow(), QPainter::setViewport() and QPainter::worldTransform() (the latter can be used to map mouse event coordinates to what you specify by setWindow()).

    The most trivial approach would be something along:

    Qt Code:
    1. void X::mousePressEvent(QMouseEvent *me) {
    2. QPointF meF = me->pos();
    3. m_points << QPointF(100*meF.x()/(qreal)width(), 100*meF.y()/(qreal)height());
    4. update();
    5. }
    6.  
    7. void X::paintEvent(QPaintEvent *pe) {
    8. QPainter p(this);
    9. p.setViewport(rect());
    10. p.setWindow(QRect(0,0,100,100));
    11. foreach(QPointF pt, m_points) p.drawPoint(pt);
    12. }
    13.  
    14. void X::resizeEvent(QResizeEvent*) { update(); }
    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.


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

    ebsaith (25th June 2013)

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

    Smile Re: Paint Event does not correspond to coordinates!

    Thanks will try it out

    My apologies, What I meant by "resize my paintEvent".
    Is If I draw a point(p1) at lets say (x=50, y=50)
    & I resize my window to lets say double the initial size of the window
    that The p1 should now move to (x=100, y=100)

    The painted point needs to move with the resize!

    further Ideas welcome
    Kind Regards

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

    Default Re: Paint Event does not correspond to coordinates!

    What I gave you earlier should work fine. Another approach, one that doesn't use setWindow would be:

    Qt Code:
    1. void X::mousePressEvent(QMouseEvent *me) {
    2. m_pts << QPointF(me->pos().x()/(qreal)width(), me->pos().y()/(qreal)height());
    3. }
    4.  
    5. void X::paintEvent(QPaintEvent *) {
    6. QPainter p(this):
    7. foreach(QPointF pt, m_pts) p.drawPoint(pt.x()*width(), pt.y()*height());
    8. }
    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.


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

    ebsaith (25th June 2013)

Similar Threads

  1. event in the paint of the drawEllipse
    By NewLegend in forum Qt Programming
    Replies: 1
    Last Post: 20th November 2010, 20:18
  2. Delegate paint event bug?
    By jerry7 in forum Qt Programming
    Replies: 2
    Last Post: 20th October 2010, 02:14
  3. setPixmap and Paint Event
    By Magu in forum Newbie
    Replies: 7
    Last Post: 11th March 2010, 10:32
  4. Timer event & paint event, priority
    By Teuniz in forum Qt Programming
    Replies: 0
    Last Post: 2nd February 2010, 13:33
  5. Paint event function in key press event
    By soumya in forum Qt Programming
    Replies: 6
    Last Post: 2nd February 2010, 12:40

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
  •  
Qt is a trademark of The Qt Company.