ドキュメントのレイアウト
ドキュメントのレイアウトは、それがデバイス上に表示されるとき、またはドキュメントの視覚的な表現を必要とする何らかの情報が要求されたときにのみ関連します。このような事態が発生するまでは、ドキュメントをフォーマットしてデバイス用に準備する必要はありません。
概要
各ドキュメントのレイアウトは、QAbstractTextDocumentLayout クラスのサブクラスによって管理されます。このクラスはレイアウトエンジンとレンダリングエンジンに共通のインタフェースを提供する。デフォルトのレンダリング動作は現在、プライベートクラスで実装されています。このアプローチにより、カスタムレイアウトの作成が可能になり、印刷やPortable Document Format(PDF)ファイルへのエクスポートのためにページを準備するときに使用されるメカニズムが提供されます。
例 - シェイプテキストレイアウト
例えば、カスタムウィジェットをレンダリングする場合など、不規則な形状の領域内でプレーンテキストをフォーマットできることが重要な場合があります。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();
テキストレイアウトを作成し、表示したいテキスト文字列と使用するフォントを指定します。テキストフォーマットからテキスト行を取得し、使用可能なスペースを使って残りのテキストを折り返すことで、供給したテキストが正しくフォーマットされるようにします。罫線は、ページを移動するにつれて配置されます。
整形されたテキストはペイント・デバイスに描画することができます。上のコードでは、テキストはウィジェットに直接描画されています。
© 2025 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.