【发布时间】:2011-06-13 12:08:59
【问题描述】:
尝试查找是否发生触摸事件,或者只是绘制它们。
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);
}
}
此代码显示如下错误:
mywidget.cpp:116:47:错误:从“QEvent*”到“QPaintEvent*”的无效转换
mywidget.cpp:116:47:错误:初始化“virtual void MyWidget::paintEvent(QPaintEvent*)”的参数 1
mywidget.cpp:116:47: 错误: void 值没有被忽略,因为它应该被忽略
【问题讨论】:
-
也许你忘了包含
? -
你不能写
return MyWidget::paintEvent( event);,因为它返回void而不是bool。 -
您可能只想处理触摸事件,然后为其余的返回父处理程序的结果:
return QWidget::event( event );这将正确处理所有其他事件类型。
标签: qt events qt4 qpainter touch-event