2011-06-13 4 views
1

터치 이벤트가 발생했는지 확인하려고하거나 아니면 그냥 페인트합니다.'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 
+0

아마 을 잊어 버리셨습니까? – red1ynx

+1

그리고'return MyWidget :: paintEvent (event);'는'bool' 대신에'void'를 리턴하기 때문에 쓸 수 없습니다. – red1ynx

+0

터치 이벤트를 처리 한 다음 나머지 핸들러에 대한 상위 처리기의 결과를 반환합니다. return QWidget :: event (event);'다른 이벤트 유형의 * all *을 올바르게 처리합니다. –

답변

3

그것의 안 좋은 관행은 다음 오른쪽 매개 변수 paintEvent()를 호출합니다 paintEvent() directly.Call repaint() 또는 update() instead.Those 메소드를 호출합니다. 당신이 paintEvent를 호출 할 경우

+0

하지만 다시 MyWidget :: event()에 의해 수집됩니다. 그것은 모든 이벤트를 수집합니다. 그래서 어떻게 든 paintEvent()를 호출해야합니다. – user767060

+0

당신은 아무것도하지 말아야합니다 .. 단지'event()'메소드에서 QEvent :: Paint case를 처리하지 마십시오. repaint/update를 호출하면 (자), paintEvent 메소드가 불려갑니다. – Abhijith

+0

나는 이벤트 함수를 호출하려고했지만 작동하지 않았다. 어디에서 그것을 – user767060

3

, 당신은 QEvent* 캐스팅과 같이해야합니다 :

paintEvent(static_cast<QPaintEvent*>(event)); 
return true; 

을하지만 다른 사람이 말했듯이, 당신은 끝없는 다시 그리기 루프에서 자신을 발견하면 불평 오지 않는다 그렇지 않으면 막힌 이벤트 루프로 인해 발생합니다.

주기적으로 다시 그리려면 QTimer을 설정하고 위젯의 update() 슬롯이라고 부릅니다.

관련 문제