Okay, so I really am totally new to all this, so bear with me .

I want to make a table that has two columns; one with the name of a specific property (ie distance) and the other with a double spin box in which the user can enter a value. Yesterday it was suggested to me on this forum that I use the SpinBox Delegate example to help me out. I did, but I didn't really understand any of it (did I mention that I'm totally new to this??), mainly because I didn't understand the concept of model/view programming and delegates in general. So in the end I just copy-pasted the code (with a few minor modifications) into mine, praying that it would work. As you might guess, it didn't. The code actually compiles, but nothing appears in the listView object (which I created in the ui file). Please help!

Here is the .cpp file in which the tableView object is modifed:

Qt Code:
  1. warmup::warmup(QWidget *parent, Qt::WFlags flags)
  2. : QMainWindow(parent, flags)
  3. {
  4. ui.setupUi(this);
  5. QStandardItemModel model(4,2);
  6. ui.tableView->setModel(&model);
  7.  
  8. SpinBoxTable table;
  9. ui.tableView->setItemDelegate(&table);
  10.  
  11. ui.tableView->horizontalHeader()->setStretchLastSection(true);
  12.  
  13. for (int row = 0; row < 4; ++row) {
  14. for (int column = 0; column < 2; ++column) {
  15. QModelIndex index = model.index(row, column, QModelIndex());
  16. model.setData(index, QVariant((row+1) * (column+1)));
  17. }
  18. }
  19. }
To copy to clipboard, switch view to plain text mode 

Here is the delegate header file:

Qt Code:
  1. #ifndef SPINBOXTABLE_H
  2. #define SPINBOXTABLE_H
  3.  
  4. #include <QItemDelegate>
  5. #include <QModelIndex>
  6. #include <QObject>
  7. #include <QSize>
  8. #include <QSpinBox>
  9.  
  10. class SpinBoxTable : public QItemDelegate
  11. {
  12. Q_OBJECT
  13.  
  14. public:
  15. SpinBoxTable(QObject *parent = 0);
  16.  
  17. QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
  18. const QModelIndex &index) const;
  19.  
  20. void setEditorData(QWidget *editor, const QModelIndex &index) const;
  21. void setModelData(QWidget *editor, QAbstractItemModel *model,
  22. const QModelIndex &index) const;
  23. void updateEditorGeometry(QWidget *editor,
  24. const QStyleOptionViewItem &option, const QModelIndex &index) const;
  25. };
  26. #endif
To copy to clipboard, switch view to plain text mode 

...and here is the delegate .cpp file:

Qt Code:
  1. #include "spinboxtable.h"
  2.  
  3. SpinBoxTable::SpinBoxTable(QObject *parent)
  4. :QItemDelegate(parent)
  5. {
  6. }
  7.  
  8. QWidget *SpinBoxTable::createEditor(QWidget *parent,
  9. const QStyleOptionViewItem &option,
  10. const QModelIndex &index) const
  11. {
  12. QDoubleSpinBox *editor = new QDoubleSpinBox(parent);
  13. editor->setMinimum(0.0);
  14. editor->setMaximum(10000.00);
  15.  
  16. return editor;
  17. }
  18.  
  19. void SpinBoxTable::setEditorData(QWidget *editor,
  20. const QModelIndex &index) const
  21. {
  22. int value = index.model()->data(index, Qt::EditRole).toInt();
  23.  
  24. QDoubleSpinBox *doubleSpinBox = static_cast<QDoubleSpinBox*>(editor);
  25. doubleSpinBox->setValue(value);
  26. }
  27.  
  28. void SpinBoxTable::setModelData(QWidget *editor, QAbstractItemModel *model,
  29. const QModelIndex &index) const
  30. {
  31. QDoubleSpinBox *doubleSpinBox = static_cast<QDoubleSpinBox*>(editor);
  32. doubleSpinBox->interpretText();
  33. int value = doubleSpinBox->value();
  34.  
  35. model->setData(index, value, Qt::EditRole);
  36. }
  37.  
  38. void SpinBoxTable::updateEditorGeometry(QWidget *editor,
  39. const QStyleOptionViewItem &option, const QModelIndex &index) const
  40. {
  41. editor->setGeometry(option.rect);
  42. }
To copy to clipboard, switch view to plain text mode 

Thanks!