Results 1 to 2 of 2

Thread: Extract QStandardItemModel from QTableView

  1. #1
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Extract QStandardItemModel from QTableView

    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.....

    Qt Code:
    1. /* simple prepare QTableView - > model to print return QTextDocument */
    2. static inline QTextDocument *TabletoDom( const QStandardItemModel * model , int remove_last_line = 0 )
    3. {
    4. /* remove last line if needet to remove Empty line to scroll more down on QTableView */
    5. const int righe = qBound(0,model->rowCount() - remove_last_line ,model->rowCount());
    6. const int colonne = model->columnCount();
    7. /* normal paragraph format */
    8. QTextBlockFormat paraformat;
    9. paraformat.setBottomMargin(0);
    10. paraformat.setTopMargin(0);
    11. /* Empty paragraph format */
    12. QTextBlockFormat paraspace;
    13. paraspace.setBottomMargin(8);
    14. paraspace.setTopMargin(8);
    15. QColor BackgroundColor;
    16. int CursorPointer = 0;
    17. QTextCursor Tcursor = QTextCursor(doc);
    18. ///////////bool IsCorrectCursor = Tcursor.movePosition(QTextCursor::End);
    19. CursorPointer = Tcursor.position();
    20.  
    21. QTextTable *qtable = Tcursor.insertTable(righe+1,colonne); /////// righe
    22. QTextTableFormat tableFormat;
    23. tableFormat.setWidth(QTextLength(QTextLength::PercentageLength,98));
    24. tableFormat.setBackground ( QColor(Qt::white) );
    25. tableFormat.setBorder(0);
    26. tableFormat.setCellSpacing(2);
    27. tableFormat.setCellPadding(2);
    28. tableFormat.setAlignment ( Qt::AlignLeft );
    29. /* START HEADER ONLY ONE TD LINE */
    30. for (int e = 0; e < colonne; ++e) {
    31. const QString testosopra = model->headerData(e,Qt::Horizontal,Qt::DisplayRole).toString();
    32. QTextTableCell cellheader = qtable->cellAt(0,e);
    33. /* set cursor position on first cell! */
    34. Tcursor = cellheader.firstCursorPosition();
    35. QFont fx1 = model->headerData(e,Qt::Horizontal,Qt::FontRole).value<QFont>();
    36. QColor TextColor1 = model->headerData(e,Qt::Horizontal,Qt::TextColorRole).value<QColor>();
    37. ////////QColor BackgroundColor1 = model->headerData(e,Qt::Horizontal,Qt::BackgroundRole).value<QColor>();
    38. QTextCharFormat td_format = cellheader.format();
    39. td_format.setBackground(QColor("#dfdfdf")); /* set BG color on header only palette */
    40. cellheader.setFormat(td_format);
    41.  
    42. QTextCharFormat InlineStyle1;
    43. InlineStyle1.setForeground(QBrush(TextColor1));
    44. InlineStyle1.setFont(fx1);
    45. Tcursor.insertText(testosopra,InlineStyle1);
    46. /////////////////qDebug() << "testa ->" << testosopra;
    47. }
    48. /* START LINE ROW LOOPS */
    49. for (int i = 0; i < righe; ++i) {
    50.  
    51. for (int o = 0; o < colonne; ++o) {
    52. QTextTableCell cellstart = qtable->cellAt(i+1,o);
    53. QModelIndex index = model->index(i,o);
    54.  
    55. if (index.isValid ()) {
    56. const QString txt = model->data(index,Qt::DisplayRole).toString();
    57. //////////qDebug() << "corpo ->" << txt;
    58. QFont fx = model->data(index,Qt::FontRole).value<QFont>();
    59. QColor TextColor = model->data(index,Qt::TextColorRole).value<QColor>();
    60. BackgroundColor = model->data(index,Qt::BackgroundRole).value<QColor>();
    61. /* alignment txt */
    62. paraformat.setAlignment((Qt::Alignment)model->data(index,Qt::TextAlignmentRole).toInt());
    63. if ( !BackgroundColor.isValid ()) {
    64. if (i%2) {
    65. BackgroundColor = QColor("#e1e1e1");
    66. } else {
    67. BackgroundColor = QColor("#ffffff");
    68. }
    69. }
    70.  
    71. QTextCharFormat existformat = cellstart.format();
    72. existformat.setBackground(BackgroundColor);
    73. cellstart.setFormat(existformat);
    74.  
    75. QTextCharFormat InlineStyle;
    76. InlineStyle.setForeground(QBrush(TextColor));
    77. InlineStyle.setFont(fx);
    78.  
    79. Tcursor = cellstart.firstCursorPosition();
    80. if (txt.trimmed().size() < 1) {
    81. Tcursor.setBlockFormat(paraspace);
    82. } else {
    83. Tcursor.setBlockFormat(paraformat);
    84. }
    85.  
    86. Tcursor.insertText(txt,InlineStyle);
    87. }
    88. }
    89.  
    90. }
    91.  
    92.  
    93. qtable->setFormat( tableFormat );
    94. /////////QString xdhtml = doc->toHtml("utf-8");
    95. //////qDebug() << "xdhtml0000000000 ->" << xdhtml;
    96. return doc;
    97. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jacek; 15th January 2008 at 22:04. Reason: shortened too long line

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Extract QStandardItemModel from QTableView

    Quote Originally Posted by patrik08 View Post
    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.