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. QPdfView how to select text by mouse

QPdfView how to select text by mouse

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 88 Views 1 Watching
  • 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.
  • S Offline
    S Offline
    songziming
    wrote last edited by
    #1

    I can load and render a PDF file using QPdfView:

    // inside MainWindow constructor
    QPdfView *pdf = new QPdfView();
    pdf->setPageMode(QPdfView::PageMode::MultiPage);
    pdf->setZoomMode(QPdfView::ZoomMode::FitToWidth);
    setCentralWidget(pdf);
    
    QPdfDocument *doc = new QPdfDocument(this);
    pdf->setDocument(doc);
    doc->load("C:\\path\\to\\sample.pdf");
    

    How to make text selectable using mouse?

    Do I need to convert mousePress/Move/Release position to pdf coordinate (and how)? And manually construct QPdfSelection object using start/end location?

    JonBJ 1 Reply Last reply
    0
    • S songziming

      I can load and render a PDF file using QPdfView:

      // inside MainWindow constructor
      QPdfView *pdf = new QPdfView();
      pdf->setPageMode(QPdfView::PageMode::MultiPage);
      pdf->setZoomMode(QPdfView::ZoomMode::FitToWidth);
      setCentralWidget(pdf);
      
      QPdfDocument *doc = new QPdfDocument(this);
      pdf->setDocument(doc);
      doc->load("C:\\path\\to\\sample.pdf");
      

      How to make text selectable using mouse?

      Do I need to convert mousePress/Move/Release position to pdf coordinate (and how)? And manually construct QPdfSelection object using start/end location?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote last edited by JonB
      #2

      @songziming
      Googling QPdfView select text gives e.g. https://askubuntu.com/questions/1071771/where-is-the-text-selection-tool-in-qpdfview. And see https://forum.qt.io/topic/115458/how-can-i-select-and-copy-text-from-a-qpdfview-widget.

      S 1 Reply Last reply
      0
      • JonBJ JonB

        @songziming
        Googling QPdfView select text gives e.g. https://askubuntu.com/questions/1071771/where-is-the-text-selection-tool-in-qpdfview. And see https://forum.qt.io/topic/115458/how-can-i-select-and-copy-text-from-a-qpdfview-widget.

        S Offline
        S Offline
        songziming
        wrote last edited by
        #3

        @JonB I mean the class QPdfView in module QtPDF, not the standalone program qpdfviewer.

        The link you give is about the program.

        1 Reply Last reply
        0
        • KH-219DesignK Offline
          KH-219DesignK Offline
          KH-219Design
          wrote last edited by
          #4

          (disclaimer: I have not used either the standalone program nor the QPdfView class)

          Although @JonB appears to have provided links that refer to the standalone utility viewer, said viewer is open source and built using Qt. Therefore, (if no better option or no quicker tip appears in this forum), we can probably find clues about how to achieve text selection by looking at the code for the standalone utility and learning how it achieves this.

          Source code: https://git.launchpad.net/qpdfview

          (Learning from source code of qpdfview utility may or may not have been what @JonB had in mind, but either way the point stands. I do apologize if I am appearing to just "copy" or "regurgitate" JonB's answer as my own.)

          www.219design.com
          Software | Electrical | Mechanical | Product Design

          1 Reply Last reply
          2
          • I Online
            I Online
            IgKh
            wrote last edited by
            #5

            It is not an easy ask.

            The first challenge is obtaining the QPdfSelection. Those can't be created directly, but QPdfDocument::getSelection is mostly what you want. The problem is that it needs to be called for a single page, and the start and end points are relative to the top-left of the page.

            That means that you need access to the geometry of the pages (for each page, its' bounding rectangle in viewport coordinates) after the mouse is released and you have the selected rectangle (in widget or viewport coordinates). That is needed to determine which pages intersect with the selection and to convert the intersection with each into page coordinates. QPdfView of course manages that data, but does not expose it.

            It can be reasonably independently calculated from information in the QPdfDocument together with information about the current screen DPI. But if you do that, you'll probably recognize like me that that is actually most of what QPdfView does, so you might as well stop using it and implement your own viewing widget anyway.

            The second and larger challenge is providing feedback to the user as to what text they selected. QPdfSelection::bounds() has that information, but showing it in the traditional style of rendering the background of the selected text in the highlight color and the selected text itself in the highlighted text color can't really be done with the QPdf module. I think that most PDF readers do that correctly by adding a transient annotation to the PDF and feeding that to the rendering pipeline, but there are no facilities for that in the module.

            The bottom line is that QPdfView is really convenient for what it does, but if you need anything even slightly more advanced than that - you should look at a more full featured PDF library like Poppler or MuPDF.

            1 Reply Last reply
            1

            • Login

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