Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to set QPdfView's viewport background color?

How to set QPdfView's viewport background color?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 927 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CPPUIX
    wrote on 27 May 2023, 20:31 last edited by CPPUIX
    #1

    I'm trying to set a QPdfView's viewport background color, but unlike QAbstractScrollArea (from which QPdfView'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 your CMakeLists.txt according the below, to be able to use QPdfView:

    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 of QPdfView on the borders:

    PdfView with stylesheet

    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.

    P 1 Reply Last reply 28 May 2023, 03:47
    0
    • P Pl45m4
      28 May 2023, 03:47

      @Abderrahmene_Rayene

      Have you tried what is described here?

      • https://doc.qt.io/qt-6/stylesheet-examples.html#customizing-qabstractscrollarea

      Note: You have background and the example says background-color.

      That painting the viewPort with a brush covers other content should be clear :)

      C Offline
      C Offline
      ChrisW67
      wrote on 28 May 2023, 05:15 last edited by ChrisW67
      #3

      @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();
      }
      

      53299272-79b8-4de3-b911-037fcf50deb9-image.png

      C 2 Replies Last reply 28 May 2023, 06:44
      3
      • C CPPUIX
        27 May 2023, 20:31

        I'm trying to set a QPdfView's viewport background color, but unlike QAbstractScrollArea (from which QPdfView'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 your CMakeLists.txt according the below, to be able to use QPdfView:

        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 of QPdfView on the borders:

        PdfView with stylesheet

        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.

        P Offline
        P Offline
        Pl45m4
        wrote on 28 May 2023, 03:47 last edited by
        #2

        @Abderrahmene_Rayene

        Have you tried what is described here?

        • https://doc.qt.io/qt-6/stylesheet-examples.html#customizing-qabstractscrollarea

        Note: You have background and the example says background-color.

        That painting the viewPort with a brush covers other content should be clear :)


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        C C 2 Replies Last reply 28 May 2023, 05:15
        0
        • P Pl45m4
          28 May 2023, 03:47

          @Abderrahmene_Rayene

          Have you tried what is described here?

          • https://doc.qt.io/qt-6/stylesheet-examples.html#customizing-qabstractscrollarea

          Note: You have background and the example says background-color.

          That painting the viewPort with a brush covers other content should be clear :)

          C Offline
          C Offline
          ChrisW67
          wrote on 28 May 2023, 05:15 last edited by ChrisW67
          #3

          @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();
          }
          

          53299272-79b8-4de3-b911-037fcf50deb9-image.png

          C 2 Replies Last reply 28 May 2023, 06:44
          3
          • P Pl45m4
            28 May 2023, 03:47

            @Abderrahmene_Rayene

            Have you tried what is described here?

            • https://doc.qt.io/qt-6/stylesheet-examples.html#customizing-qabstractscrollarea

            Note: You have background and the example says background-color.

            That painting the viewPort with a brush covers other content should be clear :)

            C Offline
            C Offline
            CPPUIX
            wrote on 28 May 2023, 06:41 last edited by
            #4

            @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 a QPainter to paint a widget, it only clicked in my head after I kept messing with it.

            Thanks for you input!

            1 Reply Last reply
            0
            • C CPPUIX has marked this topic as solved on 28 May 2023, 06:41
            • C ChrisW67
              28 May 2023, 05:15

              @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();
              }
              

              53299272-79b8-4de3-b911-037fcf50deb9-image.png

              C Offline
              C Offline
              CPPUIX
              wrote on 28 May 2023, 06:44 last edited by CPPUIX
              #5

              @ChrisW67 Thank you! That does the trick, it fills the background without obscuring the open document, and I don't have to use paintEvent!

              Here's how it looks:

              fixed.png

              1 Reply Last reply
              0
              • C ChrisW67
                28 May 2023, 05:15

                @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();
                }
                

                53299272-79b8-4de3-b911-037fcf50deb9-image.png

                C Offline
                C Offline
                CPPUIX
                wrote on 28 May 2023, 06:57 last edited by
                #6

                @ChrisW67

                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's paintEvent?

                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!

                C 1 Reply Last reply 28 May 2023, 21:57
                0
                • C CPPUIX
                  28 May 2023, 06:57

                  @ChrisW67

                  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's paintEvent?

                  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!

                  C Offline
                  C Offline
                  ChrisW67
                  wrote on 28 May 2023, 21:57 last edited by
                  #7

                  @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.

                  C 1 Reply Last reply 28 May 2023, 22:02
                  1
                  • C ChrisW67
                    28 May 2023, 21:57

                    @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.

                    C Offline
                    C Offline
                    CPPUIX
                    wrote on 28 May 2023, 22:02 last edited by
                    #8

                    @ChrisW67 Thanks a lot! This clarifies it for me.

                    1 Reply Last reply
                    0

                    1/8

                    27 May 2023, 20:31

                    • Login

                    • Login or register to search.
                    1 out of 8
                    • First post
                      1/8
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved