Results 1 to 8 of 8

Thread: QSqlTableModel::SetData on different column

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2014
    Posts
    8
    Qt products
    Qt5
    Platforms
    Unix/X11
    Thanks
    1

    Default QSqlTableModel::SetData on different column

    I have a QSqlTableModel that works well and all the slots are firing, but I need to update a different column when an check box is clicked.

    The code I have right now is below and I just can't get the model to update a different column in the index

    Qt Code:
    1. #include <QtSql>
    2. #include <QTextCharFormat>
    3. #include <QtDebug>
    4. #include <QCheckBox>
    5.  
    6. #include "includes/selectionsetmodel.h"
    7.  
    8.  
    9. selectionsetSqlModel::selectionsetSqlModel(QObject *parent) : QSqlTableModel(parent)
    10. {
    11. }
    12.  
    13. //! [0]
    14. QVariant selectionsetSqlModel::data(const QModelIndex &index, int role) const
    15. {
    16. QVariant value = QSqlQueryModel::data(index, role);
    17. int col = index.column();
    18.  
    19. switch(role){
    20. case Qt::DisplayRole:
    21. if (col == 6)
    22. {
    23. if (value.toInt()<1024)
    24. {
    25. return QString(QString::number(value.toInt()) + "B");
    26. }
    27. else
    28. {
    29. return QString(QString::number(value.toInt()/1024) + "KB");
    30. }
    31. }
    32. else
    33. {
    34. return QString(value.toString());
    35. }
    36. case Qt::FontRole:
    37.  
    38. // QFont boldFont;
    39. // boldFont.setBold(true);
    40. // return boldFont;
    41.  
    42. case Qt::BackgroundRole:
    43.  
    44. case Qt::TextAlignmentRole:
    45. if (col == 5 or col == 6)
    46. {
    47. return Qt::AlignRight + Qt::AlignVCenter;
    48. }
    49. if (col == 2)
    50. {
    51. return Qt::AlignVCenter;
    52. }
    53. case Qt::CheckStateRole:
    54. int selected = index.model()->data(index.model()->index(index.row(), 1), Qt::DisplayRole).toInt();
    55. if (col == 2 )
    56. {
    57. if(selected==1)
    58. {
    59. return Qt::Checked;
    60. }
    61. else
    62. {
    63. return Qt::Unchecked;
    64. }
    65. }
    66. }
    67. return QVariant();
    68. }
    69.  
    70. Qt::ItemFlags selectionsetSqlModel::flags ( const QModelIndex & index ) const
    71. {
    72. if (index.column() == 2)
    73. return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable ;
    74. else
    75. return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
    76. }
    77.  
    78. bool selectionsetSqlModel::setData(const QModelIndex& index,const QVariant& value,int role)
    79. {
    80.  
    81. bool v;
    82. if(role == Qt::CheckStateRole && index.column() == 2)
    83. {
    84. bool selected = index.model()->data(index.model()->index(index.row(), 1), Qt::DisplayRole).toBool();
    85. v = selected ? false : true;
    86. }
    87. //QModelIndex idx = QModelIndex(index.row(), 1, index);
    88. //bool r = this->setData(idx,v,role);
    89. QModelIndex top = createIndex(index.row(), 0);
    90. QModelIndex bottom = createIndex(index.row(), 3);
    91.  
    92. emit dataChanged(top, bottom); // emit layoutChanged() if headers changed
    93. //qDebug() << "Now Checkable Column: " << r << " = " << v;
    94. return true;
    95. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by silmaril; 30th January 2014 at 20:58.

Similar Threads

  1. QSqlTableModel remove column by name
    By qlands in forum Qt Programming
    Replies: 2
    Last Post: 5th July 2011, 12:48
  2. QSqlTableModel column customization
    By kishore7771 in forum Qt Programming
    Replies: 1
    Last Post: 16th October 2009, 09:07
  3. QSqlTableModel setData not working?
    By jon-ecm in forum Qt Programming
    Replies: 10
    Last Post: 7th May 2009, 06:30
  4. QSqlTableModel and possible bug with setData?
    By sylvainb in forum Qt Programming
    Replies: 2
    Last Post: 25th February 2009, 21:43
  5. Sum of a column in QSqlTableModel
    By giusepped in forum Qt Programming
    Replies: 4
    Last Post: 10th November 2008, 23:38

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
  •  
Qt is a trademark of The Qt Company.