Results 1 to 8 of 8

Thread: QItemDelegate, QDataWidgetMapper and QComboBox

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2008
    Location
    Rome, Italy
    Posts
    95
    Thanks
    19
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default QItemDelegate, QDataWidgetMapper and QComboBox

    Hi to all,
    I'm using a QDialog to edit data from QSqlTableModel through QDataWidgetMapper. Now I've a record field integer that can take only five values but I need to code in text those values and use a QComboBox to do it. So I've thought to implement a QItemDelegate following the example demos/spreadsheet/main.cpp that came with QAssistant but it doesn't work

    The attachment is a Snapshot about the Qdialog.

    The following code to attempt an explanation :

    frmCategorie_AME.h
    Qt Code:
    1. #ifndef FRMCATEGORIE_AME_H
    2. #define FRMCATEGORIE_AME_H
    3.  
    4. #include <QtGui>
    5. #include <QtSql>
    6.  
    7. #include "ui_frmCategorie_AME.h"
    8. #include "ComboBoxDelegate.h"
    9.  
    10. class frmCategorie_AME : public QDialog
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. frmCategorie_AME(QSqlTableModel *model, const int &isItNew, QWidget *parent=0);
    16.  
    17. private slots:
    18. void revert();
    19. void submit();
    20.  
    21. private:
    22. Ui::frmCategorie_AME ui;
    23. QSqlTableModel *modelAME;
    24. int id;
    25. ComboBoxDelegate boxEU;
    26. };
    27.  
    28. #endif // FRMCATEGORIE_AME_H
    To copy to clipboard, switch view to plain text mode 

    frmCategorie_AME.cpp
    Qt Code:
    1. #include "frmCategorie_AME.h"
    2.  
    3. /*----------------------------------------------------------------------------*/
    4.  
    5. frmCategorie_AME::frmCategorie_AME(QSqlTableModel *model, const int &isItNew,
    6. QWidget *parent) : QDialog(parent)
    7. {
    8. ui.setupUi(this);
    9. this->setWindowFlags(Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
    10. this->setAttribute(Qt::WA_DeleteOnClose);
    11.  
    12. modelAME = new QSqlTableModel(this);
    13. modelAME = model;
    14.  
    15. modelAME->database().transaction();
    16.  
    17. if (isItNew < 0)
    18. {
    19. id = modelAME->rowCount();
    20. modelAME->insertRow(id);
    21. }
    22. else
    23. {
    24. id = isItNew;
    25. }
    26.  
    27. mapper = new QDataWidgetMapper(this);
    28. mapper->setModel(modelAME);
    29. mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
    30. mapper->addMapping(ui.lneDescrizione, 1);
    31. mapper->addMapping(ui.cboText, 2);
    32. mapper->setCurrentIndex(id);
    33.  
    34. ui.cboText->setItemDelegate(&boxEU);
    35.  
    36. connect(ui.btnAnnulla, SIGNAL(clicked()), this, SLOT(revert()));
    37. connect(ui.btnOk, SIGNAL(clicked()), this, SLOT(submit()));
    38.  
    39. qDebug() << "frmCategorie_AME Loaded!";
    40. }
    41.  
    42. /*----------------------------------------------------------------------------*/
    43.  
    44. void frmCategorie_AME::revert()
    45. {
    46. mapper->revert();
    47. qDebug() << "->frmCategorie_AME Aggiunta Rifiutata!";
    48. modelAME->revertAll();
    49. modelAME->database().rollback();
    50.  
    51. qDebug() << "frmAME closed!";
    52. }
    53.  
    54. /*----------------------------------------------------------------------------*/
    55.  
    56. void frmCategorie_AME::submit()
    57. {
    58. if (mapper->submit())
    59. {
    60. mapper->setCurrentIndex(id);
    61. qDebug() << "Ok submit!";
    62.  
    63. if (modelAME->submitAll())
    64. {
    65. modelAME->database().commit();
    66. qDebug() << "Valore eliminato!";
    67. }
    68. else
    69. {
    70. modelAME->revertAll();
    71. modelAME->database().rollback();
    72. QMessageBox::warning(this, tr("Attenzione!"),
    73. tr("Il database ha riportato un errore: %1")
    74. .arg(modelAME->lastError().text()));
    75. }
    76. }
    77. else
    78. {
    79. qDebug() << "No submit!";
    80. QMessageBox::warning(this, tr("Attenzione!"),
    81. tr("Il database ha riportato un errore: %1")
    82. .arg(modelAME->lastError().text()));
    83. }
    84. }
    To copy to clipboard, switch view to plain text mode 

    ComboBoxDelegate.h
    Qt Code:
    1. #ifndef COMBOBOXDELEGATE_H
    2. #define COMBOBOXDELEGATE_H
    3.  
    4. #include <QtGui>
    5.  
    6. class ComboBoxDelegate: public QItemDelegate
    7. {
    8. public:
    9. ComboBoxDelegate(QObject *parent = 0);
    10. QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &,
    11. const QModelIndex &index) const;
    12. void setEditorData(QWidget *editor, const QModelIndex &index) const;
    13. void setModelData(QWidget *editor, QAbstractItemModel *model,
    14. const QModelIndex &index) const;
    15.  
    16. private slots:
    17. void commitAndCloseEditor();
    18. };
    19.  
    20. #endif /* COMBOBOXDELEGATE_H */
    To copy to clipboard, switch view to plain text mode 

    ComboBoxDelegate.cpp

    Qt Code:
    1. #include "ComboBoxDelegate.h"
    2.  
    3. /*----------------------------------------------------------------------------*/
    4.  
    5. ComboBoxDelegate::ComboBoxDelegate(QObject *parent)
    6. : QItemDelegate(parent)
    7. {
    8. qDebug() << "ComboBoxDelegate Costruttore";
    9.  
    10. }
    11.  
    12. /*----------------------------------------------------------------------------*/
    13.  
    14. QWidget *ComboBoxDelegate::createEditor(QWidget *parent,
    15. const QStyleOptionViewItem&, const QModelIndex &index) const
    16. {
    17. QComboBox *editor = new QComboBox(parent);
    18. editor->addItem("Green", 0);
    19. editor->addItem("Red", 1);
    20. editor->addItem("Yellow", 2);
    21. editor->addItem("Blue", 3);
    22. editor->addItem("Magenta", 4);
    23. editor->setCurrentIndex(0);
    24. return editor;
    25. }
    26.  
    27. /*----------------------------------------------------------------------------*/
    28.  
    29. void ComboBoxDelegate::commitAndCloseEditor()
    30. {
    31.  
    32. QComboBox *editor = qobject_cast<QComboBox *>(sender());
    33. emit commitData(editor);
    34. emit closeEditor(editor);
    35.  
    36. }
    37.  
    38. /*----------------------------------------------------------------------------*/
    39.  
    40. void ComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
    41. {
    42. QComboBox *edit = qobject_cast<QComboBox *>(editor);
    43. int value = index.model()->data(index, Qt::EditRole).toInt();
    44. edit->setCurrentIndex(value);
    45.  
    46. }
    47.  
    48. /*----------------------------------------------------------------------------*/
    49.  
    50. void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
    51. const QModelIndex &index) const
    52. {
    53. QComboBox *edit = qobject_cast<QComboBox *>(editor);
    54. model->setData(index, edit->itemData(edit->currentIndex()));
    55. }
    To copy to clipboard, switch view to plain text mode 

    Thanks in advance!
    Attached Images Attached Images

Similar Threads

  1. QDataWidgetMapper and QComboBox
    By mazurekwrc in forum Qt Programming
    Replies: 0
    Last Post: 31st March 2009, 13:02
  2. QDataWidgetMapper and QCombobox
    By miraks in forum Qt Programming
    Replies: 4
    Last Post: 6th December 2008, 17:53
  3. QComboBox QSqlQueryModel & relation.
    By matheww in forum Qt Programming
    Replies: 2
    Last Post: 20th June 2007, 04:56
  4. QDataWidgetMapper <=> QComboBox best practice
    By saknopper in forum Qt Programming
    Replies: 1
    Last Post: 18th January 2007, 10:50

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.