Results 1 to 15 of 15

Thread: Paint Event does not correspond to coordinates!

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

    Arrow Paint Event does not correspond to coordinates!

    Good Day,

    I'm struggling with resizing my paint event!

    In my constructor:
    Qt Code:
    1. qLabel::qLabel(QWidget *parent) :
    2. QLabel(parent), windowSize(333, 494)
    3. {...
    4. QRect rect;
    5. rect.setTopLeft(QPoint(9, 9));
    6. rect.setSize(windowSize);
    7. setGeometry(rect);
    8. }
    To copy to clipboard, switch view to plain text mode 

    In my paint event
    Qt Code:
    1. void mmetLabel::paintEvent(QPaintEvent *ev)
    2. {
    3. QLabel::paintEvent(ev);
    4. QPainter painter(this);
    5.  
    6. qreal sx = ev->rect().width() / (qreal)windowSize.width();
    7. qreal sy = ev->rect().height() / (qreal)windowSize.height();
    8. painter.scale(sx, sy);
    9. ...
    10. //draw points and lines between every 2 consective points
    11. }
    To copy to clipboard, switch view to plain text mode 

    Once I click on my label, I display the coordinates of the mouse ->working
    Label also displays the 1st point and 2nd point coordinates -> working

    My Paintevent -> NOT Working accordingly

    It draws points not where mouse is clicked, hence the lines are completely not right
    example, If I click at point1 (x=100, y=100) & click point2(x=152, y=251)
    it draws a line from (x=230, y=128) -> (x=351, y=195) = completely wrong!

    Please help
    Kind Regards
    Previous Thread(Title: Re: Paint event does not Rescale with label maximise)

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

    Default Re: Paint Event does not correspond to coordinates!

    Hmm, that looks wrong. I don't think you should be taking the event's rect, that can be only part of the widget.

    If I understand your goal correctly, then you might be better off by transforming your points when the widget resizes.

    That way your points are always in the same frame of reference as the widget itself.

    Cheers,
    _

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

    ebsaith (25th 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 correspond to coordinates!

    Thanks,
    Transforming points? how do i go about it

  5. #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: Paint Event does not correspond to coordinates!

    What is "windowSize"? Where do you get that value from?
    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.


  6. #5
    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!

    I set my windowSize in constructor

    in my class:
    QSize windowSize;

    The label I created is of that size... under forms, I click on label, it shows the size in the right toolBar(property/value)

    [code]
    mmetLabel::mmetLabel(QWidget *parent) :
    QLabel(parent), windowSize(333, 494)
    {
    QRect rect;
    rect.setTopLeft(QPoint(9, 9));
    rect.setSize(windowSize);
    setGeometry(rect);
    }


    Added after 31 minutes:


    Re: moving around code with flags

    Qt Code:
    1. void resizeEvent(QResizeEvent *ev)
    2. {
    3. wresize = true;
    4. }
    To copy to clipboard, switch view to plain text mode 

    in paintEvent
    Qt Code:
    1. if(wresize == true)
    2. {
    3. qreal sx = (ev->rect().width() / (qreal)windowSize.width());
    4. qreal sy = (ev->rect().height() / (qreal)windowSize.height());
    5. painter.scale(sx, sy);
    6. wresize = false;
    7. }
    To copy to clipboard, switch view to plain text mode 

    How do I set a flag in ResizeEvent... It gives me an error when I try to set the flag to true in ResizeEvent
    Any Ideas, Not sure if this implementation will work though... testing ideas
    Last edited by ebsaith; 25th June 2013 at 11:29.

  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: 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.


  8. #7
    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

  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: 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.


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

    ebsaith (25th June 2013)

  11. #9
    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

  12. #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: 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.


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

    ebsaith (25th June 2013)

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

    Default Re: Paint Event Points does not correspond to coordinates!

    Thanks @ wysota

    I got one snag with the code!
    My code draws the line between 2 mouse clicks(2 points) ---> line is shown
    But the points are not displayed * *//though
    If I dont use the resize code (testing) resize points are shown!
    Any ideas, where the problem may be

    Kind Regards
    Last edited by ebsaith; 26th June 2013 at 08:31. Reason: updated contents

  15. #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: Paint Event Points does not correspond to coordinates!

    Show your code.
    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.


  16. #13
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Paint Event does not correspond to coordinates!

    Quote Originally Posted by ebsaith View Post
    Transforming points? how do i go about it
    You create a QTransform object, apply all necessary transformations to it (e.g. scale), then iterate over the points and have the transform object map them to their new value which you then store again.

    Cheers,
    _

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

    ebsaith (26th June 2013)

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

    Thumbs up Re: Paint Event does not correspond to coordinates!

    When I try using painter.drawPoint(p1); doesnt work
    Tried
    Qt Code:
    1. painter.drawEllipse(TempMark, 2, 2);
    To copy to clipboard, switch view to plain text mode 
    works fine!
    For the time being will use ellipse.

    With regards to topic:
    Re: Paint Event does not correspond to coordinates! SOLVED!
    Many Thanks to all
    Last edited by ebsaith; 26th June 2013 at 09:48. Reason: updated contents

  19. #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: Paint Event does not correspond to coordinates!

    Quote Originally Posted by ebsaith View Post
    When I try using painter.drawPoint(p1); doesnt work
    It works but it draws a small point.
    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.


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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.