1 Attachment(s)
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
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
Re: To draw a circle on a frame when the key is pressed
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
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
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
Re: To draw a circle on a frame when the key is pressed
All painting is supposed to be done only in paintEvent function.
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
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
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
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 ???
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
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.
Quote:
ya i know that draw can be done only in paintevent function
Can u then explain the code -
Code:
void circleForm::repaint(){
painter.drawEllipse(100,20,180,180);
}
// void paintEvent(QPaintEvent *event);
:confused:
Quote:
. 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 -
Code:
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.
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::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
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 -
Code:
void drawForm
::PaintEvent(QEvent* event
) {
if(needToDrawCircle){
painter.drawEllipse(100,20,180,180);
}
}
Also try to study your code what you are doing..
Quote:
class drawForm : public QDialog, private Ui::drawForm
Do you think its correct design ?
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::paintEvent(QPaintEvent *event)
{
QDialog::paintEvent(event);
if(needToDrawCircle){
QPainter painter(this);
painter.drawEllipse(100,20,180,180);
}
}
How to i proceed further ?
Regards,
Soumya
Re: To draw a circle on a frame when the key is pressed
Quote:
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.
Quote:
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++.
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::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
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:-)