/* simple prepare QTableView - > model to print return QTextDocument */
{
/* remove last line if needet to remove Empty line to scroll more down on QTableView */
const int righe = qBound(0,model->rowCount() - remove_last_line ,model->rowCount());
const int colonne = model->columnCount();
/* normal paragraph format */
paraformat.setBottomMargin(0);
paraformat.setTopMargin(0);
/* Empty paragraph format */
paraspace.setBottomMargin(8);
paraspace.setTopMargin(8);
int CursorPointer = 0;
///////////bool IsCorrectCursor = Tcursor.movePosition(QTextCursor::End);
CursorPointer = Tcursor.position();
QTextTable *qtable
= Tcursor.
insertTable(righe
+1,colonne
);
/////// righe tableFormat.
setBackground ( QColor(Qt
::white) );
tableFormat.setBorder(0);
tableFormat.setCellSpacing(2);
tableFormat.setCellPadding(2);
tableFormat.setAlignment ( Qt::AlignLeft );
/* START HEADER ONLY ONE TD LINE */
for (int e = 0; e < colonne; ++e) {
const QString testosopra
= model
->headerData
(e,Qt
::Horizontal,Qt
::DisplayRole).
toString();
/* set cursor position on first cell! */
Tcursor = cellheader.firstCursorPosition();
QFont fx1
= model
->headerData
(e,Qt
::Horizontal,Qt
::FontRole).
value<QFont>
();
QColor TextColor1
= model
->headerData
(e,Qt
::Horizontal,Qt
::TextColorRole).
value<QColor>
();
////////QColor BackgroundColor1 = model->headerData(e,Qt::Horizontal,Qt::BackgroundRole).value<QColor>();
td_format.
setBackground(QColor("#dfdfdf"));
/* set BG color on header only palette */ cellheader.setFormat(td_format);
InlineStyle1.
setForeground(QBrush(TextColor1
));
InlineStyle1.setFont(fx1);
Tcursor.insertText(testosopra,InlineStyle1);
/////////////////qDebug() << "testa ->" << testosopra;
}
/* START LINE ROW LOOPS */
for (int i = 0; i < righe; ++i) {
for (int o = 0; o < colonne; ++o) {
if (index.isValid ()) {
const QString txt
= model
->data
(index,Qt
::DisplayRole).
toString();
//////////qDebug() << "corpo ->" << txt;
QFont fx
= model
->data
(index,Qt
::FontRole).
value<QFont>
();
QColor TextColor
= model
->data
(index,Qt
::TextColorRole).
value<QColor>
();
BackgroundColor = model->data(index,Qt::BackgroundRole).value<QColor>();
/* alignment txt */
paraformat.setAlignment((Qt::Alignment)model->data(index,Qt::TextAlignmentRole).toInt());
if ( !BackgroundColor.isValid ()) {
if (i%2) {
BackgroundColor
= QColor("#e1e1e1");
} else {
BackgroundColor
= QColor("#ffffff");
}
}
existformat.setBackground(BackgroundColor);
cellstart.setFormat(existformat);
InlineStyle.
setForeground(QBrush(TextColor
));
InlineStyle.setFont(fx);
Tcursor = cellstart.firstCursorPosition();
if (txt.trimmed().size() < 1) {
Tcursor.setBlockFormat(paraspace);
} else {
Tcursor.setBlockFormat(paraformat);
}
Tcursor.insertText(txt,InlineStyle);
}
}
}
qtable->setFormat( tableFormat );
/////////QString xdhtml = doc->toHtml("utf-8");
//////qDebug() << "xdhtml0000000000 ->" << xdhtml;
return doc;
}
/* simple prepare QTableView - > model to print return QTextDocument */
static inline QTextDocument *TabletoDom( const QStandardItemModel * model , int remove_last_line = 0 )
{
/* remove last line if needet to remove Empty line to scroll more down on QTableView */
const int righe = qBound(0,model->rowCount() - remove_last_line ,model->rowCount());
const int colonne = model->columnCount();
/* normal paragraph format */
QTextBlockFormat paraformat;
paraformat.setBottomMargin(0);
paraformat.setTopMargin(0);
/* Empty paragraph format */
QTextBlockFormat paraspace;
paraspace.setBottomMargin(8);
paraspace.setTopMargin(8);
QColor BackgroundColor;
int CursorPointer = 0;
QTextDocument *doc = new QTextDocument();
QTextCursor Tcursor = QTextCursor(doc);
///////////bool IsCorrectCursor = Tcursor.movePosition(QTextCursor::End);
CursorPointer = Tcursor.position();
QTextTable *qtable = Tcursor.insertTable(righe+1,colonne); /////// righe
QTextTableFormat tableFormat;
tableFormat.setWidth(QTextLength(QTextLength::PercentageLength,98));
tableFormat.setBackground ( QColor(Qt::white) );
tableFormat.setBorder(0);
tableFormat.setCellSpacing(2);
tableFormat.setCellPadding(2);
tableFormat.setAlignment ( Qt::AlignLeft );
/* START HEADER ONLY ONE TD LINE */
for (int e = 0; e < colonne; ++e) {
const QString testosopra = model->headerData(e,Qt::Horizontal,Qt::DisplayRole).toString();
QTextTableCell cellheader = qtable->cellAt(0,e);
/* set cursor position on first cell! */
Tcursor = cellheader.firstCursorPosition();
QFont fx1 = model->headerData(e,Qt::Horizontal,Qt::FontRole).value<QFont>();
QColor TextColor1 = model->headerData(e,Qt::Horizontal,Qt::TextColorRole).value<QColor>();
////////QColor BackgroundColor1 = model->headerData(e,Qt::Horizontal,Qt::BackgroundRole).value<QColor>();
QTextCharFormat td_format = cellheader.format();
td_format.setBackground(QColor("#dfdfdf")); /* set BG color on header only palette */
cellheader.setFormat(td_format);
QTextCharFormat InlineStyle1;
InlineStyle1.setForeground(QBrush(TextColor1));
InlineStyle1.setFont(fx1);
Tcursor.insertText(testosopra,InlineStyle1);
/////////////////qDebug() << "testa ->" << testosopra;
}
/* START LINE ROW LOOPS */
for (int i = 0; i < righe; ++i) {
for (int o = 0; o < colonne; ++o) {
QTextTableCell cellstart = qtable->cellAt(i+1,o);
QModelIndex index = model->index(i,o);
if (index.isValid ()) {
const QString txt = model->data(index,Qt::DisplayRole).toString();
//////////qDebug() << "corpo ->" << txt;
QFont fx = model->data(index,Qt::FontRole).value<QFont>();
QColor TextColor = model->data(index,Qt::TextColorRole).value<QColor>();
BackgroundColor = model->data(index,Qt::BackgroundRole).value<QColor>();
/* alignment txt */
paraformat.setAlignment((Qt::Alignment)model->data(index,Qt::TextAlignmentRole).toInt());
if ( !BackgroundColor.isValid ()) {
if (i%2) {
BackgroundColor = QColor("#e1e1e1");
} else {
BackgroundColor = QColor("#ffffff");
}
}
QTextCharFormat existformat = cellstart.format();
existformat.setBackground(BackgroundColor);
cellstart.setFormat(existformat);
QTextCharFormat InlineStyle;
InlineStyle.setForeground(QBrush(TextColor));
InlineStyle.setFont(fx);
Tcursor = cellstart.firstCursorPosition();
if (txt.trimmed().size() < 1) {
Tcursor.setBlockFormat(paraspace);
} else {
Tcursor.setBlockFormat(paraformat);
}
Tcursor.insertText(txt,InlineStyle);
}
}
}
qtable->setFormat( tableFormat );
/////////QString xdhtml = doc->toHtml("utf-8");
//////qDebug() << "xdhtml0000000000 ->" << xdhtml;
return doc;
}
To copy to clipboard, switch view to plain text mode
Bookmarks