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. PRINTING A DISPLAYED TABLE FROM DATABASE
Forum Updated to NodeBB v4.3 + New Features

PRINTING A DISPLAYED TABLE FROM DATABASE

Scheduled Pinned Locked Moved Unsolved General and Desktop
20 Posts 4 Posters 3.4k Views 3 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.
  • GREYONG GREYON

    @Pablo-J-Rogina okey thanks,I will try it dear

    Pl45m4P Offline
    Pl45m4P Offline
    Pl45m4
    wrote on last edited by Pl45m4
    #10

    @GREYON

    Just to add:

    PUPILS_PAID_SCH_FEE_BY_GRADE is a horrible name for any c++ class. Name your widgets after what they are, not what content they display, since the content could change. In addition it's way too long, as you can see from how your cpp file looks like :)

    PupilFeeDialog for example, would be much shorter and easier to handle.


    If debugging is the process of removing software bugs, then programming must be the process of putting them in.

    ~E. W. Dijkstra

    GREYONG 1 Reply Last reply
    4
    • Pl45m4P Pl45m4

      @GREYON

      Just to add:

      PUPILS_PAID_SCH_FEE_BY_GRADE is a horrible name for any c++ class. Name your widgets after what they are, not what content they display, since the content could change. In addition it's way too long, as you can see from how your cpp file looks like :)

      PupilFeeDialog for example, would be much shorter and easier to handle.

      GREYONG Offline
      GREYONG Offline
      GREYON
      wrote on last edited by
      #11

      @Pl45m4 thanks noted

      1 Reply Last reply
      0
      • Pablo J. RoginaP Pablo J. Rogina

        @GREYON said in PRINTING A DISPLAYED TABLE FROM DATABASE:

        Now want to be able to print these tables

        You may want to take a look at QtRPT report generator. Spoiler alert: I haven't used it myself.

        GREYONG Offline
        GREYONG Offline
        GREYON
        wrote on last edited by
        #12

        @Pablo-J-Rogina
        So where and how is the function PrintTable() declared?

        1 Reply Last reply
        0
        • mrjjM mrjj

          Hi
          The code prints the actual widget so it cannot be used if many items as then not all are printed.
          You can use the way Gojir4 shows using a QTextDocument or using html as i did here
          https://forum.qt.io/topic/52652/solved-pdf-print-in-multiple-pages/21

          In any case you have to control the printing yourself and if you have many items so that cannot be on one page then you also need to handle new pages etc.

          GREYONG Offline
          GREYONG Offline
          GREYON
          wrote on last edited by
          #13

          @mrjj how and where is function PrintTable declared in your example?

          mrjjM 1 Reply Last reply
          0
          • GREYONG GREYON

            @mrjj how and where is function PrintTable declared in your example?

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #14

            @GREYON

            Its there. :)
            just scroll the code. there are more than shown first.

            alt text

            GREYONG 2 Replies Last reply
            1
            • mrjjM mrjj

              @GREYON

              Its there. :)
              just scroll the code. there are more than shown first.

              alt text

              GREYONG Offline
              GREYONG Offline
              GREYON
              wrote on last edited by
              #15

              @mrjj
              I can just see the function call,or maybe it's in the.h file?

              mrjjM 1 Reply Last reply
              0
              • GREYONG GREYON

                @mrjj
                I can just see the function call,or maybe it's in the.h file?

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by mrjj
                #16

                @GREYON

                Im not sure what you see ?
                it should all be there. just need to scroll more :)

                void PrintTable( QPrinter* printer, QSqlQuery& Query )
                is in any case.

                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(" "));
                }
                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);
                }
                }

                1 Reply Last reply
                1
                • mrjjM mrjj

                  @GREYON

                  Its there. :)
                  just scroll the code. there are more than shown first.

                  alt text

                  GREYONG Offline
                  GREYONG Offline
                  GREYON
                  wrote on last edited by
                  #17

                  @mrjj
                  I can just see the function definition,or maybe it's in the.h file?

                  mrjjM 1 Reply Last reply
                  0
                  • GREYONG GREYON

                    @mrjj
                    I can just see the function definition,or maybe it's in the.h file?

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #18

                    @GREYON

                    The function is there. there is no .h file.
                    but full function is.

                    GREYONG 1 Reply Last reply
                    0
                    • mrjjM mrjj

                      @GREYON

                      The function is there. there is no .h file.
                      but full function is.

                      GREYONG Offline
                      GREYONG Offline
                      GREYON
                      wrote on last edited by
                      #19

                      okey I have seen it now!,let me try to use it

                      mrjjM 1 Reply Last reply
                      0
                      • GREYONG GREYON

                        okey I have seen it now!,let me try to use it

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #20

                        @GREYON
                        Ok good :)

                        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