Displaying QImage on screen



  • I currently pass the "bits" pointer of a QImage to our video decoder, which renders incoming video frames into this buffer. I have a timer that goes off and, if the frame has been changed, "updates" the widget to blit the bits to the window:

    @void VideoWidget::paintEvent(QPaintEvent *)
    {
    QPainter p(this);

    p.drawImage(imagex,imagey,*videoImage);
    

    }
    @

    This works pretty well, although I wonder if it is the most efficient. But now I want to add scroll bars to the widget so that if it is resized to be smaller than the QImage, it shows scroll bars instead of scaling it to the size of the widget. I'm not having too much luck with creating a scroll area with a label on the widget and using the same algorithm as the above:

    @void VideoWidget::VideoWidget()
    {
    ...
    vpix = QPixmap::fromImage(*videoImage);
    ui->imageLabel->setPixmap(vpix);
    ...
    }

    void VideoWidget::paintEventx(QPaintEvent *)
    {
    if ( !vpix.isNull() )
    {
    QPainter p(this->ui->imageLabel->pixmap());

        p.drawImage(imagex,imagey,*videoImage);
    }
    

    }
    @

    Any other way of doing this?



  • Well, FWIW, this seems to work, although I'm not sure how efficient it is:

    @void VideoWidget::DoUpdate()
    {
    vpix.convertFromImage(*videoImage);
    ui->imageLabel->setPixmap(vpix);
    repaint();
    }
    @

    I'm not sure why I need to keep calling setPixmap. Why doesn't my update to it just show up when I repaint? If I take out the setPixmap, no updates show on the screen.



  • Hi jdarnold,

    this->ui->imageLabel->pixmap() returns a const QPixmap*. This means a pointer to a const pixmap. So I think, it's clear why it does not work. Although, the label will not know, that it's contents was changed.



  • Yes, I realized soon after posting the first one that the code wasn't even compiling :( But, after more tweaking, I ended up with the second bit of code and now I'm wondering why I need to keep calling setPixmap. I've commented out the repaint() call and it still works, so it is the setPixmap that does the heavy lifting. I'm wondering how inefficient it is, as I do this a lot. Intuitively, I would think that just updating the pixmap and, perhaps, doing a repaint, should update the image on the screen, but it does not.



  • Still wondering if there is a more efficient way of rapidly blitting a QImage to the screen, as this current method seems to be pretty slow.



  • Look into using OpenGL instead.



  • You know, that's a great idea. We probably need more efficient graphics than can be provided with the native libraries.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.