Do you own a Debenu Quick PDF Library 12/11/10/9/8/7? Upgrade to Debenu Quick PDF Library 13!

Foxit Quick PDF Library

Frequently Asked Question:

Return to FAQ Index

Stop flickering when rendering with Qt

Question

I have a PDF viewer working in my C++ app using the trial version of this PDF SDK. The only problem is that it flickers when scrolling.

To put this in context, the application is written in Qt, which itself uses double buffering to avoid flickering. However, since a display context is used for RenderPageToDC, the buffering needs to be turned off.

Basically a still render is just fine... its just when it updates (e.g. moving or changing size) that there is a visible issue.

Answer

The right way to paint flicker free with Quick PDF Library and Qt is:

void MyPage::paintEvent(QPaintEvent *event)
{
/* mPage is the page number */
    mRenderer->SelectPage(mPage);

    /* mCache is a QPixmap member for the page. */
    if (mCache.isNull()) {
        QSize sz = sizeHint();

        HDC dc = getDC();
        HDC bufDC = CreateCompatibleDC(dc);
        releaseDC(dc);
        mCache = QPixmap(sz);
        HBITMAP bufBMP = mCache.toWinHBITMAP();
        SelectObject(bufDC, bufBMP);
        mRenderer->RenderPageToDC(72, mPage, (int)bufDC);
        mCache = QPixmap::fromWinHBITMAP(bufBMP);
        DeleteObject(bufBMP);
        DeleteDC(bufDC);
    }

    QPainter p(this);
    p.drawPixmap(event->rect(), mCache, event->rect());
}

This removes any flickering since it only has to blit the sub-rect that changes, allowing Qt to do clever mem-move and other double buffering activities. For very large PDF files you'll likely have to be a bit smarter about dropping pixmaps from cache, but that is a simple optimization for later (QCache).


© 2015 Debenu & Foxit. All rights reserved. AboutBuyContactBlogNewsletterSupportFAQProduct UpdatesForum