来自“QEvent*”的无效转换到“QPaintEvent*”
尝试查找触摸事件是否发生,或者只是绘制它们。
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.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
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.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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想调用
paintEvent
,则需要强制转换QEvent*
,例如:但正如其他人所说,如果您发现自己陷入困境,请不要抱怨无休止的重绘循环或卡住的事件循环。
如果您想要定期重新绘制,请设置一个
QTimer
并让它调用您的小部件的update()
槽。If you want to call
paintEvent
, you'll need to cast theQEvent*
, something like: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'supdate()
slot.直接调用 PaintEvent() 不是一个好习惯。请改为调用
repaint()
或update()
。这些方法随后将调用paintEvent()
> 使用正确的参数。Its not a good practice to call paintEvent() directly.Call
repaint()
orupdate()
instead.Those methods will then callpaintEvent()
with the right parameter.