PDA

View Full Version : Extract QStandardItemModel from QTableView



patrik08
15th January 2008, 19:29
I make 20 QTableView table from QStandardItemModel
only by table -> setModel ( QAbstractItemModel * model )

At end to print table i wand back the model

QAbstractItemModel * QAbstractItemView::model () const

now how i can convert QAbstractItemModel to QStandardItemModel without subclass the model?

Text and color i set on QStandardItemModel , i want only back my data how?

I can extract only from QStandardItemModel :-( and print on this way.....




/* 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::Perc entageLength,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).toStr ing();
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).val ue<QColor>();
////////QColor BackgroundColor1 = model->headerData(e,Qt::Horizontal,Qt::BackgroundRole).va lue<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;
}

wysota
16th January 2008, 08:34
now how i can convert QAbstractItemModel to QStandardItemModel without subclass the model?

If I understood your question properly then the answer is "qobject_cast" or "dynamic_cast". And please stop posting code that is irrelevant to the question.