How to set QPdfView's viewport background color?
-
wrote on 27 May 2023, 20:31 last edited by CPPUIX
I'm trying to set a QPdfView's
viewport
background color, but unlikeQAbstractScrollArea
(from whichQPdfView
's derived), using stylesheet does not work.Here's how I tried to do that:
#include <QApplication> #include <QPdfView> int main(int argc, char *argv[]) { QApplication a(argc, argv); QPdfView *pdfView = new QPdfView(); pdfView->setMinimumSize(100,100); //I tried to set stylesheet in different ways, in multiple combinations //but none worked //pdfView->setStyleSheet("background: white"); pdfView->setStyleSheet("QWidget{background: white;}"); //pdfView->viewport()->setStyleSheet("background: white"); pdfView->show(); return a.exec(); }
Note: You need to to find and target
PdfWidgets
, so modify yourCMakeLists.txt
according the below, to be able to useQPdfView
:find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets PdfWidgets) find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets PdfWidgets) target_link_libraries(MyExperiments PRIVATE Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::PdfWidgets)
Here's how it looks,
viewport
is grey, and you can see the white background ofQPdfView
on the borders:I also tried to change the background color of
viewport
by overriding QAbstractScrollArea::paintEvent, and here's how:void paintEvent(QPaintEvent *event) { QPdfView::paintEvent(event); QPainter p(viewport()); QRect rect = viewport()->rect(); rect.adjust(-1,-1,0,0); p.setBrush(QBrush(QColor(0,0,100))); p.drawRect(rect); }
But that ended up painting over
viewport
, not changing its background color, so it covers open documents, because If I use a transparent color, I can see the document through it.I'm open to either or other ways, but I'd like to avoid using
paintEvent
if possible. -
@Abderrahmene_Rayene
Have you tried what is described here?
Note: You have
background
and the example saysbackground-color
.That painting the viewPort with a brush covers other content should be clear :)
wrote on 28 May 2023, 05:15 last edited by ChrisW67@Pl45m4 That does not work here. The viewer appears to fill its viewport widget interior with a default dark grey. So, these do not fly:
QPdfView view; view.setStyleSheet("QWidget {background-color: lime;} "); // ^^ Results in a thin green line at the edges of the grey fill view.viewport()->setStyleSheet("background-color: lime;"); // ^^ No visible effect at all
Even replacing the viewport with a generic widget or setting the palette QPalette::Window brush does not appear to achieve the goal.
Edit: Inspection of the QPdfView source leads to this result:
#include <QApplication> #include <QPdfView> #include <QPalette> int main(int argc, char **argv) { QApplication app(argc, argv); QPdfView view; QPalette palette = view.palette(); palette.setBrush(QPalette::Dark, QColor("lime")); view.setPalette(palette); view.show(); return app.exec(); }
-
I'm trying to set a QPdfView's
viewport
background color, but unlikeQAbstractScrollArea
(from whichQPdfView
's derived), using stylesheet does not work.Here's how I tried to do that:
#include <QApplication> #include <QPdfView> int main(int argc, char *argv[]) { QApplication a(argc, argv); QPdfView *pdfView = new QPdfView(); pdfView->setMinimumSize(100,100); //I tried to set stylesheet in different ways, in multiple combinations //but none worked //pdfView->setStyleSheet("background: white"); pdfView->setStyleSheet("QWidget{background: white;}"); //pdfView->viewport()->setStyleSheet("background: white"); pdfView->show(); return a.exec(); }
Note: You need to to find and target
PdfWidgets
, so modify yourCMakeLists.txt
according the below, to be able to useQPdfView
:find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets PdfWidgets) find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets PdfWidgets) target_link_libraries(MyExperiments PRIVATE Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::PdfWidgets)
Here's how it looks,
viewport
is grey, and you can see the white background ofQPdfView
on the borders:I also tried to change the background color of
viewport
by overriding QAbstractScrollArea::paintEvent, and here's how:void paintEvent(QPaintEvent *event) { QPdfView::paintEvent(event); QPainter p(viewport()); QRect rect = viewport()->rect(); rect.adjust(-1,-1,0,0); p.setBrush(QBrush(QColor(0,0,100))); p.drawRect(rect); }
But that ended up painting over
viewport
, not changing its background color, so it covers open documents, because If I use a transparent color, I can see the document through it.I'm open to either or other ways, but I'd like to avoid using
paintEvent
if possible.wrote on 28 May 2023, 03:47 last edited by@Abderrahmene_Rayene
Have you tried what is described here?
Note: You have
background
and the example saysbackground-color
.That painting the viewPort with a brush covers other content should be clear :)
-
@Abderrahmene_Rayene
Have you tried what is described here?
Note: You have
background
and the example saysbackground-color
.That painting the viewPort with a brush covers other content should be clear :)
wrote on 28 May 2023, 05:15 last edited by ChrisW67@Pl45m4 That does not work here. The viewer appears to fill its viewport widget interior with a default dark grey. So, these do not fly:
QPdfView view; view.setStyleSheet("QWidget {background-color: lime;} "); // ^^ Results in a thin green line at the edges of the grey fill view.viewport()->setStyleSheet("background-color: lime;"); // ^^ No visible effect at all
Even replacing the viewport with a generic widget or setting the palette QPalette::Window brush does not appear to achieve the goal.
Edit: Inspection of the QPdfView source leads to this result:
#include <QApplication> #include <QPdfView> #include <QPalette> int main(int argc, char **argv) { QApplication app(argc, argv); QPdfView view; QPalette palette = view.palette(); palette.setBrush(QPalette::Dark, QColor("lime")); view.setPalette(palette); view.show(); return app.exec(); }
-
@Abderrahmene_Rayene
Have you tried what is described here?
Note: You have
background
and the example saysbackground-color
.That painting the viewPort with a brush covers other content should be clear :)
wrote on 28 May 2023, 06:41 last edited by@Pl45m4 said in How to set QPdfView's viewport background color?:
That painting the viewPort with a brush covers other content should be clear
It's my first timing overriding a
paintEvent
and trying to use aQPainter
to paint a widget, it only clicked in my head after I kept messing with it.Thanks for you input!
-
-
@Pl45m4 That does not work here. The viewer appears to fill its viewport widget interior with a default dark grey. So, these do not fly:
QPdfView view; view.setStyleSheet("QWidget {background-color: lime;} "); // ^^ Results in a thin green line at the edges of the grey fill view.viewport()->setStyleSheet("background-color: lime;"); // ^^ No visible effect at all
Even replacing the viewport with a generic widget or setting the palette QPalette::Window brush does not appear to achieve the goal.
Edit: Inspection of the QPdfView source leads to this result:
#include <QApplication> #include <QPdfView> #include <QPalette> int main(int argc, char **argv) { QApplication app(argc, argv); QPdfView view; QPalette palette = view.palette(); palette.setBrush(QPalette::Dark, QColor("lime")); view.setPalette(palette); view.show(); return app.exec(); }
-
@Pl45m4 That does not work here. The viewer appears to fill its viewport widget interior with a default dark grey. So, these do not fly:
QPdfView view; view.setStyleSheet("QWidget {background-color: lime;} "); // ^^ Results in a thin green line at the edges of the grey fill view.viewport()->setStyleSheet("background-color: lime;"); // ^^ No visible effect at all
Even replacing the viewport with a generic widget or setting the palette QPalette::Window brush does not appear to achieve the goal.
Edit: Inspection of the QPdfView source leads to this result:
#include <QApplication> #include <QPdfView> #include <QPalette> int main(int argc, char **argv) { QApplication app(argc, argv); QPdfView view; QPalette palette = view.palette(); palette.setBrush(QPalette::Dark, QColor("lime")); view.setPalette(palette); view.show(); return app.exec(); }
wrote on 28 May 2023, 06:57 last edited byI have a follow up question, just trying to understand.
Why is it needed to match the palette's color role with the one used in
QPdfView
'spaintEvent
?I messed with it, and only
QPalette::Dark
seems to work.Just curious, I could move on without an answer if it's not possible to provide one.
Thanks again!
-
I have a follow up question, just trying to understand.
Why is it needed to match the palette's color role with the one used in
QPdfView
'spaintEvent
?I messed with it, and only
QPalette::Dark
seems to work.Just curious, I could move on without an answer if it's not possible to provide one.
Thanks again!
wrote on 28 May 2023, 21:57 last edited by@Abderrahmene_Rayene The first couple lines in the QPdfView::paintEvent() do this:
QPainter painter(viewport()); painter.fillRect(event->rect(), palette().brush(QPalette::Dark));
Create a painter on the widget's viewport() and fill it using whatever brush is in the current palette' s QPalette::Dark position. This palette entry is hard-coded in QPdfView so the only control you have is to change the view (or viewport()) palette's QPalette::Dark entry.
-
@Abderrahmene_Rayene The first couple lines in the QPdfView::paintEvent() do this:
QPainter painter(viewport()); painter.fillRect(event->rect(), palette().brush(QPalette::Dark));
Create a painter on the widget's viewport() and fill it using whatever brush is in the current palette' s QPalette::Dark position. This palette entry is hard-coded in QPdfView so the only control you have is to change the view (or viewport()) palette's QPalette::Dark entry.
1/8