PDA

View Full Version : To draw a circle on a frame when the key is pressed



soumya
3rd February 2010, 05:19
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

navi1084
3rd February 2010, 05:29
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

nikhilqt
3rd February 2010, 07:14
Check out this link, this has how to draw a circle,

http://doc.trolltech.com/4.3/painting-transformations.html

soumya
3rd February 2010, 07:25
thank you i will check it out and get back to you

soumya
4th February 2010, 07:22
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

soumya
4th February 2010, 07:36
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

aamer4yu
4th February 2010, 07:38
All painting is supposed to be done only in paintEvent function.

soumya
4th February 2010, 07:50
so how to call paintEvent function inside update so that when i press a key a circle can be drawn

Lykurg
4th February 2010, 07:57
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

soumya
5th February 2010, 09:51
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

aamer4yu
5th February 2010, 11:31
Do you read the replies ? How many times have you been told to draw only in paintEvent function ???

soumya
5th February 2010, 11:57
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

aamer4yu
5th February 2010, 12:26
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 -

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

// void paintEvent(QPaintEvent *event);

:confused:


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

myWidget::paintEvent()
{
if(needToDrawCircle)
// draw circle.
}

//and in ur slot -
myWidget::slotEneterPressed()
{
needToDrawCircle = true;
}

and you will need more member variables to store the radius and position of circle if necessary.

soumya
8th February 2010, 06:53
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::paintEvent()

{
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

aamer4yu
8th February 2010, 06:58
You are not setting needToDrawCircle to true anywhere.
Also you are not calling base class paintEvent function. Your paintevent shud be like -

void drawForm::PaintEvent(QEvent* event)
{
QDialog::paintEvent(event)
if(needToDrawCircle){
QPainter painter(this);
painter.drawEllipse(100,20,180,180);
}
}

Also try to study your code what you are doing..

class drawForm : public QDialog, private Ui::drawForm
Do you think its correct design ?

soumya
9th February 2010, 04:36
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::paintEvent(QPaintEvent *event)

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

How to i proceed further ?

Regards,
Soumya

aamer4yu
9th February 2010, 05:35
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++.

soumya
9th February 2010, 10:16
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::paintEvent(QPaintEvent *event)

{
QDialog::paintEvent(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

soumya
9th February 2010, 10:21
Hi,

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