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 html page margin problem (QTextDocument, QPrintPreviewDialog)
Qt 6.11 is out! See what's new in the release blog

Print html page margin problem (QTextDocument, QPrintPreviewDialog)

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 4.1k Views 2 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.
  • M Offline
    M Offline
    mschmit
    wrote on last edited by
    #1

    I tried following code snippets:

    QPrintPreviewDialog *dialog = new QPrintPreviewDialog(&printer);
    dialog->setWindowTitle("Druckansicht");
    
    dialog->setGeometry(20,30,1280,900);
    
    connect(dialog,SIGNAL(paintRequested(QPrinter*)),this,SLOT(print(QPrinter*)));
    dialog->exec();
    

    void MainWindow::print(QPrinter *printer)
    {
    QFile file(_htmlFilePath);

    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        qWarning("fehler datei nicht gefunden: ");
        qWarning(_htmlFilePath.toStdString().c_str());
        std::cout << file.isOpen() << file.fileName().toStdString().c_str() << std::endl;
        exit(EXIT_FAILURE);
    }
    
    QString htmlContent;
    QTextStream in(&file);
    in.setCodec("UTF-8");
    htmlContent = in.readAll();
    
    QTextDocument *doc = new QTextDocument();
    doc->setHtml(htmlContent);
    doc->print(printer);
    

    }

    The printpreview is shown. It is possible to change the page paper size and the oriantation of the page
    When i try to change the page margins in the page setup nothing happens.
    Is that a bug or do i something wrong?

    K 1 Reply Last reply
    0
    • M mschmit

      I tried following code snippets:

      QPrintPreviewDialog *dialog = new QPrintPreviewDialog(&printer);
      dialog->setWindowTitle("Druckansicht");
      
      dialog->setGeometry(20,30,1280,900);
      
      connect(dialog,SIGNAL(paintRequested(QPrinter*)),this,SLOT(print(QPrinter*)));
      dialog->exec();
      

      void MainWindow::print(QPrinter *printer)
      {
      QFile file(_htmlFilePath);

      if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
      {
          qWarning("fehler datei nicht gefunden: ");
          qWarning(_htmlFilePath.toStdString().c_str());
          std::cout << file.isOpen() << file.fileName().toStdString().c_str() << std::endl;
          exit(EXIT_FAILURE);
      }
      
      QString htmlContent;
      QTextStream in(&file);
      in.setCodec("UTF-8");
      htmlContent = in.readAll();
      
      QTextDocument *doc = new QTextDocument();
      doc->setHtml(htmlContent);
      doc->print(printer);
      

      }

      The printpreview is shown. It is possible to change the page paper size and the oriantation of the page
      When i try to change the page margins in the page setup nothing happens.
      Is that a bug or do i something wrong?

      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @mschmit

      What OS are you using?

      My guess is that you are using the native dialog for your preview. Therefore, I personally doubt that this is influenced by Qt. Such things are most of the time dependent on the actual driver used.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      2
      • M Offline
        M Offline
        mschmit
        wrote on last edited by
        #3

        we use windows in my case windows 7 x64.

        the print preview is the QPrintPreviewDialog and not the OS specific dialog. Or do you mean the page setup dialog?

        How can i fix or solve this problem?

        K 1 Reply Last reply
        0
        • M mschmit

          we use windows in my case windows 7 x64.

          the print preview is the QPrintPreviewDialog and not the OS specific dialog. Or do you mean the page setup dialog?

          How can i fix or solve this problem?

          K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          @mschmit

          AFAIK general dialogs as file dialog, printer dialog, preview and so on are the native dialogs of the OS in your case windows. Qt is providing only an identical interface for use on different platforms (OS). The options of your dialog will change sometimes with the actual printer driver, since not every printer has the same options.
          Based on my experience changing page size and orientation is typically the easy stuff and not of a major problem. With margins it is typically more complicated. Also WinWord for instance cannot always handle that stuff accurately. For one printer you set every thing up, but when changing the printer everything is messed up and you might even find a solution.

          You can check the actual values of pageLayout set for the current printer. You can also ensure in QTextDocument that there are no different margins or other layout setups.

          Vote the answer(s) that helped you to solve your issue(s)

          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