Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. QPainter howto draw Table
Qt 6.11 is out! See what's new in the release blog

QPainter howto draw Table

Scheduled Pinned Locked Moved Solved Qt Creator and other tools
16 Posts 4 Posters 13.4k 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.
  • mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by mrjj
    #7

    Hi
    You can do it like this ( among many ways)
    Credits to @Ni.Sumi Sumi for base function

    void MainWindow::on_pushButton_released() {
      PrintWidget(ui->tableWidget);
    }
    
    void MainWindow::PrintWidget(QWidget* widget) {
    
      QPixmap pix(widget->size());
      QPainter painter(&pix);
      widget->render(&painter);
      painter.end();
      QPrinter printer(QPrinter::HighResolution);
      printer.setOrientation(QPrinter::Landscape);
      printer.setOutputFormat(QPrinter::PdfFormat);
      printer.setPaperSize(QPrinter::A4);
      printer.setOutputFileName("test.pdf"); // will be in build folder
    
      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(-widget->width() / 2, -widget->height() / 2);
      painter.drawPixmap(0, 0, pix);
      painter.end();
    }
    
    

    Just disable the pdf part to have it on paper.

    Screen:alt text

    output:
    alt text

    Test project
    https://www.dropbox.com/s/kvgo5fissc9i9t0/printtable.zip?dl=0

    M 1 Reply Last reply
    4
    • mrjjM mrjj

      Hi
      You can do it like this ( among many ways)
      Credits to @Ni.Sumi Sumi for base function

      void MainWindow::on_pushButton_released() {
        PrintWidget(ui->tableWidget);
      }
      
      void MainWindow::PrintWidget(QWidget* widget) {
      
        QPixmap pix(widget->size());
        QPainter painter(&pix);
        widget->render(&painter);
        painter.end();
        QPrinter printer(QPrinter::HighResolution);
        printer.setOrientation(QPrinter::Landscape);
        printer.setOutputFormat(QPrinter::PdfFormat);
        printer.setPaperSize(QPrinter::A4);
        printer.setOutputFileName("test.pdf"); // will be in build folder
      
        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(-widget->width() / 2, -widget->height() / 2);
        painter.drawPixmap(0, 0, pix);
        painter.end();
      }
      
      

      Just disable the pdf part to have it on paper.

      Screen:alt text

      output:
      alt text

      Test project
      https://www.dropbox.com/s/kvgo5fissc9i9t0/printtable.zip?dl=0

      M Offline
      M Offline
      MrLibya
      wrote on last edited by
      #8

      @mrjj said in QPainter howto draw Table:

      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(-widget->width() / 2, -widget->height() / 2);
      painter.drawPixmap(0, 0, pix);
      painter.end();

      i got : alt text

      code :

      QTableWidget * test = new QTableWidget(this);
          int row = 0;
          for(MAPEmployeesTable::const_iterator it = mEmployeesTable->begin(); it != mEmployeesTable->end(); ++it){
              test->setRowCount(row+1);
              test->setItem(row,0,new QTableWidgetItem(it->second.getName()));
              test->setItem(row,1,new QTableWidgetItem(it->second.getBsalary()));
              test->setItem(row,2,new QTableWidgetItem(it->second.getAccommBonus()));
              test->setItem(row,3,new QTableWidgetItem(it->second.getDiscriminBonus()));
              test->setItem(row,4,new QTableWidgetItem(it->second.getDelegatBonus()));
              test->setItem(row,5,new QTableWidgetItem(it->second.getTeachingBonus()));
              test->setItem(row,6,new QTableWidgetItem(it->second.getReward()));
              test->setItem(row,7,new QTableWidgetItem(it->second.getNOWBonus()));
              row++;
          }
      
          QPrinter printer(QPrinter::HighResolution);
          printer.setOutputFileName("print.pdf");
          printer.setOrientation(QPrinter::Landscape);
          printer.setOutputFormat(QPrinter::PdfFormat);
          printer.setPageSize(QPrinter::A4);
      
          QPixmap pix(test->size());
          QPainter painter(&pix);
          test->render(&painter);
          painter.end();
      
          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(-test->width() / 2, -test->height() / 2);
          painter.drawPixmap(0, 0, pix);
          painter.end();
      
          delete test;
      
      1 Reply Last reply
      0
      • M Offline
        M Offline
        MrLibya
        wrote on last edited by
        #9
        This post is deleted!
        1 Reply Last reply
        0
        • M Offline
          M Offline
          MrLibya
          wrote on last edited by
          #10

          the problem above is cuz i'm working on new QTableWidget without giveing it any size , so i try this on another table wodget and the result :

          alt text

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

            well it will have a default size then.
            And it seems it has scrollbars too.
            try to resize it to have the needed width.

            M 1 Reply Last reply
            0
            • mrjjM mrjj

              well it will have a default size then.
              And it seems it has scrollbars too.
              try to resize it to have the needed width.

              M Offline
              M Offline
              MrLibya
              wrote on last edited by
              #12

              @mrjj there will be another problem , so as u can see the render fucntion it's not take the table items..etc it just take a screenshot for the table
              i don't want the header & scroolbar , also when i've more then 10 rows ( then i will have a scroolbar ) the render is only take screenshot for what is visible ( maybe only first 5 rows ) , i will have table with over 1k rows so it should be in every page maybe there 20 or 30 based on paper height , it's there any another class can do same this ?

              also if i make it by HTML code is't ok ? ( while i think this is very stupid method)

              1 Reply Last reply
              0
              • M Offline
                M Offline
                MrLibya
                wrote on last edited by
                #13
                This post is deleted!
                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  MrLibya
                  wrote on last edited by
                  #14
                  This post is deleted!
                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    MrLibya
                    wrote on last edited by
                    #15

                    http://www.qtrpt.tk/?page=doc_qtrpt.php#usinginproject
                    that's is what i was looking for :)
                    thx all for help

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

                      Oh you have 1000 rows. Yes then report generator is surely the way to go as it would have been a huge image to show all them at once :)

                      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