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. print preview widget
Forum Updated to NodeBB v4.3 + New Features

print preview widget

Scheduled Pinned Locked Moved Solved General and Desktop
21 Posts 2 Posters 9.2k 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.
  • R Offline
    R Offline
    rest
    wrote on last edited by rest
    #4

    @mrjj solved, but the size of the preview i can't change it and is in the same line as the toolbar from where i do the printaction, where could be the problem?

    mrjjM 1 Reply Last reply
    1
    • R rest

      @mrjj solved, but the size of the preview i can't change it and is in the same line as the toolbar from where i do the printaction, where could be the problem?

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #5

      @rest
      Super.
      The widgets render in the size they have/are.
      If you want it bigger, you could print to image and scale it.
      You can maybe even do it directly with render and painter.scale but
      i never tested this.

      void MainWindow::printshot() {
        QPixmap pix = QPixmap::grabWindow(QApplication::desktop()->winId());
        QPrinter printer(QPrinter::HighResolution);
        printer.setOrientation(QPrinter::Landscape);
      
        QPainter painter;
        painter.begin(&printer);
        double xscale = printer.pageRect().width() / double(pix.width());
        double yscale = printer.pageRect().height() / double(pix.height());
        double scale = qMin(xscale, yscale);
        painter.translate(printer.paperRect().x() + printer.pageRect().width() / 2,
                          printer.paperRect().y() + printer.pageRect().height() / 2);
        painter.scale(scale, scale);
        painter.translate(-width() / 2, -height() / 2); // note uses the form width/height! use pix.h/w if random image
        painter.drawPixmap(0, 0, pix);
        painter.end();
      }
      

      If you need very high fidelity, split your paintEvent into a new function taking a QPainter *
      and call it for printing too. That way you can scale to use the full DPI of the printer.

      • and is in the same line as the toolbar from where i do the printaction
        I do not understand this line :)
      R 1 Reply Last reply
      3
      • mrjjM mrjj

        @rest
        Super.
        The widgets render in the size they have/are.
        If you want it bigger, you could print to image and scale it.
        You can maybe even do it directly with render and painter.scale but
        i never tested this.

        void MainWindow::printshot() {
          QPixmap pix = QPixmap::grabWindow(QApplication::desktop()->winId());
          QPrinter printer(QPrinter::HighResolution);
          printer.setOrientation(QPrinter::Landscape);
        
          QPainter painter;
          painter.begin(&printer);
          double xscale = printer.pageRect().width() / double(pix.width());
          double yscale = printer.pageRect().height() / double(pix.height());
          double scale = qMin(xscale, yscale);
          painter.translate(printer.paperRect().x() + printer.pageRect().width() / 2,
                            printer.paperRect().y() + printer.pageRect().height() / 2);
          painter.scale(scale, scale);
          painter.translate(-width() / 2, -height() / 2); // note uses the form width/height! use pix.h/w if random image
          painter.drawPixmap(0, 0, pix);
          painter.end();
        }
        

        If you need very high fidelity, split your paintEvent into a new function taking a QPainter *
        and call it for printing too. That way you can scale to use the full DPI of the printer.

        • and is in the same line as the toolbar from where i do the printaction
          I do not understand this line :)
        R Offline
        R Offline
        rest
        wrote on last edited by
        #6

        @mrjj thank you so much but solved it last night, was just a problem when i was using the constructor for the printpreview

        R 1 Reply Last reply
        2
        • R rest

          @mrjj thank you so much but solved it last night, was just a problem when i was using the constructor for the printpreview

          R Offline
          R Offline
          rest
          wrote on last edited by
          #7

          @mrjj but question, when i trigger print i should get an sample.pdf saved somewherE?

          mrjjM 1 Reply Last reply
          0
          • R rest

            @mrjj but question, when i trigger print i should get an sample.pdf saved somewherE?

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #8

            @rest
            yes if u set the filename and type to pdf
            printer.setOutputFormat(QPrinter::PdfFormat);
            printer.setOutputFileName(filename);

            1 Reply Last reply
            2
            • R Offline
              R Offline
              rest
              wrote on last edited by
              #9

              @mrjj , i set all this in an action print preview where i have also preview->print;
              preview->show . but i dont get an pdf saved nowhere

              1 Reply Last reply
              0
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by mrjj
                #10

                @mrjj said in print preview widget:

                printer.setOutputFileName(filename);

                you control where with setOutputFileName
                make sure to use / and not \ for the full path to the file

                1 Reply Last reply
                1
                • R Offline
                  R Offline
                  rest
                  wrote on last edited by
                  #11

                  void MainWindow::on_actionPrint_triggered()
                  {
                  QPrinter *printer = new QPrinter(QPrinter::HighResolution);
                  printer->setOutputFormat(QPrinter::PdfFormat);

                  printer->setPaperSize(QPrinter::A4);
                  printer->setOrientation(QPrinter::Landscape);
                  printer->setFullPage(true);
                  //printer->setResolution(100);
                  printer->setOutputFileName("Desktop//aaaa.pdf");

                  QPrintPreviewWidget *preview =0;
                  preview = new QPrintPreviewWidget(printer,preview);

                  preview->print();

                  connect(preview, SIGNAL(paintRequested(QPrinter*)),allWorkspaceShapes[currentIndex]->drawShape,SLOT(Print(QPrinter*)));

                  preview->show();

                  }

                  i used this and still didn't get anything, i have another problem in the code maybe?

                  mrjjM 1 Reply Last reply
                  0
                  • R rest

                    void MainWindow::on_actionPrint_triggered()
                    {
                    QPrinter *printer = new QPrinter(QPrinter::HighResolution);
                    printer->setOutputFormat(QPrinter::PdfFormat);

                    printer->setPaperSize(QPrinter::A4);
                    printer->setOrientation(QPrinter::Landscape);
                    printer->setFullPage(true);
                    //printer->setResolution(100);
                    printer->setOutputFileName("Desktop//aaaa.pdf");

                    QPrintPreviewWidget *preview =0;
                    preview = new QPrintPreviewWidget(printer,preview);

                    preview->print();

                    connect(preview, SIGNAL(paintRequested(QPrinter*)),allWorkspaceShapes[currentIndex]->drawShape,SLOT(Print(QPrinter*)));

                    preview->show();

                    }

                    i used this and still didn't get anything, i have another problem in the code maybe?

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by mrjj
                    #12

                    @rest said in print preview widget:
                    That is NOT a valid path
                    setOutputFileName("Desktop//aaaa.pdf");

                    Please use a valid full path and only single /

                    like
                    C:/Users/SOMEUSERNAME/Desktop/test.pdf

                    1 Reply Last reply
                    1
                    • R Offline
                      R Offline
                      rest
                      wrote on last edited by
                      #13

                      printer->setOutputFileName("C:/Users/admin/Desktop/test.pdf");
                      i tried and nothing :(

                      mrjjM 1 Reply Last reply
                      0
                      • R rest

                        printer->setOutputFileName("C:/Users/admin/Desktop/test.pdf");
                        i tried and nothing :(

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by mrjj
                        #14

                        @rest

                        and you do have write access there ?
                        and the path IS real. meaning u didnt make up the admin name.

                        1 Reply Last reply
                        0
                        • R Offline
                          R Offline
                          rest
                          wrote on last edited by
                          #15

                          @mrjj and then i used this and still does not work
                          printer->setOutputFileName("C:/Users/Public/Public Documents/test.pdf");

                          mrjjM 1 Reply Last reply
                          0
                          • R rest

                            @mrjj and then i used this and still does not work
                            printer->setOutputFileName("C:/Users/Public/Public Documents/test.pdf");

                            mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by
                            #16

                            @rest
                            and it does show stuff in preview ?
                            but the file is NOT created?

                            1 Reply Last reply
                            0
                            • R Offline
                              R Offline
                              rest
                              wrote on last edited by
                              #17

                              exactly, it shows but it's not saved

                              mrjjM 1 Reply Last reply
                              0
                              • R rest

                                exactly, it shows but it's not saved

                                mrjjM Offline
                                mrjjM Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on last edited by
                                #18

                                @rest
                                and u press print inside preview ?
                                to make it actually print and not just preview ?

                                R 1 Reply Last reply
                                0
                                • mrjjM mrjj

                                  @rest
                                  and u press print inside preview ?
                                  to make it actually print and not just preview ?

                                  R Offline
                                  R Offline
                                  rest
                                  wrote on last edited by
                                  #19

                                  @mrjj solved, i had make the print preview and printer object in the constructor of the window

                                  mrjjM 1 Reply Last reply
                                  0
                                  • R rest

                                    @mrjj solved, i had make the print preview and printer object in the constructor of the window

                                    mrjjM Offline
                                    mrjjM Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #20

                                    @rest
                                    oh, so moving them helped?

                                    R 1 Reply Last reply
                                    0
                                    • mrjjM mrjj

                                      @rest
                                      oh, so moving them helped?

                                      R Offline
                                      R Offline
                                      rest
                                      wrote on last edited by
                                      #21

                                      @mrjj yes, thanks

                                      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