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. QPrinter doesn't print with colors.

QPrinter doesn't print with colors.

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 388 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.
  • H Offline
    H Offline
    HenkCoder
    wrote on last edited by HenkCoder
    #1

    Hello, I'm currently making a notepad in Qt and I tried to print a file with colors.
    836ac905-e696-4b95-8d6a-ad8fa1b43757-image.png
    this is the file, sorry it's in italian.

    This is my code:

    void MainWindow::on_print_clicked()
    {
        //Getting the settings.json file
        QFile settings(QCoreApplication::applicationDirPath() + "/settings.json");
        QByteArray content;
        if(settings.open(QIODevice::ReadOnly | QIODevice::Text))
        {
            content = settings.readAll();
            settings.close();
        }
    
        QJsonParseError *errPars{new QJsonParseError()};
        QJsonDocument jsonDoc{QJsonDocument::fromJson(content, errPars)};
        QJsonObject obj{jsonDoc.object()};
    
        //Setting up the printer
        QPrinter printer;
        printer.setResolution(QPrinter::HighResolution);
        printer.setPageSize(QPageSize::A4);
        if(obj["printHorizontally"].toBool() == false)
            printer.setPageOrientation(QPageLayout::Portrait);
        else
            printer.setPageOrientation(QPageLayout::Landscape);
        if(obj["printWithColors"].toBool() == false)
            printer.setColorMode(QPrinter::GrayScale);
        else
            printer.setColorMode(QPrinter::Color);
        //Creating the page setup dialog
        QPageSetupDialog *pageSetup = new QPageSetupDialog(&printer, this);
        //Creating the printpreview dialog
        QPrintPreviewDialog *printPrev = new QPrintPreviewDialog(&printer);
        //QObject::connecting printPrev paint signal to print slot
        QObject::connect(printPrev, &QPrintPreviewDialog::paintRequested, this, &MainWindow::print);
        if(pageSetup->exec() == QDialog::Accepted)
        {
            printPrev->exec();
        }
    }
    
    //Print slot
    void MainWindow::print(QPrinter *p)
    {
        TextEdit *edit{getTabTextEdit()};
        edit->print(p);
    }
    

    So it checks if in the settings.json file the "printWithColors" is true, and it is.
    987a8531-b781-4667-a6b4-d7285f234a00-image.png
    Even if I've written printer.setColorMode(QPrinter::Color);, it still doesn't print in colors.
    I tried and everything is correct but it is all black.
    How can I solve this?

    artwawA 1 Reply Last reply
    0
    • H HenkCoder

      Hello, I'm currently making a notepad in Qt and I tried to print a file with colors.
      836ac905-e696-4b95-8d6a-ad8fa1b43757-image.png
      this is the file, sorry it's in italian.

      This is my code:

      void MainWindow::on_print_clicked()
      {
          //Getting the settings.json file
          QFile settings(QCoreApplication::applicationDirPath() + "/settings.json");
          QByteArray content;
          if(settings.open(QIODevice::ReadOnly | QIODevice::Text))
          {
              content = settings.readAll();
              settings.close();
          }
      
          QJsonParseError *errPars{new QJsonParseError()};
          QJsonDocument jsonDoc{QJsonDocument::fromJson(content, errPars)};
          QJsonObject obj{jsonDoc.object()};
      
          //Setting up the printer
          QPrinter printer;
          printer.setResolution(QPrinter::HighResolution);
          printer.setPageSize(QPageSize::A4);
          if(obj["printHorizontally"].toBool() == false)
              printer.setPageOrientation(QPageLayout::Portrait);
          else
              printer.setPageOrientation(QPageLayout::Landscape);
          if(obj["printWithColors"].toBool() == false)
              printer.setColorMode(QPrinter::GrayScale);
          else
              printer.setColorMode(QPrinter::Color);
          //Creating the page setup dialog
          QPageSetupDialog *pageSetup = new QPageSetupDialog(&printer, this);
          //Creating the printpreview dialog
          QPrintPreviewDialog *printPrev = new QPrintPreviewDialog(&printer);
          //QObject::connecting printPrev paint signal to print slot
          QObject::connect(printPrev, &QPrintPreviewDialog::paintRequested, this, &MainWindow::print);
          if(pageSetup->exec() == QDialog::Accepted)
          {
              printPrev->exec();
          }
      }
      
      //Print slot
      void MainWindow::print(QPrinter *p)
      {
          TextEdit *edit{getTabTextEdit()};
          edit->print(p);
      }
      

      So it checks if in the settings.json file the "printWithColors" is true, and it is.
      987a8531-b781-4667-a6b4-d7285f234a00-image.png
      Even if I've written printer.setColorMode(QPrinter::Color);, it still doesn't print in colors.
      I tried and everything is correct but it is all black.
      How can I solve this?

      artwawA Offline
      artwawA Offline
      artwaw
      wrote on last edited by
      #2

      @HenkCoder I would assume that content of your editor is being treated as plain text?

      For more information please re-read.

      Kind Regards,
      Artur

      H 1 Reply Last reply
      0
      • artwawA artwaw

        @HenkCoder I would assume that content of your editor is being treated as plain text?

        H Offline
        H Offline
        HenkCoder
        wrote on last edited by
        #3

        @artwaw I don't know, how do I make to save formatting infos?

        artwawA 1 Reply Last reply
        0
        • H HenkCoder

          @artwaw I don't know, how do I make to save formatting infos?

          artwawA Offline
          artwawA Offline
          artwaw
          wrote on last edited by
          #4

          @HenkCoder Try simple check. I assume that by TextEdit *edit{getTabTextEdit()}; you get the QTextEdit instance from the Ui, right? So I'd check acceptRichText() and the flags from autoFormatting().
          Also doing qDebug() << edit->toHtml(); will show you if formatting tags are present.
          On logic if you see rich text in the Ui it should also get printed, unless you mismatched printer settings or something.

          For more information please re-read.

          Kind Regards,
          Artur

          1 Reply Last reply
          2

          • Login

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