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. [SOLVED] PDF Print in multiple pages
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] PDF Print in multiple pages

Scheduled Pinned Locked Moved General and Desktop
22 Posts 5 Posters 15.4k Views 4 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
    mrjj
    Lifetime Qt Champion
    wrote on 3 Feb 2018, 12:43 last edited by
    #21

    Hi
    Maybe the /t way is too simple.
    For a better table look , you can cheat and use HTML

    bool createConnection() {
      QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
      db.setDatabaseName(":memory:");
      if (!db.open()) {
        QMessageBox::critical(0, qApp->tr("Cannot open database"), "Click Cancel to exit.", QMessageBox::Cancel);
        return false;
      }
      QSqlQuery query;
      qDebug() << "table:" <<   query.exec("create table person (id int primary key, "
                                           "firstname varchar(20), lastname varchar(20), num int )");
      query.exec("insert into person values(101, 'Dennis', 'Young','1')");
      query.exec("insert into person values(102, 'Christine', 'Holand','2')");
      query.exec("insert into person values(103, 'Lars junior', 'Gordon','4')");
      query.exec("insert into person values(104, 'Roberto', 'Robitaille','5')");
      query.exec("insert into person values(105, 'Maria', 'Papadopoulos','3')");
      return true;
    }
    
    // credits to. (i adabted from his)  https://stackoverflow.com/questions/3147030/qtableview-printing/4079676#4079676
    
    void PrintTable( QPrinter* printer, QSqlQuery&  Query ) {
      QString strStream;
      QTextStream out(&strStream);
    
      const int rowCount = Query.size();
      const int columnCount = Query.record().count();
    
      out <<  "<html>\n"
          "<head>\n"
          "<meta Content=\"Text/html; charset=Windows-1251\">\n"
          <<  QString("<title>%1</title>\n").arg("TITLE OF TABLE")
          <<  "</head>\n"
          "<body bgcolor=#ffffff link=#5000A0>\n"
          "<table border=1 cellspacing=0 cellpadding=2>\n";
    
      // headers
      out << "<thead><tr bgcolor=#f0f0f0>";
      for (int column = 0; column < columnCount; column++)
        out << QString("<th>%1</th>").arg(Query.record().fieldName(column));
      out << "</tr></thead>\n";
    
      while (Query.next()) {
        out << "<tr>";
        for (int column = 0; column < columnCount; column++) {
          QString data = Query.value(column).toString();
          out << QString("<td bkcolor=0>%1</td>").arg((!data.isEmpty()) ? data : QString("&nbsp;"));
        }
        out << "</tr>\n";
      }
    
      out <<  "</table>\n"
          "</body>\n"
          "</html>\n";
    
      QTextDocument document;
      document.setHtml(strStream);
      document.print(printer);
    
    }
    
    void print() {
      QPrinter printer(QPrinter::HighResolution);
      printer.setOrientation(QPrinter::Portrait);
      printer.setPageSize(QPrinter::A4);
      printer.setOutputFormat(QPrinter::PdfFormat);
     //  printer.setOutputFileName("e:/file.pdf"); // just for me testing
     QPrintDialog dlg(&printer, 0);
     if(dlg.exec() == QDialog::Accepted) {
      QSqlQuery query;
      query.exec("SELECT * from person");
      PrintTable(&printer, query);
     }
    }
    ...
    //To use
    // you dont need that just makes my DB createConnection();
      print();
    

    And then you can get this
    alt text

    1 Reply Last reply
    1
    • F filipdns
      3 Feb 2018, 11:07

      @mrjj Hello I try that but I got error :
      erreur : C2819: type 'QPainter' does not have an overloaded member 'operator ->'
      erreur : C2232: '->QPainter::drawText': left operand has 'class' type, use '.'
      erreur : C2065: 'x': undeclared identifier
      erreur : C2065: 'y': undeclared identifier
      erreur : C3861: 'width': identifier not found
      erreur : C3861: 'height': identifier not found

      void print()
      
      {
      
      
          QPrinter printer(QPrinter::HighResolution);
          //printer.setResolution(1200);
          printer.setOrientation(QPrinter::Portrait);
          printer.setPageSize(QPrinter::A4);
          QPrintDialog *dlg = new QPrintDialog(&printer,0);
          if(dlg->exec() == QDialog::Accepted) {
              //QPainter::Antialiasing;
              QPainter::TextAntialiasing;
              QPainter painter(&printer);
              QSqlQuery query;
              query.exec("SELECT date_etape, immatriculation FROM flight_log");
              while (query.next()) {
                  QString date_etape = query.value(0).toString();
                  QString immatriculation = query.value(1).toString();
                  QString text = date_etape +"\t" + immatriculation ; // make one row line with tabs
                  painter->drawText(x, y, width, height, Qt::TextExpandTabs , text); // fix width, height
                  //     that would print one "row" tabbed
              }
      
              painter.end();
          }
      
      J Online
      J Online
      jsulm
      Lifetime Qt Champion
      wrote on 5 Feb 2018, 07:07 last edited by
      #22

      @filipdns said in [SOLVED] PDF Print in multiple pages:

      QPainter painter(&printer);

      Your painter is not a pointer, so change

      painter->drawText(x, y, width, height, Qt::TextExpandTabs , text);
      

      to

      painter.drawText(x, y, width, height, Qt::TextExpandTabs , text);
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1

      21/22

      3 Feb 2018, 12:43

      • Login

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