Results 1 to 2 of 2

Thread: QTableView, QSqlQueryModel and delegates

  1. #1
    Join Date
    Jan 2008
    Location
    Brasil
    Posts
    131
    Thanks
    18
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question QTableView, QSqlQueryModel and delegates

    Good day friends!

    I'm trying to customize a QTableView through QItemDelegate. Trying debug noticed that my class MyDelegate is not being processed. I ask: Is it possible to delegate with QSqlQueryModel? What may be occurring's wrong? Follow my code:

    Qt Code:
    1. void CadComissoes::getUsuarios()
    2. {
    3. OpenDB();
    4. QSqlQuery qry;
    5. QString SQL;
    6.  
    7. model = new QSqlQueryModel();
    8.  
    9. SQL = "SELECT COD_USER, NAME, COD_GROUP ";
    10. SQL+= "FROM USERS";
    11.  
    12. qry.prepare(SQL);
    13. qry.exec();
    14.  
    15. model->setQuery(qry);
    16. ui.tableView_ComissaoUsuarios->setModel(model);
    17.  
    18. MyDelegate *iDelegate = new MyDelegate(this);
    19. ui.tableView_ComissaoUsuarios->setItemDelegate(iDelegate);
    20. CloseDB();
    21. }
    22.  
    23. MyDelegate::MyDelegate(QObject *parent)
    24. :QItemDelegate(parent)
    25. {
    26.  
    27. }
    28.  
    29. QWidget *MyDelegate::createEditor(QWidget *parent,
    30. const QStyleOptionViewItem &option,
    31. const QModelIndex &index) const
    32. {
    33. if (!index.isValid())
    34. {
    35. qDebug() << "Not Valid index";
    36. }
    37. if (index.column() == 2)
    38. {
    39. QSpinBox *editor = new QSpinBox(parent);
    40. editor->setMinimum(0);
    41. editor->setMaximum(100);
    42. return editor;
    43. }
    44. }
    45.  
    46. void MyDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
    47. {
    48. int value = index.model()->data(index, Qt::DisplayRole).toInt();
    49.  
    50. QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
    51. spinBox->setValue(value);
    52. }
    53.  
    54. void MyDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
    55. const QModelIndex &index) const
    56. {
    57. QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
    58. spinBox->interpretText();
    59. int value = spinBox->value();
    60.  
    61. model->setData(index, value, Qt::EditRole);
    62. }
    To copy to clipboard, switch view to plain text mode 

    Thanks,

    Marcelo E. Geyer

  2. #2
    Join Date
    Jan 2008
    Location
    Brasil
    Posts
    131
    Thanks
    18
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView, QSqlQueryModel and delegates

    I found the problem, I gave to that QSqlQueryModel is only for reading. So QSqlTableModel used. Thanks.

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.