当前位置: 代码迷 >> QT开发 >> QPainter:begin: Widget painting can only begin as a result of a paintEvent,该怎么解决
  详细解决方案

QPainter:begin: Widget painting can only begin as a result of a paintEvent,该怎么解决

热度:998   发布时间:2016-04-25 03:58:33.0
QPainter::begin: Widget painting can only begin as a result of a paintEvent
想把摄像头采集的数据放到QT上面显示!摄像头可以显示,但是总会出现这个错误,网上说要增加Qwidget的属性,但是不知道怎么加!!代码如下,希望大神指教一下

#include <QtGui>
#include "processImage.h"
#include "videodevice.h"
extern "C"
{
#include <stdio.h>
#include <stdlib.h>
}
ProcessImage::ProcessImage(QWidget *parent):QWidget(parent)
{
   //this->setAttribute(Qt::WA_PaintOutsidePaintEvent); 
   pp = (unsigned char *)malloc(320 * 240/*QWidget::width()*QWidget::height()*/* 3 * sizeof(char));
    painter = new QPainter(this); 
   frame = new QImage(pp,320,240,QImage::Format_RGB888);
   //frame = new QPixmap(640,240);
    label = new QLabel();
    vd = new VideoDevice(tr("/dev/video0"));

    connect(vd, SIGNAL(display_error(QString)), this,SLOT(display_error(QString)));
    rs = vd->open_device();
    if(-1==rs)
    {
        QMessageBox::warning(this,tr("error"),tr("open /dev/dsp error"),QMessageBox::Yes);
        vd->close_device();
    }

    rs = vd->init_device();
    if(-1==rs)
    {
        QMessageBox::warning(this,tr("error"),tr("init failed"),QMessageBox::Yes);
        vd->close_device();
    }

    rs = vd->start_capturing();
    if(-1==rs)
    {
        QMessageBox::warning(this,tr("error"),tr("start capture failed"),QMessageBox::Yes);
        vd->close_device();
    }

    if(-1==rs)
    {
        QMessageBox::warning(this,tr("error"),tr("get frame failed"),QMessageBox::Yes);
        vd->stop_capturing();
    }

    timer = new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(update()));
    timer->start(30);

    QHBoxLayout *hLayout = new QHBoxLayout();
    hLayout->addWidget(label);
    setLayout(hLayout);
    setWindowTitle(tr("Capture"));
}

ProcessImage::~ProcessImage()
{
    rs = vd->stop_capturing();
    rs = vd->uninit_device();
    rs = vd->close_device();
}

void ProcessImage::paintEvent(QPaintEvent *)
{
    rs = vd->get_frame((void **)&p,&len);
    convert_yuv_to_rgb_buffer(p,pp,320,240/*QWidget::width(),QWidget::height()*/);
     frame->loadFromData((uchar *)p,/*len*/320 * 240 * 3 * sizeof(char));

//    painter->begin(this);
//    painter->drawImage(0,0,*frame);
//    painter->end();
  相关解决方案