来自“QEvent*”的无效转换到“QPaintEvent*”

发布于 2024-11-15 01:59:15 字数 1947 浏览 4 评论 0原文

尝试查找触摸事件是否发生,或者只是绘制它们。


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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何以笙箫默 2024-11-22 01:59:15

如果您想调用paintEvent,则需要强制转换QEvent*,例如:

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

但正如其他人所说,如果您发现自己陷入困境,请不要抱怨无休止的重绘循环或卡住的事件循环。

如果您想要定期重新绘制,请设置一个QTimer并让它调用您的小部件的update()槽。

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.

孤单情人 2024-11-22 01:59:15

直接调用 PaintEvent() 不是一个好习惯。请改为调用 repaint()update()。这些方法随后将调用 paintEvent() > 使用正确的参数。

Its not a good practice to call paintEvent() directly.Call repaint() or update() instead.Those methods will then call paintEvent() with the right parameter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文