이 페이지에서

문서 레이아웃

문서의 레이아웃은 문서가 장치에 표시될 때, 또는 문서의 시각적 표현이 필요한 정보가 요청될 때에만 관련이 있습니다. 이러한 상황이 발생하기 전까지는 문서를 장치에 맞게 서식을 지정하거나 준비할 필요가 없습니다.

개요

각 문서의 레이아웃은 ` QAbstractTextDocumentLayout ` 클래스의 서브클래스에 의해 관리됩니다. 이 클래스는 레이아웃 및 렌더링 엔진을 위한 공통 인터페이스를 제공합니다. 기본 렌더링 동작은 현재 비공개 클래스에 구현되어 있습니다. 이러한 접근 방식을 통해 사용자 정의 레이아웃을 생성할 수 있으며, 인쇄를 위해 페이지를 준비하거나 PDF(Portable Document Format) 파일로 내보낼 때 사용되는 메커니즘을 제공합니다.

예제 - 형상 텍스트 레이아웃

예를 들어 사용자 정의 위젯을 렌더링할 때와 같이, 불규칙한 모양의 영역 내에서 일반 텍스트를 서식 지정할 수 있는 기능이 중요한 경우가 있습니다. Scribe는 개발자가 먼저 문서를 생성할 필요 없이 단어 줄바꿈 및 레이아웃 작업을 수행할 수 있도록, ` QTextLayout ` 클래스가 제공하는 것과 같은 범용 기능을 제공합니다.

원 주위를 따라 흐르는 텍스트의 스크린샷.

일반 텍스트 단락의 서식 지정 및 그리기는 간단합니다. 아래 예제는 단일 글꼴을 사용하여 원의 오른쪽 가장자리를 따라 텍스트 단락을 배치합니다.

QTextLayout textLayout(text, font);
qreal margin = 10;
qreal radius = qMin(width()/2.0, height()/2.0) - margin;
QFontMetrics fm(font);

qreal lineHeight = fm.height();
qreal y = 0;

textLayout.beginLayout();

while (1) {
    // create a new line
    QTextLine line = textLayout.createLine();
    if (!line.isValid())
        break;

    qreal x1 = qMax(0.0, pow(pow(radius,2)-pow(radius-y,2), 0.5));
    qreal x2 = qMax(0.0, pow(pow(radius,2)-pow(radius-(y+lineHeight),2), 0.5));
    qreal x = qMax(x1, x2) + margin;
    qreal lineWidth = (width() - margin) - x;

    line.setLineWidth(lineWidth);
    line.setPosition(QPointF(x, margin+y));
    y += line.height();
}

textLayout.endLayout();

QPainter painter;
painter.begin(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.fillRect(rect(), Qt::white);
painter.setBrush(QBrush(Qt::black));
painter.setPen(QPen(Qt::black));
textLayout.draw(&painter, QPoint(0,0));

painter.setBrush(QBrush(QColor("#a6ce39")));
painter.setPen(QPen(Qt::black));
painter.drawEllipse(QRectF(-radius, margin, 2*radius, 2*radius));
painter.end();

표시할 텍스트 문자열과 사용할 글꼴을 지정하여 텍스트 레이아웃을 생성합니다. 텍스트 형식으로 텍스트 줄을 가져오고, 남은 텍스트를 사용 가능한 공간에 맞춰 줄바꿈함으로써 제공된 텍스트가 올바르게 서식 처리되도록 합니다. 페이지를 아래로 내려가면서 줄들이 배치됩니다.

서식이 지정된 텍스트는 페인트 장치에 그려질 수 있습니다. 위 코드에서는 텍스트가 위젯에 직접 그려집니다.

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.