Utilisation de QTextCursor pour la mise en page
:salut: à tous,
Je souhaite créer un rapport PDF depuis mon application. Pour cela j'utilise un QTExtDocument et un QTextCursor implémentés de la manière suivante :
Code:
1 2
| QTextDocument doc;
QTextCursor cursor(&doc); |
Et j'ai créé quelques blocs me permettant d'ajouter un titre, un sous titre, une partie en HTML, ... comme ceci :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| void ExportFile::addTitle(QTextCursor& cursor)
{
QTextBlockFormat format;
format.setAlignment(Qt::AlignCenter);
format.setBackground(QColor("lightGray"));
//qDebug() << format.foreground();
//format.setForeground(QBrush(QColor(255, 0, 0)));
QTextCharFormat textFormat;
textFormat.setFont(QFont("Deja Vu", 14));
cursor.setBlockFormat(format);
cursor.setCharFormat(textFormat);
cursor.insertText(m_title);
//cursor.movePosition( QTextCursor::Down );
}
void ExportFile::addSubTitle(QTextCursor& cursor, QString subTitle)
{
QTextBlockFormat format;
format.setAlignment(Qt::AlignLeft);
format.setForeground(QBrush(QColor(255, 0, 0)));
QTextCharFormat textFormat;
textFormat.setFont(QFont("Deja Vu", 12));
cursor.setBlockFormat(format);
cursor.setCharFormat(textFormat);
cursor.insertText(subTitle);
cursor.insertHtml("<br>");
//cursor.movePosition( QTextCursor::End );
}
void ExportFile::addHtml(QTextCursor& cursor, QString html)
{
QTextBlockFormat format;
format.setAlignment(Qt::AlignCenter);
QTextCharFormat textFormat;
textFormat.setFont(QFont("Deja Vu", 9));
cursor.setBlockFormat(format);
cursor.setCharFormat(textFormat);
cursor.insertHtml(html);
//cursor.movePosition( QTextCursor::End );
} |
Que j'utilise comme suit :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
| QTextDocument doc;
QTextCursor cursor(&doc);
// ....
// ....
m_title = "Fiche Ident STEP";
addTitle(cursor);
addSubTitle(cursor, "SubTitle 1");
addHtml(cursor, body);
// ....
// .... |
Mon souci vient du fait que le QTextBlockFormat qui devrait s'appliquer à chaque "bloc" ne l'est pas et c'est le dernier appelé qui est pris en considération pour tous les "blocs".
Je n'ai pas ce phénomène avec le QTextCharFormat qui lui est bien pris en compte lors de la création de chaque bloc.
Ai-je loupé quelque chose ?
D'avance merci pour votre aide.
++
J