Results 1 to 6 of 6

Thread: Set an editable column for QTableView

  1. #1
    Join Date
    Feb 2011
    Posts
    64
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Set an editable column for QTableView

    I wont to set an editable column in QTableView, so far I tried to subclass QTableView and reimplementing Qt::ItemFlags flags(const QModelIndex & index) const, subclassing QSqlQueryModel and reimplementing Qt::ItemFlags flags(const QModelIndex & index) const, finally, subclassing QAbstractItemView, but this a compiler error showed up.
    Subclassing QTableView
    Qt Code:
    1. #include "exqtableview.h"
    2.  
    3. ExQTableView::ExQTableView(QWidget *parent) :
    4. QTableView(parent)
    5. {
    6. }
    7.  
    8. Qt::ItemFlags ExQTableView::flags(const QModelIndex & index) const
    9. {
    10. if (!index.isValid())
    11. return Qt::ItemIsEnabled;
    12. if (index.column() == 2)
    13. return index.parent().flags() & ~Qt::ItemIsEditable;
    14. }
    To copy to clipboard, switch view to plain text mode 
    Subclassing QSqlQueryModel
    Qt Code:
    1. #include "exsqlquerymodel.h"
    2.  
    3. ExSqlQueryModel::ExSqlQueryModel(QObject *parent) :
    4. {
    5. }
    6.  
    7. Qt::ItemFlags ExSqlQueryModel::flags( const QModelIndex &index) const
    8. {
    9. Qt::ItemFlags flags = QSqlQueryModel::flags(index);
    10. if (index.column() == 2 )
    11. flags &= ~Qt::ItemIsEditable;
    12. return flags;
    13. }
    To copy to clipboard, switch view to plain text mode 
    Subclassing QAbstractItemView
    Qt Code:
    1. #include "exitemdelegate.h"
    2. #include <QTableWidgetItem>
    3.  
    4. ExItemDelegate::ExItemDelegate(QObject *parent) :
    5. QItemDelegate(parent)
    6. {
    7. }
    8.  
    9. QWidget *ExItemDelegate::createEditor(QWidget *parent,
    10. const QModelIndex & index ) const
    11. {
    12. return item;
    13. }
    14.  
    15. void ExItemDelegate::setEditorData(QWidget *editor,
    16. const QModelIndex &index) const
    17. {
    18. float value = index.model()->data(index, Qt::EditRole).toFloat();
    19.  
    20. QTableWidgetItem *cell = static_cast<QTableWidgetItem*>(editor);
    21. cell->setData(0, value);
    22. }
    23.  
    24. void ExItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
    25. const QModelIndex &index) const
    26. {
    27. QTableWidgetItem *cell = static_cast<QTableWidgetItem*>(editor);
    28. float value = cell->data(Qt::DisplayRole);
    29. model->setData(index, value, Qt::EditRole);
    30. }
    31.  
    32. void ExItemDelegate::updateEditorGeometry(QWidget *editor,
    33. const QStyleOptionViewItem &option, const QModelIndex &) const
    34. {
    35. editor->setGeometry(option.rect);
    36. }
    To copy to clipboard, switch view to plain text mode 
    So, what wrong I am doing here?

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Set an editable column for QTableView

    Neither QAbstractItemView nor the derived QTableView has as flags() function to override. This leaves your other option.
    I wont to set an editable column in QTableView
    Then you should add the Qt::ItemIsEditable flag. Your code currently removes the flag by using a bitwise AND with the negation of the editable flag value. You want to use a bitwise OR to add the flag.

  3. #3
    Join Date
    Feb 2011
    Posts
    64
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Set an editable column for QTableView

    I subclassed from QSqlQueryModel and reimplmented flags(), now my QTableView is editable.
    Qt Code:
    1. Qt::ItemFlags EditableSqlModel::flags(
    2. const QModelIndex &index) const
    3. {
    4. Qt::ItemFlags flags = QSqlQueryModel::flags(index);
    5. if (index.column() == 1 || index.column() == 2)
    6. flags |= Qt::ItemIsEditable;
    7. return flags;
    8. }
    To copy to clipboard, switch view to plain text mode 
    However, when I changed some data in cell, and after hint enter, the value back to the first one before I edit, is there some way to keep changed data in view, and how to reimplemnt cellChanged(int, int) in QTableView?

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Set an editable column for QTableView

    QSqlQueryModel is read-only by design. Its inherited setData() does nothing and returns false. You could implement setData() to put the data into the database, but it won't show in the view until you rerun the query. setData() could also emit a cellChanged() signal if you so desired.

  5. #5
    Join Date
    Feb 2011
    Posts
    64
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Set an editable column for QTableView

    I am trying to avoid setData() because I have many things depend on widget. QTableView has no cellChanged() signal, how suppose I catch user modification in view?

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Set an editable column for QTableView

    You want to modify the data in the model, but you want to avoid setData()? Sorry, you've lost me. Using the model mechanisms is precisely the way you ensure that all views on the model stay in sync.

    By placing a delegate on the view you can intercept the attempt to write data back into the model, see QItemDelegate::setModelData().

Similar Threads

  1. QTableWidget, one column editable
    By stella1016 in forum Qt Programming
    Replies: 5
    Last Post: 11th July 2012, 09:35
  2. Make a column readonly/not editable in QTableView?
    By qlands in forum Qt Programming
    Replies: 3
    Last Post: 3rd August 2011, 10:44
  3. Replies: 3
    Last Post: 9th May 2011, 17:23
  4. Column Not Editable
    By waynew in forum Qt Programming
    Replies: 1
    Last Post: 1st August 2010, 01:43
  5. Editable QListView Column ??
    By darpan in forum Qt Programming
    Replies: 1
    Last Post: 3rd May 2006, 18:55

Tags for this Thread

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.