한국어
Qt
 
Qt Quick 1을 사용했다면 QML 기반 슬라이드에서 PDF를 작성하는 작업이 쉬웠다. QtQuick1은 QPainter를 통한 래스터 기반의 페인팅을 사용한다. QPainter를 사용하면 QPrinter로 렌더링을 리디렉션하는 것이 간단하다. QPrinter는 출력 파일 형식으로 PDF 파일을 사용할 수 있다.
 
QtQuick 2는 렌더링을 위해 OpenGL Scene Graph를 사용하므로 QPainter를 이용하여 현재 장면을 PDF로 직접 렌더링이 불가능하다.
 
단순한 표에 어떤 값들을 나열하여 출력하는 것이라면 아래와 같은 방법이 대안이 될수 있다. 어떤 데이터들을 html 코드로 표를 작성하고 PDF문서를 만드는 예제이다.
 
예제에서 필요한 모듈
*.pro
QT += sql gui printsupport
 
main.cpp
#include <QGuiApplication>
#include <QtSql>
#include <QPrinter>
#include <QTextDocument>

bool createConnection() {
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName(":memory:");
    if (!db.open()) {
        qDebug() << "Cannot open database";
        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;
}

void printTable(QPrinter* printer, QSqlQuery& Query) {
    QString strStream;
    QTextStream out(&strStream);

    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(const QString &name) {
    QPrinter printer(QPrinter::HighResolution);
    printer.setOrientation(QPrinter::Portrait);
    printer.setPageSize(QPrinter::A4);
    printer.setOutputFormat(QPrinter::PdfFormat);
    printer.setOutputFileName(name);

    // DB Query
    QSqlQuery query;
    query.exec("SELECT * from person");

    // write
    printTable(&printer, query);
}

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    // Create DB table.
    createConnection();

    // Create PDF document file.
    print("file.pdf");

    return 1;
}

 

PDF문서는 A4사이즈로 아래 그림과 같이 프린트된다.

output.png

번호 제목 글쓴이 날짜 조회 수
공지 Qt프로그래밍(QtQuick) Beginner를 위한 글 읽는 순서 운영자 2019.01.05 118370
60 가상키보드(Qt Virtual Keyboard)를 사용하는 방법 [32] file makersweb 2019.05.03 237487
59 Windows에서 Qt D-Bus를 사용하여 프로세스간 통신(IPC) file makersweb 2019.05.02 8304
58 QML과 QtQuick 모듈 개념과 기본 타입들 makersweb 2019.04.26 17273
57 QML 전역 객체 (Global Object) file makersweb 2019.04.10 4883
56 tslib의 ts_calibrate를 응용해서 Qt로 터치보정기능 구현 file makersweb 2019.04.06 4686
55 GPU가 없는 장치에서 Qt Quick을 사용 makersweb 2019.04.02 6267
54 QTextCodec클래스를 사용하여 유니코드와 EUC-KR 변환 makersweb 2019.03.25 8380
53 qInstallMessageHandler를 이용한 디버그 메세지 출력 제어하기 makersweb 2019.02.25 5061
52 Qt5기반 독립 프로세스(out-of-process)로 동작하는 가상키보드(virtual keyboard) file makersweb 2019.02.24 5967
51 Qml 기본 컴포넌트 강좌 (4) - 모델 리스팅(Listing) file 운영자 2019.02.23 8453
50 Qt Bluetooth를 이용한 시리얼(Serial) 통신 file makersweb 2019.02.17 7995
49 Qml 기본 컴포넌트 강좌 (3) - 배치(positioning) 컴포넌트 file 운영자 2019.02.10 10274
48 QString 문자열 다루기 예제 운영자 2019.01.26 48931
47 Qt SQL을 이용한 가벼운 데이터베이스 다루기 [1] file 운영자 2019.01.23 11071
46 구글 클라우드 Speech-To-Text API를 Qt기반(C++, Qml)테스트 [7] file makersweb 2019.01.20 7564
45 QNetworkAccessManager를 통해 HTTP POST 하는 예제 makersweb 2019.01.17 7929
44 Qt응용프로그램 실행 시 콘솔창(터미널)같이 띄우기 file makersweb 2019.01.16 8739
43 안드로이드 가상장치 사용 file makersweb 2019.01.13 4729
42 Qml 기본 컴포넌트 강좌 (2) [2] file makersweb 2019.01.05 12603
41 Qml 기본 컴포넌트 강좌 (1) file makersweb 2019.01.03 16371