How to set QPdfView's viewport background color?
-
I'm trying to set a QPdfView's
viewportbackground 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.txtaccording 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,
viewportis grey, and you can see the white background ofQPdfViewon the borders:I also tried to change the background color of
viewportby 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
paintEventif possible. -
@Abderrahmene_Rayene
Have you tried what is described here?
Note: You have
backgroundand the example saysbackground-color.That painting the viewPort with a brush covers other content should be clear :)
@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 allEven 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
viewportbackground 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.txtaccording 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,
viewportis grey, and you can see the white background ofQPdfViewon the borders:I also tried to change the background color of
viewportby 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
paintEventif possible.@Abderrahmene_Rayene
Have you tried what is described here?
Note: You have
backgroundand 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
backgroundand the example saysbackground-color.That painting the viewPort with a brush covers other content should be clear :)
@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 allEven 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
backgroundand the example saysbackground-color.That painting the viewPort with a brush covers other content should be clear :)
@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
paintEventand trying to use aQPainterto paint a widget, it only clicked in my head after I kept messing with it.Thanks for you input!
-
C CPPUIX has marked this topic as solved on
-
@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 allEven 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 allEven 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 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::Darkseems 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::Darkseems to work.Just curious, I could move on without an answer if it's not possible to provide one.
Thanks again!
@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.

