void VideoPreviewer::paintEvent(QPaintEvent* event) { Q_UNUSED(event); QPainter painter(this); // Copy the image so the lock isn't held while actually drawing the image. // This is fast because QImage is reference counted. QImage img; QSize source_size; { SharedLock lock(&m_shared_data); img = lock->m_image; source_size = lock->m_source_size; } if(!img.isNull()) { // draw the image // Scaling is only used if the widget was resized after the image was captured, which is unlikely // except when the video is paused. That's good because the quality after Qt's scaling is horrible. QSize out_size = CalculateScaledSize(source_size, QSize(width() - 2, height() - 2)); QPoint out_pos((width() - out_size.width()) / 2, (height() - out_size.height()) / 2); QRect out_rect(out_pos, out_size); painter.drawImage(out_rect, img); // draw the border painter.setPen(Qt::black); painter.setBrush(Qt::NoBrush); painter.drawRect(out_rect.adjusted(-1, -1, 0, 0)); } }
void VideoPreviewer::paintEvent(QPaintEvent* event) { Q_UNUSED(event); QPainter painter(this); // copy the image data so the lock isn't held while actually drawing the image std::shared_ptr<TempBuffer<uint8_t> > image_buffer; int image_stride; QSize image_size, source_size; { SharedLock lock(&m_shared_data); image_buffer = lock->m_image_buffer; // shared pointer copy is cheap image_stride = lock->m_image_stride; image_size = lock->m_image_size; source_size = lock->m_source_size; } if(image_buffer != NULL) { // create image (data is not copied) QImage img(image_buffer->GetData(), image_size.width(), image_size.height(), image_stride, QImage::Format_RGB32); // draw the image // Scaling is only used if the widget was resized after the image was captured, which is unlikely // except when the video is paused. That's good because the quality after Qt's scaling is horrible. QSize out_size = CalculateScaledSize(source_size, QSize(width() - 2, height() - 2)); QPoint out_pos((width() - out_size.width()) / 2, (height() - out_size.height()) / 2); QRect out_rect(out_pos, out_size); painter.drawImage(out_rect, img); // draw the border painter.setPen(Qt::black); painter.setBrush(Qt::NoBrush); painter.drawRect(out_rect.adjusted(-1, -1, 0, 0)); } }