Results 1 to 6 of 6

Thread: setting header for specific rows within QTableView with custom model

  1. #1
    Join Date
    Mar 2011
    Posts
    45
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default setting header for specific rows within QTableView with custom model

    hello,

    i have implemented a custom model which was subclassed from QAbstractTableModel.
    now i would like to know how to set the headers for specific rows to certain values.

    i tried
    Qt Code:
    1. bool set = ui->tableViewSource->model()->setHeaderData(0, Qt::Vertical, QString("12"));
    To copy to clipboard, switch view to plain text mode 
    just right after the model and view were glued together (model contains data).
    but return value is false !?!

    my model implementation looks like:
    Qt Code:
    1. #include "tablemodel.h"
    2.  
    3. // //////////////////////////////////////////////
    4. TableModel::TableModel(QObject *parent)
    5. // //////////////////////////////////////////////
    6. {
    7. p2Data = NULL;
    8. }
    9.  
    10. // //////////////////////////////////////////////
    11. TableModel::TableModel(QStringList *p2Contents,
    12. QObject *parent) : QAbstractTableModel(parent)
    13. // //////////////////////////////////////////////
    14. {
    15. p2Data = p2Contents;
    16. }
    17.  
    18.  
    19. // //////////////////////////////////////////////
    20. int TableModel::rowCount(const QModelIndex &parent) const
    21. // //////////////////////////////////////////////
    22. {
    23. Q_UNUSED(parent);
    24. if( !p2Data )
    25. return 0;
    26. return p2Data->size();
    27. }
    28.  
    29. // //////////////////////////////////////////////
    30. int TableModel::columnCount(const QModelIndex &parent) const
    31. // //////////////////////////////////////////////
    32. {
    33. Q_UNUSED(parent);
    34. return 1;
    35. }
    36.  
    37. // //////////////////////////////////////////////
    38. QVariant TableModel::data(const QModelIndex &index, int role) const
    39. // //////////////////////////////////////////////
    40. {
    41. if( !p2Data )
    42. return QVariant();
    43.  
    44. if (!index.isValid())
    45. return QVariant();
    46.  
    47. if (index.row() >= (int)p2Data->size() || index.row() < 0)
    48. return QVariant();
    49.  
    50. if (role == Qt::DisplayRole)
    51. {
    52. if (index.column() == 0)
    53. return QVariant((*p2Data)[index.row()]);
    54. }
    55. return QVariant();
    56. }
    57.  
    58. // //////////////////////////////////////////////
    59. bool TableModel::insertRows(int position,
    60. int rows,
    61. const QModelIndex &index)
    62. // //////////////////////////////////////////////
    63. {
    64. if(!p2Data)
    65. return false;
    66.  
    67. Q_UNUSED(index);
    68.  
    69. beginInsertRows(QModelIndex(), position, position+rows-1);
    70.  
    71. for (int row=0; row < rows; row++)
    72. {
    73. p2Data->push_back("");
    74. }
    75.  
    76. endInsertRows();
    77. return true;
    78. }
    79.  
    80.  
    81. // //////////////////////////////////////////////
    82. bool TableModel::removeRows(int position,
    83. int rows,
    84. const QModelIndex &index)
    85. // //////////////////////////////////////////////
    86. {
    87. if(!p2Data)
    88. return false;
    89.  
    90. Q_UNUSED(index);
    91. beginRemoveRows(QModelIndex(), position, position+rows-1);
    92.  
    93. for (int row=0; row < rows; ++row)
    94. {
    95. p2Data->erase(p2Data->begin()+position);
    96. }
    97.  
    98. endRemoveRows();
    99. return true;
    100. }
    To copy to clipboard, switch view to plain text mode 

    so its some kind of (n, 1) table.

    anyone some clue why this is !?!?

  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: setting header for specific rows within QTableView with custom model

    You didn't implement setHeaderData() for your model.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2011
    Posts
    45
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: setting header for specific rows within QTableView with custom model

    hmmm ...
    using this:
    Qt Code:
    1. // //////////////////////////////////////////////
    2. bool TableModel::setHeaderData ( int section,
    3. Qt::Orientation orientation,
    4. const QVariant & value,
    5. int role )
    6. // //////////////////////////////////////////////
    7. {
    8. QAbstractTableModel::setHeaderData(section,orientation,value,role);
    9. emit headerDataChanged(Qt::Vertical,0,1);
    10. }
    To copy to clipboard, switch view to plain text mode 
    it still does not work.
    just for the case their is no misunderstanding:
    i intend to change the line number for first line to "12" (just an example)

    i hope wysota will be kind enough to show me the trees within this forrest because i cant see em

  4. #4
    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: setting header for specific rows within QTableView with custom model

    Quote Originally Posted by kerim View Post
    using this:
    it still does not work.
    Why would it work? Especially that you don't return any value from this method.


    i intend to change the line number for first line to "12" (just an example)
    Tell that to your model. You need to make headerData() return "12" for the appropriate input parameters. QAbstractTableModel will not do that by itself.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    kerim (12th April 2011)

  6. #5
    Join Date
    Jul 2010
    Location
    Indonesia
    Posts
    83
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: setting header for specific rows within QTableView with custom model

    In this case, you still have to put the "headers data" in a data structures then reimplement headerData and setHeaderData.
    ~ We are nothing in this universe ~

  7. The following user says thank you to viulskiez for this useful post:

    kerim (12th April 2011)

  8. #6
    Join Date
    Mar 2011
    Posts
    45
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: setting header for specific rows within QTableView with custom model

    thnx all 4 the hints ..
    of course the last posted code fragment missed a return statement,
    but actually implementing setHeaderData(..) is not necessary in my case
    because returning the appropriate return-value within headerData(..) is enough.

    thnx alot, i am learning alot within this forum.
    greets.

Similar Threads

  1. Replies: 5
    Last Post: 10th March 2011, 20:06
  2. Replies: 1
    Last Post: 8th September 2010, 21:38
  3. Replies: 4
    Last Post: 10th February 2010, 06:47
  4. Replies: 0
    Last Post: 1st February 2010, 11:00
  5. horizontal header over two rows in tablewidget
    By Nippler in forum Qt Programming
    Replies: 2
    Last Post: 28th April 2008, 10:33

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.