Results 1 to 4 of 4

Thread: How can I modify the SpinBox Delegate Example to work in my code?

  1. #1
    Join Date
    Jan 2010
    Posts
    19
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default How can I modify the SpinBox Delegate Example to work in my code?

    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!

  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: How can I modify the SpinBox Delegate Example to work in my code?

    You're certainly on the right track.

    Line 8 of the first listing declares a variable that goes out of scope at the end of the warmup() method. You almost certainly want the delegate lifetime to be longer than that, i.e. long enough to get back to the event loop. Allocate it on the heap with a suitable QObject parent so that Qt cleans up for you later.

    Your delegate should derive from QStyledItemDelegate to get the best match to the host environment.

    See if that gets you any further.

  3. #3
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How can I modify the SpinBox Delegate Example to work in my code?

    According to the documentation and my testing, you should get a double spin box if you provide double data in the model. No delegate required.
    Qt Code:
    1. model.setData(index, 2.2);
    To copy to clipboard, switch view to plain text mode 

  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: How can I modify the SpinBox Delegate Example to work in my code?

    Without a delegate, passing a QVariant::Double gives you a default, unlimited double spin box. The OP seems to want to limit the input to between 0.0 and 10000.0 (lines 13, 14 of the delegate) which, AFAICT, is only achievable with a delegate.

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

    numbat (22nd January 2010)

Similar Threads

  1. delegate isn't work with QSqlTableModel?
    By TomASS in forum Newbie
    Replies: 2
    Last Post: 3rd August 2009, 12:23
  2. Why this code doesn't work?
    By prykHetQuo in forum Qt Programming
    Replies: 2
    Last Post: 11th February 2009, 19:08
  3. Autosave in a spinbox delegate
    By SailinShoes in forum Qt Programming
    Replies: 2
    Last Post: 19th August 2008, 07:20
  4. Modify a ContextMenu
    By smarinr in forum Qt Programming
    Replies: 5
    Last Post: 10th May 2008, 17:41

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.