invalid conversion from ‘QEvent*’ to ‘QPaintEvent*’
trying to find if touch event occurs or else just paint them.
bool MyWidget::event(QEvent *event)
{
switch (event->type())
{
case QEvent::TouchBegin:
case QEvent::TouchUpdate:
case QEvent::TouchEnd:
{
QTouchEvent *touchEvent = static_cast(event);
if (touchEvent->touchPoints().count() == 2)
{
const QTouchEvent::TouchPoint &touchPoint1 = touchEvent->touchPoints().first();
const QTouchEvent::TouchPoint &touchPoint2 = touchEvent->touchPoints().last();
nx=touchPoint1.scenePos().x();
ny=touchPoint1.scenePos().y();
pix = QPixmap::grabWidget (this,nx,ny,1,1);
img = pix.toImage();
rgb = img.pixel(0,0);
color.setRgb(rgb);
drawBit=1;
}
break;
}
case QEvent::Paint:
return MyWidget::paintEvent( event);
break;
default:
return false;
break;
}
return true;
}
void MyWidget::paintEvent(QPaintEvent *event)
{
time_counter++;
for(i=0;(ired,b[i]->green,b[i]->blue,255), Qt::SolidPattern));
painter开发者_StackOverflow.drawEllipse(b[i]->x,b[i]->y,b[i]->w, b[i]->w);
painter.drawLine(b[i]->x+b[i]->w/2,b[i]->y+b[i]->w,b[i]->x+b[i]->w/2,b[i]->y+2*b[i]->w);
if(b[i]->ballDead==false)
b[i]->y+=b[i]->vy;
if(drawBit==1 && b[i]->red==color.red() && b[i]->green==color.green() && b[i]->blue==color.blue())
ballHit(i);
}
}
this code shows error like: mywidget.cpp:116:47: error: invalid conversion from ‘QEvent*’ to ‘QPaintEvent*’ mywidget.cpp:116:47: error: initializing argument 1 of ‘virtual void MyWidget::paintEvent(QPaintEvent*)’ mywidget.cpp:116:47: error: void value not ignored as it ought to be
If you want to call paintEvent
, you'll need to cast the QEvent*
, something like:
paintEvent(static_cast<QPaintEvent*>(event));
return true;
But as others have said, don't come complaining if you find yourself in an endless repaint loop or with an otherwise stuck event loop.
If you want a periodic repaint, set up a QTimer
and have it call you widget's update()
slot.
Its not a good practice to call paintEvent() directly.Call repaint()
or update()
instead.Those methods will then call paintEvent()
with the right parameter.
精彩评论