PDA

View Full Version : To set focus on circumference circles when key is pressed



soumya
18th February 2010, 08:02
Hi All,

When key is pressed i need to get a focus on smaller circles which is on the circumference of big circle as shown in the attachment .When i press F11 key it should rotate in anticlockwise directions form right hole to next hole similarly if i press F10 key it should rotate in clockwise direction form right hole to previous hole. I have written the code for it its entering the particular function i am getting the debug message on the console but its not getting focused on smaller circles.

Here is the .cpp code as shown below,



void circleForm::keyPressEvent( QKeyEvent *event )
{
// qDebug( "Key Press Event" );
switch ( event->key() )
{
case Qt::Key_F11:
nextBoltHoleCircle = TRUE;
break;
case Qt::Key_F10:
prevBoltHoleCircle = TRUE;
break;
case Qt::Key_Enter:
needToDrawCircle = TRUE;
break;
default:
QWidget::keyPressEvent(event);
}
}


void circleForm::PaintEvent(QPaintEvent *event){
QDialog::PaintEvent(event);
if(needToDrawCircle){
QPainter painter(this);
painter.setPen(QPen(Qt::blue, 1, Qt::DashDotDotLine));
painter.drawLine(0,180,380,180);//horizontal line
painter.drawLine(190,0,190,360);//vertical line
painter.setPen(QPen(Qt::black, 2, Qt::SolidLine));

int framecentre_x = 190;
int framecentre_y = 180;
painter.drawPoint(framecentre_x,framecentre_y);//centrepoint
painter.drawEllipse(framecentre_x-100,framecentre_y-100,200,200);//big circle(90,80)
int i;
double x;
angle = 360.00/Hol;
for(i = 0;i < Hol;i++ ){
x = (angle * i);
qDebug(" x = %lf\n",x);
xangle = (x * PI)/180;
qDebug("xangle = %lf",xangle);
xcordinate = cosine(xangle);
qDebug("xcordinate = %lf",xcordinate);
yangle = (x * PI)/180;
qDebug("yangle = %lf",yangle);
ycordinate = sine(yangle);
qDebug("ycordinate = %lf",ycordinate);
painter.setPen(QPen(Qt::black, 2, Qt::SolidLine));
painter.drawEllipse((190 + 100 * xcordinate)- 10,(180 + 100 * ycordinate)- 10,20,20);//working
}
painter.setBrush(QBrush(Qt::red, Qt::SolidPattern));
painter.drawEllipse((190 + 100 * 1)- 10,(180 + 100 * 0)- 10,20,20);

}
needToDrawCircle = FALSE;

if(prevBoltHoleCircle){
QPainter painter(this);
qDebug("painting bolt hole circle\n");
qDebug("xcordinate = %lf",xcordinate);
qDebug("ycordinate = %lf",ycordinate);
painter.setBrush(QBrush(Qt::blue, Qt::SolidPattern));
painter.drawEllipse((190 + 100 * xcordinate)- 10,(180 + 100 * ycordinate)- 10,20,20);
qDebug("PREVIOUS\n");

}
prevBoltHoleCircle = FALSE;
if(nextBoltHoleCircle){
QPainter painter(this);
qDebug("painting bolt hole circle\n");
qDebug("xcordinate = %lf",xcordinate);
qDebug("ycordinate = %lf",ycordinate);
painter.setBrush(QBrush(Qt::blue, Qt::SolidPattern));
painter.drawEllipse((190 + 100 * xcordinate)- 10,(180 + 100 * ycordinate)- 10,20,20);
qDebug("NEXT\n");

}
nextBoltHoleCircle = FALSE;
}


Sould i use signal slot mechanism ???..To draw a big circle on the frame i had used signal slot mechanism after entering the value in the lineEdit when i press enter key a circle should be drawn something like this which i have do now when i press F11 it should focus to next circle and if i press F10 it should focus to previous circle.Any help or suggestions

Thanks in advance
Soumya

wysota
19th February 2010, 11:10
First of all it is paintEvent() and not PaintEvent(). Second of all you can't assume the paint event fill only be called once after your key press event. You should keep a state of your instrument in the widget class (i.e. as the number of circle being considered as "focused") and update the state in your key press event. Then when the widget is redrawn it has to pickup the state and draw itself accordingly. The state has to be persistent and not volatile (i.e. you are not allowed to change any variables being part of the state in the paint event).

soumya
22nd February 2010, 06:43
Hello Sir,

Thanks for the reply.Instead of using F10 and F11 if i use Key_Enter and Key_Return it works fine. The small circles which is on the circumference of big circle works fine and rotate in clockwise and antoclockwise direction when key is pressed but why not for F10 and F11?..Please give me some suggestions to proceed further

wysota
22nd February 2010, 09:10
The keys may be intercepted globally by some other application (like the desktop or the system itself) but my remarks on the paint event still stand.

high_flyer
22nd February 2010, 10:21
t why not for F10 and F11?
The first thing to test is to see if your application does indeed the get key events.
Set a break point in your keyPressEvent(), and see if they are being caught.

soumya
23rd February 2010, 04:46
Thanks for everyone. Need to call update function now its working fine and below is the code snippet


switch ( event->key() )
{
case Qt::Key_F11:
nextBoltHoleCircle = TRUE;
update();
break;
case Qt::Key_F10:
prevBoltHoleCircle = TRUE;
update();
break;
case Qt::Key_Enter:
needToDrawCircle = TRUE;
update();
break;
default:
QWidget::keyPressEvent(event);
}


Best Regards,
Soumya

wysota
23rd February 2010, 15:04
Could you tell me if your program works properly if you press F10 or F11 and then start resizing the dialog?

soumya
25th February 2010, 08:00
ya its working fine

wysota
25th February 2010, 17:25
With the paintEvent() you posted earlier? Or did you change it? Could you post the complete code of your widget here as an attachment?

soumya
26th February 2010, 05:15
Hi,

I have sent the entire code in the attachment it works fine

Regards,
Soumya

wysota
27th February 2010, 09:16
I'm afraid your application doesn't work on my system, I can't see any "holes" and you disabled resizing the dialog so I don't know how could you say that your application works when the dialog is resized :) The code is broken, believe me.