Thanks, I should have thought to look in the code, but that's straightforward enough. I'm with you, I don't think it should use QPalette because the default should definitely be white regardless of whether or not you are using dark mode. I'm thinking separate functions to set/get the background color would be best. It seems most pdf viewers have an option to change the background color for accessibility, so that seems like a reasonable option. I'll suggest it. The change would be fairly easy.

Josh

void QPdfView::paintEvent(QPaintEvent *event)
{
    Q_D(QPdfView);

    QPainter painter(viewport());
    painter.fillRect(event->rect(), palette().brush(QPalette::Dark));
    painter.translate(-d->m_viewport.x(), -d->m_viewport.y());

    for (auto it = d->m_documentLayout.pageGeometryAndScale.cbegin();
         it != d->m_documentLayout.pageGeometryAndScale.cend(); ++it) {
        const QRect pageGeometry = it.value().first;
        if (pageGeometry.intersects(d->m_viewport)) { // page needs to be 
painted
            painter.fillRect(pageGeometry, Qt::white);

So the answer for now is no.  You can write up a suggestion on bugreports.qt.io 
if you want.  Perhaps the page background should use a palette color.  But 
then, dark mode would be problematic, wouldn't it?
 If the page background is dark, and the PDF also has dark text, we’d get a bug 
report right away, now that dark mode is popular.  

I also hate trying to read bright-white PDFs in dark rooms when I’m trying to 
have most of the onscreen content be dark.  It can make my eyes go into a weird 
mode where I see jagged-edged round-ish rainbows
for a while, and can’t read anything for a few minutes.  (Does anybody know 
what that’s called?)  But PDF is a simulation of paper.. and paper is usually 
white, so the content usually has dark text.  In
another PDF viewer, I once tried to solve that problem with a shader.  (That’s 
also tricky: you want to only replace the background color itself, and the main 
text color, which is likely black but perhaps
not.  So the fragment shader can invert any color that is a shade of grey, but 
leave the rest as they are.  And often it will work well enough, but not 
always.) It’s just that if we shipped such a feature,
there would probably be more bug reports, I suppose.

It’s easier in Qt Quick though.  Image { source: “file.pdf”; currentFrame: 5 } 
will give you page 5 (starting from 0, so probably page 6) with no background 
at all (unless the PDF does its own background
fill): whatever is behind it will show through.  (Window is white by default 
though.)  The packaged viewer components are much more complex than that, of 
course.  You can experiment by forking one of those
QML files (depending whether you want to scroll through all the pages, or just 
one page at a time).  
https://code.qt.io/cgit/qt/qtwebengine.git/tree/src/pdfquick

      On Dec 5, 2024, at 00:58, Joshua Grauman <jnf...@grauman.com> wrote:

Hello,

I generate pdfs by using QPainter to paint onto a QPrinter. My understanding is 
that the generated pdf has a transparent background, even though most viewers 
view the pdf with a white background. I am
able to view the pdf with a different color background with Okular for example. 
I'm wondering if there is any way to change the background color of QPdfView 
(to black)? I tried changing the palette of
QPdfView or change it's style sheet but those didn't do it. Does anyone know if 
there is any way to display the pdf with a different background color using 
QPdfView?

Josh

_______________________________________________
Interest mailing list
Interest@qt-project.org
https://lists.qt-project.org/listinfo/interest



_______________________________________________
Interest mailing list
Interest@qt-project.org
https://lists.qt-project.org/listinfo/interest

Reply via email to