Results 1 to 9 of 9

Thread: Delegates and Table

  1. #1
    Join Date
    Mar 2006
    Location
    Vadodara, Gujarat, India
    Posts
    65
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Exclamation Delegates and Table

    Hi all,

    How can i hav different delegates in different columns of a table. for instance, i want spinbox delegate in 2nd column and combobox in 4 column.

    i took an example of spinbox delegate from examples directories and tried to do the changes:

    Qt Code:
    1. /*
    2.   main.cpp
    3.  
    4.   A simple example that shows how a view can use a custom delegate to edit
    5.   data obtained from a model.
    6. */
    7.  
    8. #include <QApplication>
    9. #include <QHeaderView>
    10. #include <QItemSelectionModel>
    11. #include <QStandardItemModel>
    12. #include <QTableView>
    13. #include <QObject>
    14.  
    15. #include "delegate.h"
    16.  
    17. int main(int argc, char *argv[])
    18. {
    19. QApplication app(argc, argv);
    20.  
    21. QStandardItemModel model(4, 2);
    22. QTableView tableView;
    23. tableView.setModel(&model);
    24.  
    25. SpinBoxDelegate delegate;
    26. ComboBoxDelegate comDel;
    27.  
    28.  
    29. for (int row = 0; row < 4; ++row)
    30. {
    31. for (int column = 0; column < 1; ++column)
    32. {
    33. tableView.setItemDelegate(&comDel);
    34. QModelIndex index = model.index(0, 0, QModelIndex());
    35. }
    36. for (int column = 1; column < 2; ++column)
    37. {
    38. tableView.setItemDelegate(&delegate);
    39. QModelIndex index = model.index(1, 1, QModelIndex());
    40. }
    41. }
    42.  
    43. tableView.setWindowTitle(QObject::tr("Spin Box Delegate"));
    44. tableView.show();
    45.  
    46. return app.exec();
    47. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. /*
    2.   delegate.cpp
    3.  
    4.   A delegate that allows the user to change integer values from the model
    5.   using a spin box widget.
    6. */
    7.  
    8. #include <QtGui>
    9.  
    10. #include "delegate.h"
    11.  
    12.  
    13. SpinBoxDelegate::SpinBoxDelegate(QObject *parent)
    14. : QItemDelegate(parent)
    15. {
    16. }
    17.  
    18. QWidget *SpinBoxDelegate::createEditor(QWidget *parent,
    19. const QStyleOptionViewItem &/* option */,
    20. const QModelIndex &/* index */) const
    21. {
    22. QSpinBox *editor = new QSpinBox(parent);
    23. editor->setMinimum(0);
    24. editor->setMaximum(100);
    25. editor->installEventFilter(const_cast<SpinBoxDelegate*>(this));
    26.  
    27. return editor;
    28. }
    29.  
    30. void SpinBoxDelegate::setEditorData(QWidget *editor,
    31. const QModelIndex &index) const
    32. {
    33. int value = index.model()->data(index, Qt::DisplayRole).toInt();
    34.  
    35. QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
    36. spinBox->setValue(value);
    37. }
    38.  
    39. void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
    40. const QModelIndex &index) const
    41. {
    42. QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
    43. spinBox->interpretText();
    44. int value = spinBox->value();
    45.  
    46. model->setData(index, value);
    47. }
    48.  
    49. void SpinBoxDelegate::updateEditorGeometry(QWidget *editor,
    50. const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
    51. {
    52. editor->setGeometry(option.rect);
    53. }
    54.  
    55. /***********************************************************************************************/
    56.  
    57.  
    58. ComboBoxDelegate::ComboBoxDelegate(QObject *parent)
    59. : QItemDelegate(parent)
    60. {
    61. }
    62.  
    63. QWidget *ComboBoxDelegate::createEditor(QWidget *parent,
    64. const QStyleOptionViewItem &/* option */,
    65. const QModelIndex &/* index */) const
    66. {
    67. QComboBox *editor = new QComboBox(parent);
    68. QStringList list ;
    69. list << "ankur" << "mitesh";
    70. editor->addItems(list);
    71. editor->installEventFilter(const_cast<ComboBoxDelegate*>(this));
    72.  
    73. return editor;
    74. }
    75.  
    76. void ComboBoxDelegate::setEditorData(QWidget *editor,
    77. const QModelIndex &index) const
    78. {
    79. QString value = index.model()->data(index, Qt::DisplayRole).toString();
    80.  
    81. QComboBox *comboBox = static_cast<QComboBox*>(editor);
    82. comboBox->addItem(value);
    83. }
    84.  
    85. void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
    86. const QModelIndex &index) const
    87. {
    88. QComboBox *comboBox = static_cast<QComboBox*>(editor);
    89. QString value = comboBox->currentText();
    90.  
    91. model->setData(index, value);
    92. }
    93.  
    94. void ComboBoxDelegate::updateEditorGeometry(QWidget *editor,
    95. const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
    96. {
    97. editor->setGeometry(option.rect);
    98. }
    99.  
    100. /***********************************************************************************************/
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #ifndef DELEGATE_H
    2. #define DELEGATE_H
    3.  
    4. #include <QItemDelegate>
    5. #include <QModelIndex>
    6. #include <QObject>
    7. #include <QSize>
    8. #include <QSpinBox>
    9.  
    10. class SpinBoxDelegate : public QItemDelegate
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. SpinBoxDelegate(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.  
    24. void updateEditorGeometry(QWidget *editor,
    25. const QStyleOptionViewItem &option, const QModelIndex &index) const;
    26. };
    27.  
    28. class ComboBoxDelegate : public QItemDelegate
    29. {
    30. Q_OBJECT
    31.  
    32. public:
    33. ComboBoxDelegate(QObject *parent = 0);
    34.  
    35. QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
    36. const QModelIndex &index) const;
    37.  
    38. void setEditorData(QWidget *editor, const QModelIndex &index) const;
    39. void setModelData(QWidget *editor, QAbstractItemModel *model,
    40. const QModelIndex &index) const;
    41.  
    42. void updateEditorGeometry(QWidget *editor,
    43. const QStyleOptionViewItem &option, const QModelIndex &index) const;
    44. };
    45.  
    46. #endif
    To copy to clipboard, switch view to plain text mode 

    is there somehwere i m wrong .....
    Do what u r afraid to do, and the death of fear is sure.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Delegates and Table

    You can only set one item delegate per view. That particular item delegate may create different type of editors for different model indexes, though.
    J-P Nurmi

  3. #3
    Join Date
    Mar 2006
    Location
    Vadodara, Gujarat, India
    Posts
    65
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Delegates and Table

    Quote Originally Posted by jpn
    You can only set one item delegate per view. That particular item delegate may create different type of editors for different model indexes, though.
    does different editors means the same thing that i need, i.e., combo in one column and spinbox in other ?

    how can i set editor for different inedxes ?

    can't the different delegate be used for different columns ?
    Do what u r afraid to do, and the death of fear is sure.

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Delegates and Table

    Quote Originally Posted by ankurjain
    does different editors means the same thing that i need, i.e., combo in one column and spinbox in other ? how can i set editor for different inedxes ?
    Create a combo or spin box according to the model index column. Something like:
    Qt Code:
    1. QWidget* SpinBoxDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
    2. {
    3. if (index.column() == something)
    4. {
    5. QSpinBox* editor = new QSpinBox(parent);
    6. ...
    7. return editor;
    8. }
    9. else
    10. {
    11. QComboBox* editor = new QComboBox(parent);
    12. ...
    13. return editor;
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 
    Just be aware that then you need to check which column is in question and cast to appropriate widget in setEditorData() and setModelData()...

    can't the different delegate be used for different columns ?
    Unfortunately nope. A single item delegate can be set for the whole view and it's model.
    J-P Nurmi

  5. The following 3 users say thank you to jpn for this useful post:

    ankurjain (25th April 2006), gfunk (18th May 2006), Jimmy2775 (30th May 2006)

  6. #5
    Join Date
    Mar 2006
    Location
    Vadodara, Gujarat, India
    Posts
    65
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Delegates and Table

    thanks JPN Brother ....

    hey can u tell me how can i make myself conceptually more clear for qt. i find the tuts ( the desrciption ) somewhat insufficient ( not totally targeted towards brginners ).

    ne resources ??
    Do what u r afraid to do, and the death of fear is sure.

  7. #6
    Join Date
    May 2006
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Delegates and Table

    Hi All,
    This is my first post, by the way, I salute everyone.

    I don't know QT very much, i'm just started few weeks ago.
    Is there any way to achieve that trick by using QTableWidget / QTableWidgetItem ?
    I read a tutorial about a QT3 QTable / QTableItem where it's seems to be achievable!

    Use of QTableWidget::addItem ( int row, int column, QTableWidgetItem * qtwItem ) could be useful?
    Last edited by KekraU; 17th May 2006 at 23:17.

  8. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Delegates and Table

    Quote Originally Posted by KekraU
    Is there any way to achieve that trick by using QTableWidget / QTableWidgetItem ?
    If you are looking for different kinds of editors, you can use a custom item delegate with QTableWidget as well. Just be aware that the "default item delegate" QItemDelegate already offers through the QItemEditorFactory a few different editor widgets for different types of data:
    - QComboBox for booleans
    - QSpinBox for integers (both signed and unsigned)
    - QDoubleSpinBox for doubles
    - QDateTimeEdit for datetimes
    - QDateEdit for dates
    - QTimeEdit for times
    (- QLabel for pixmaps)
    - QLineEdit otherwise

    A common mistake is to insert all data as text. By this way you'll end up always having a QLineEdit as the editor. You can use the method QTableWidgetItem::setData() to set different kinds of data for an item.

    If you just want to place some widgets persistently in the cells, you may use cell widgets.
    J-P Nurmi

  9. #8
    Join Date
    Mar 2006
    Location
    Vadodara, Gujarat, India
    Posts
    65
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Delegates and Table

    Quote Originally Posted by KekraU
    Use of QTableWidget::addItem ( int row, int column, QTableWidgetItem * qtwItem ) could be useful?
    hi kekrau,
    i m using qt-4.1.2 but i can't find the function that u mentioned in qtablewidget.

    Thanx jpn for giving idea to use cellWidget
    Last edited by ankurjain; 18th May 2006 at 07:34.
    Do what u r afraid to do, and the death of fear is sure.

  10. #9
    Join Date
    May 2006
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Delegates and Table

    Thxs JPN.
    I have already looked at these Editors...
    I think the main point with these Editors (you mentionned) that they could be use outside TableView... to edit these kind of data (Time, Date, DateTime, Integer, and so on...) in QTableView/QTableWidget you have to include in your class which inherite from QItemDelegate, these editors.

    And in fact, that is what I did to solve my problem... include Q[Editor] (ComboBox / SpinBox...) in my cell depending on the type I want to edit.
    Plus, i used QTableWidget to setBackGroundColor... Really funny SpinBox on Red Background

    @Ankurjain:
    In fact, I search on the website, and I did not find QTableWidget::addItem, but on my 4.1.0 I have it.... But you got QTableWidget::setItem( int row, int column, QTableWidgetItem * item ) which has the same prototype and seems to do the same as previous addItem

    Your link
    Last edited by KekraU; 18th May 2006 at 19:51.

Similar Threads

  1. Table Widget Vs. Table View
    By winston2020 in forum Qt Programming
    Replies: 2
    Last Post: 19th October 2008, 09:56
  2. Easiest way to display icons in a table
    By ghorwin in forum Qt Programming
    Replies: 3
    Last Post: 19th March 2007, 13:09
  3. Setting Delegates per Column
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 30th May 2006, 21:39

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.