Results 1 to 19 of 19

Thread: To draw a circle on a frame when the key is pressed

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

    Default To draw a circle on a frame when the key is pressed

    Hi All,
    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 which is at the top left corner any suggestions

    Regards,
    Soumya
    Attached Images Attached Images

  2. #2
    Join Date
    Aug 2008
    Posts
    134
    Thanks
    10
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: To draw a circle on a frame when the key is pressed

    create your own class which is derived from the frame and handle the paint event and draw the desired shape on that.

    Please check the examples given in Qt package. %QTDIR%\Example\Painting\PainterPaths and %QTDIR%\Example\Painting\basicDrawing

  3. #3
    Join Date
    Jan 2008
    Location
    Bengaluru
    Posts
    144
    Thanks
    8
    Thanked 7 Times in 7 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: To draw a circle on a frame when the key is pressed

    Check out this link, this has how to draw a circle,

    http://doc.trolltech.com/4.3/paintin...ormations.html

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

    Default Re: To draw a circle on a frame when the key is pressed

    thank you i will check it out and get back to you

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

    Default Re: To draw a circle on a frame when the key is pressed

    As shown in the attachment i have a form has circleform in that i have many lineedits where the user can enter the position of x and y,radius,number of holes,start angle and endangle. once the user finish entering the inputs and then press the enter key a circle should be drawn on the frame as shown in the fig at top-left corner .


    Here is the circleform.h as shown below
    [CODE]
    #ifndef CIRCLEFORM_H
    #define CIRCLEFORM_H

    #include <QVariant>
    #include <QDialog>

    #include "ui_circleform.h"

    class circleForm : public QDialog, private Ui::circleForm
    {
    Q_OBJECT

    public:
    circleForm( QDialog* parent = 0, Qt::WindowFlags f = Qt::FramelessWindowHint);
    ~circleForm();


    public slots:

    void keyPressEvent( QKeyEvent * event );

    void radius();
    void ycircle();
    void xcircle();
    void start_circle();
    void end_circle();
    void holes();

    private:

    void init();

    protected:

    bool eventFilter(QObject *obj, QEvent *event);


    };

    [CODE\]
    Here is the .cpp code as shown below

    [CODE]
    #include<QValidator>
    #include<QLineEdit>
    #include<QPainter>
    #include<QtGui>
    #include<QRectF>


    circleForm::circleForm( QDialog *parent, Qt::WindowFlags f)
    : QDialog( parent, f)
    {
    setupUi(this);
    xcirclelineEdit->installEventFilter( this );
    ycirclelineEdit->installEventFilter( this );
    radiuslineEdit->installEventFilter( this );
    holeslineEdit->installEventFilter( this );
    start_circle_lineEdit->installEventFilter( this );
    end_circle_lineEdit->installEventFilter( this );
    init();
    }

    circleForm::~circleForm()
    {
    }

    void circleForm::init()
    {
    xcirclelineEdit->setText("0.000");
    ycirclelineEdit->setText("0.000");
    radiuslineEdit->setText("0.000");
    holeslineEdit->setText("0");
    start_circle_lineEdit->setText("0.000");
    end_circle_lineEdit->setText("0.000");

    xcirclelineEdit->setFocus();
    XCircleVal = XVal;
    YCircleVal = YVal;
    ZCircleVal = ZVal;
    x_aux_circle_lineEdit->setText(QString::number( XCircleVal, 'f', decimals_mm) );
    y_aux_circle_lineEdit->setText(QString::number( YCircleVal, 'f', decimals_mm) );
    z_aux_circle_lineEdit->setText(QString::number( ZCircleVal, 'f', decimals_mm) );
    DatumlineEdit->setText(QString::number(DatumVal, 10 ));
    ToollineEdit->setText(QString::number(Toolnumber, 10 ));

    }


    void circleForm::keyPressEvent( QKeyEvent *event )
    {
    // qDebug( "Key Press Event" );
    switch ( event->key() )
    {
    case Qt::Key_Enter:
    {
    // here i should write a function to call a paint event

    }
    }
    }

    [CODE\]
    when i press the enter the key on the frame circle should be drawn.



    cheers,
    Soumya

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

    Default Re: To draw a circle on a frame when the key is pressed

    I am calling a update(); function inside Qt::key_Enter . In update function
    [CODE]
    void circleForm::recall()
    {
    QPainter painter(this);
    painter.drawEllipse(100,20,180,180);
    }
    [CODE\]

    But when i run this i have get a message like,

    Key Press Event: Bolt_Hole function
    QPainter::begin: Widget painting can only begin as a result of a paintEvent

    Does anybody can help me why i am getting this message.

    Regards,
    Soumya

  7. #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: To draw a circle on a frame when the key is pressed

    All painting is supposed to be done only in paintEvent function.

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

    Default Re: To draw a circle on a frame when the key is pressed

    so how to call paintEvent function inside update so that when i press a key a circle can be drawn

  9. #9
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: To draw a circle on a frame when the key is pressed

    Hi,

    first forget about the event filters! Just use the QLineEdit::returnPressed() signal. Connect them to the update slot of your widget. By calling update() a paint event is automatically emitted. Then, inside your paintEvent method, get your parameters and paint your circle.


    Lykurg

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

    Default Re: To draw a circle on a frame when the key is pressed

    If i use signal slot mechanism

    endcirclelineEdit returnpressed frame reapint()

    if i do like this circle will not be drawn.

    Signal slot mechanism is working only for setfocus hide for allthose
    but not for my repaint function

    Here is the .cpp as shown below

    [CODE]

    void circleForm::keyPressEvent( QKeyEvent * event )
    {
    qDebug( "Key Press Event" );
    switch ( event->key() )
    {
    case Qt::Key_Enter:
    repaint();
    break;

    }
    }

    void circleForm::repaint(){
    QPainter painter(this);
    painter.drawEllipse(100,20,180,180);
    }
    [CODE\]

    here is the .h code as shown below

    #include "ui_circleform.h"

    class circleForm : public QDialog, private Ui::circleForm
    {
    Q_OBJECT

    public:
    circleForm( QDialog* parent = 0, Qt::WindowFlags f = Qt::FramelessWindowHint);
    ~circleForm();


    protected:

    void keyPressEvent( QKeyEvent * event );
    void repaint();
    // void paintEvent(QPaintEvent *event);
    };
    [CODE\]

    Any suggestions how to proceed further thanks in advance:-)


    Regards,
    Soumya

  11. #11
    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: To draw a circle on a frame when the key is pressed

    Do you read the replies ? How many times have you been told to draw only in paintEvent function ???

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

    Default Re: To draw a circle on a frame when the key is pressed

    ya i know that draw can be done only in paintevent function but for my apllication once we finish entering the values in lineEdit and then press enter circle should be drawn and lykurg had replied using signal slot mechanism it can be done so i had replied to him so that he can help me in anyways if youknow any ohter method reply me or else its ok and sorry for asking you again and again

  13. #13
    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: To draw a circle on a frame when the key is pressed

    Its ok to ask again and again if you follow up carefully.
    ya i know that draw can be done only in paintevent function
    Can u then explain the code -
    Qt Code:
    1. void circleForm::repaint(){
    2. QPainter painter(this);
    3. painter.drawEllipse(100,20,180,180);
    4. }
    5.  
    6. // void paintEvent(QPaintEvent *event);
    To copy to clipboard, switch view to plain text mode 


    . Connect them to the update slot of your widget. By calling update() a paint event is automatically emitted. Then, inside your paintEvent method, get your parameters and paint your circle.
    Thats what lykurg said.
    So your paintevent shud be something like -
    Qt Code:
    1. myWidget::paintEvent()
    2. {
    3. if(needToDrawCircle)
    4. // draw circle.
    5. }
    6.  
    7. //and in ur slot -
    8. myWidget::slotEneterPressed()
    9. {
    10. needToDrawCircle = true;
    11. }
    To copy to clipboard, switch view to plain text mode 
    and you will need more member variables to store the radius and position of circle if necessary.

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

    Default Re: To draw a circle on a frame when the key is pressed

    Hi,
    Thanks for your help ameer4yu . I have designed simple widget which has only qframe and lineedit when the linedit is pressed using keypressEvent circle is drawm but its not happening please check the code if there is any mistake please guide me how to proceed further

    Here is the .cpp code as showm below

    #include<QLineEdit>
    #include<QPainter>
    #include<QtGui>
    #include"drawform.h"

    drawForm::drawForm( QDialog *parent, Qt::WindowFlags f)
    : QDialog( parent, f)
    {

    setupUi(this);
    //init();
    }

    drawForm::~drawForm()
    {

    }


    void drawForm::keyPressEvent( QKeyEvent * event )
    {
    qDebug( "Key Press Event" );
    switch ( event->key() )
    {
    case Qt::Key_Enter:
    //connect(drawlineEdit, SIGNAL(returnPressed()), frame, SLOT(update()));
    // repaint();
    paintEvent();
    break;

    }
    }

    void drawForm:aintEvent()

    {
    if(needToDrawCircle){
    QPainter painter(this);
    painter.drawEllipse(100,20,180,180);
    }
    }


    Here is the .h code as shown below,
    #ifndef DRAWFORM_H
    #define DRAWFORM_H

    #include <QVariant>
    #include <QDialog>

    #include "ui_drawform.h"

    class drawForm : public QDialog, private Ui::drawForm
    {
    Q_OBJECT

    public:
    drawForm( QDialog* parent = 0, Qt::WindowFlags f = Qt::FramelessWindowHint);
    ~drawForm();


    protected:

    void keyPressEvent( QKeyEvent * event );
    void paintEvent();
    // void paintEvent(QPaintEvent *event);
    public:
    bool needToDrawCircle;

    protected slots:
    void slotEneterPressed()
    {
    needToDrawCircle = true;
    }
    };

    #endif // DRAWFORM_H


    Regards,
    Soumya

  15. #15
    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: To draw a circle on a frame when the key is pressed

    You are not setting needToDrawCircle to true anywhere.
    Also you are not calling base class paintEvent function. Your paintevent shud be like -
    Qt Code:
    1. void drawForm::PaintEvent(QEvent* event)
    2. {
    3. QDialog::paintEvent(event)
    4. if(needToDrawCircle){
    5. QPainter painter(this);
    6. painter.drawEllipse(100,20,180,180);
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    Also try to study your code what you are doing..
    class drawForm : public QDialog, private Ui::drawForm
    Do you think its correct design ?

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

    Default Re: To draw a circle on a frame when the key is pressed

    HI,
    Thank u so much for your support If i write my code as shown below when i open my new form itself circle will be drawn but this should not happen when i press enter key that time only circle should be drawn i am new to qt as well as C++ programming so u suggest on what i need to concentrate more.

    void drawForm:aintEvent(QPaintEvent *event)

    {
    QDialog:aintEvent(event);
    if(needToDrawCircle){
    QPainter painter(this);
    painter.drawEllipse(100,20,180,180);
    }
    }

    How to i proceed further ?

    Regards,
    Soumya

  17. #17
    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: To draw a circle on a frame when the key is pressed

    If i write my code as shown below when i open my new form itself circle will be drawn but this should not happen
    I bet you have not initialized needToDrawCircle ... check it.

    i am new to qt as well as C++ programming so u suggest on what i need to concentrate more.
    Get thorough with C++ basics,,, and then Qt.. You cant use Qt without knowing C++.

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

    Smile Re: To draw a circle on a frame when the key is pressed

    Hi,

    Thank you so much for replying i have initiliazed to bool needTo DrawCircle = TRUE;

    Below is the .cpp code

    void drawForm::init()
    {
    needToDrawCircle = FALSE;
    }


    void drawForm::keyPressEvent( QKeyEvent * event )
    {
    qDebug( "Key Press Event" );
    switch ( event->key() )
    {
    case Qt::Key_F1:
    needToDrawCircle = TRUE;
    connect(drawlineEdit, SIGNAL(returnPressed()), frame, SLOT(update()));
    break;

    }
    }

    void drawForm:aintEvent(QPaintEvent *event)

    {
    QDialog:aintEvent(event);
    if(needToDrawCircle){
    QPainter painter(this);
    painter.drawEllipse(100,20,180,180);
    }
    }

    Below is the .h code
    public:
    needToDrawCircle;

    public slots:
    void slotEnterPressed()
    {
    needToDrawCircle = TRUE;
    }


    Now when i open my form, frame will be empty i need to press F1 to set needToDrawCircle = TRUE then if i press Enter circle will
    be drawn.

    Insted of F1if i use case Qt::Key_Enter then qframe will be empty nothing will be drawn

    Correct me if i am doing anything wrong and thank you once again

    Regards,
    Soumya

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

    Default Re: To draw a circle on a frame when the key is pressed

    Hi,

    Thank you so mush for your help now its working fine:-)

Similar Threads

  1. how to draw a circle on a frame in Qt-4
    By grsandeep85 in forum Qt Programming
    Replies: 1
    Last Post: 16th September 2009, 08:05
  2. How to draw a special circle pie
    By parnedo in forum Qt Programming
    Replies: 7
    Last Post: 3rd July 2009, 15:25
  3. How to draw a semi circle or arc of ellipse
    By parnedo in forum Qt Programming
    Replies: 2
    Last Post: 2nd July 2009, 01:39
  4. How to draw fast rectangle/triangle/circle
    By nileshsince1980 in forum Qt Programming
    Replies: 1
    Last Post: 11th November 2008, 11:40
  5. What is the fastest way to draw a circle ?
    By Vladimir in forum Qt Programming
    Replies: 18
    Last Post: 6th September 2007, 17:26

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.