Results 1 to 7 of 7

Thread: Paint event function in key press event

  1. #1
    Join Date
    Jul 2009
    Posts
    49
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Paint event function in key press event

    Hi All,

    How to call a paint event function in key press event can anybody help me with the code snippet.



    Regards,
    soumya

  2. #2
    Join Date
    Dec 2007
    Posts
    627
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Paint event function in key press event

    You just need to call repaint() or update() function within keyPressEven().
    Qt suggests to call update() to avoid slowdown and flickering.

  3. #3
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Paint event function in key press event

    Call update like this:
    Qt Code:
    1. update();
    To copy to clipboard, switch view to plain text mode 

    EDIT:
    Oops... i was late :P
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  4. #4
    Join Date
    Jul 2009
    Posts
    49
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Paint event function in key press event

    Hi thanks for the reply i tried with update function but i am getting error as ,

    circleform.cpp: In function ‘void update()’:
    circleform.cpp:423: error: invalid use of ‘this’ in non-member function

    and here is the code snippet

    void circleForm::keyPressEvent( QKeyEvent *event )
    {

    switch ( event->key() )
    {
    case Qt::Ke_Enter:
    update();
    break;
    }
    }

    void update(){
    QPainter painter(this); // error
    painter.setPen(QPen(Qt::black, 3,Qt::SolidLine));
    painter.drawEllipse(100, 140, 160 , 160);
    }

    Regards,
    Soumya

  5. #5
    Join Date
    Dec 2007
    Posts
    627
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Paint event function in key press event

    Quote Originally Posted by soumya View Post
    Hi thanks for the reply i tried with update function but i am getting error as ,

    circleform.cpp: In function ‘void update()’:
    circleform.cpp:423: error: invalid use of ‘this’ in non-member function

    and here is the code snippet

    void circleForm::keyPressEvent( QKeyEvent *event )
    {

    switch ( event->key() )
    {
    case Qt::Ke_Enter:
    update();
    break;
    }
    }

    void update(){
    QPainter painter(this); // error
    painter.setPen(QPen(Qt::black, 3,Qt::SolidLine));
    painter.drawEllipse(100, 140, 160 , 160);
    }

    Regards,
    Soumya

    You never need to override update function. You can do all kind of drawing in paintEvent(). And calling update() function will call paintEvent() automatically.
    So move your code from update() to paintEvent().
    And then call update() from keyPressEvent().

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

    soumya (3rd February 2010)

  7. #6
    Join Date
    Jul 2009
    Posts
    49
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Smile Re: Paint event function in key press event

    Hi,

    I need to draw a circle on the frame which is at the top left corner as shown in the attachment. User can enter certain inputs
    and when the user press the enter key the circle should be drawn on the frame any suggestions
    Attached Images Attached Images

  8. #7
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Paint event function in key press event

    Inherit the frame and draw using QPainter

Similar Threads

  1. Mouse press event in a QGraphicsScene
    By Lykurg in forum Qt Programming
    Replies: 3
    Last Post: 19th June 2009, 10:28
  2. Mouse press event detection
    By A.H.M. Mahfuzur Rahman in forum Qt Programming
    Replies: 1
    Last Post: 14th June 2009, 13:08
  3. Checking for key press on mouse event
    By Cruz in forum Newbie
    Replies: 1
    Last Post: 24th January 2009, 18:18
  4. Key Press Event trouble
    By morraine in forum Newbie
    Replies: 6
    Last Post: 18th August 2008, 08:43
  5. Zooming on PushButton Press event
    By KosyakOFF in forum Qwt
    Replies: 5
    Last Post: 11th April 2008, 09:12

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.