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. Cannot move to next page to print HTML content with QPrinter
Qt 6.11 is out! See what's new in the release blog

Cannot move to next page to print HTML content with QPrinter

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 1.3k 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.
  • L Offline
    L Offline
    LeeMinh
    wrote on last edited by
    #1

    In my small application, I want to preview some HTML content in each page so I used the following code

    @
    /*

    • Handle events when clicking button to preview content
      */
      void MainWindow::on_pushButton_clicked()
      {
      QPrinter printer;
      printer.setPaperSize(QPrinter::A4);
      printer.setOrientation(QPrinter::Portrait);
      printer.setFullPage(true);

      QPrintPreviewDialog printPreview = new QPrintPreviewDialog(&printer);
      connect(printPreview, SIGNAL(paintRequested(QPrinter
      )), this, SLOT(printAllTitle(QPrinter*)));

      printPreview->setWindowTitle("Demo");
      Qt::WindowFlags flags(Qt::WindowTitleHint);
      printPreview->setWindowFlags(flags);
      printPreview->showMaximized();
      printPreview->exec();
      }

    /*

    • Show preview content
      */
      void MainWindow::printAllTitle(QPrinter *printer)
      {
      QVector<QString> titles;
      titles.push_back("Title 1");
      titles.push_back("Title 2");

      QString strStream;
      QTextStream out(&strStream);

      for (int i = 0; i < titles.size(); i++) {
      out << "<html><head></head><body>";
      out << "<p style="font-size:20pt">" + titles.at(i) + "</p>";
      out << "</body></html>";

        printer->newPage();   // Don't move the next page !!!
      

      }

      QTextDocument *document = new QTextDocument();
      document->setHtml(strStream);
      document->print(printer);
      delete document;
      }
      @

    And the result page I got

    !http://i.stack.imgur.com/5qATo.png(Cannot move the next page)!

    After testing the result page many times, I realized that the Printer didn't move the next page to print HTML content.

    How can I solve this issue?

    Thanks!

    1 Reply Last reply
    0
    • R Offline
      R Offline
      Rahul Das
      wrote on last edited by
      #2

      @Calling newPage() on an inactive QPrinter object will always fail. @

      So, may be you should check the PrinterState before you call newPage.


      Declaration of (Platform) independence.

      1 Reply Last reply
      0

      • Login

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