Results 1 to 8 of 8

Thread: QItemDelegate, QDataWidgetMapper and QComboBox

Hybrid View

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

    Default Re: QItemDelegate, QDataWidgetMapper and QComboBox

    Ok, now I think to be closer to a solution...
    I've read this article http://doc.trolltech.com/qq/qq21-datawidgetmapper.html and the chapter "Using Delegates to Offer Choices" maybe will be useful!
    According to the previous article I changed the code as following:

    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. void setLabel(const QString &text);
    17.  
    18. private slots:
    19. void revert();
    20. void submit();
    21.  
    22. private:
    23. Ui::frmCategorie_AME ui;
    24. QSqlTableModel *modelAME;
    25. int id;
    26. };
    27. #endif // FRMCATEGORIE_AME_H
    To copy to clipboard, switch view to plain text mode 

    frmCategorie.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. QStringList items;
    28. QStringListModel *typeModel;
    29. items << tr("Green") << tr("Red");
    30. typeModel = new QStringListModel(items, this);
    31. ui.cboText->setModel(typeModel);
    32.  
    33. mapper = new QDataWidgetMapper(this);
    34. mapper->setModel(modelAME);
    35. mapper->setItemDelegate(new ComboBoxDelegate(this));
    36. mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
    37. mapper->addMapping(ui.lneDescrizione, 1);
    38. mapper->addMapping(ui.cboText, 2);
    39. mapper->setCurrentIndex(id);
    40.  
    41. connect(ui.btnAnnulla, SIGNAL(clicked()), this, SLOT(revert()));
    42. connect(ui.btnOk, SIGNAL(clicked()), this, SLOT(submit()));
    43.  
    44. qDebug() << "frmCategorie_AME Loaded!";
    45. }
    46.  
    47. /*----------------------------------------------------------------------------*/
    48.  
    49. void frmCategorie_AME::setLabel(const QString &text)
    50. {
    51. qDebug() << "frmAME->setLabel";
    52.  
    53. ui.lblValore->setText(text.toUtf8());
    54. }
    55.  
    56. /*----------------------------------------------------------------------------*/
    57.  
    58. void frmCategorie_AME::revert()
    59. {
    60. mapper->revert();
    61. qDebug() << "->frmCategorie_AME (Aggiunta Rifiutata!";
    62. modelAME->revertAll();
    63. modelAME->database().rollback();
    64.  
    65. qDebug() << "frmAME closed!";
    66. }
    67.  
    68. /*----------------------------------------------------------------------------*/
    69.  
    70. void frmCategorie_AME::submit()
    71. {
    72. if (mapper->submit())
    73. {
    74. mapper->setCurrentIndex(id);
    75. qDebug() << "Ok submit!";
    76.  
    77. if (modelAME->submitAll())
    78. {
    79. modelAME->database().commit();
    80. qDebug() << "Valore eliminato!";
    81. }
    82. else
    83. {
    84. modelAME->revertAll();
    85. modelAME->database().rollback();
    86. QMessageBox::warning(this, tr("Attenzione!"),
    87. tr("Il database ha riportato un errore: %1")
    88. .arg(modelAME->lastError().text()));
    89. }
    90. }
    91. else
    92. {
    93. qDebug() << "No submit!";
    94. QMessageBox::warning(this, tr("Attenzione!"),
    95. tr("Il database ha riportato un errore: %1")
    96. .arg(modelAME->lastError().text()));
    97. }
    98. }
    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 <QItemDelegate>
    5.  
    6. class ComboBoxDelegate : public QItemDelegate
    7. {
    8. Q_OBJECT
    9. public:
    10. ComboBoxDelegate(QObject *parent = 0);
    11. void setEditorData(QWidget *editor, const QModelIndex &index) const;
    12. void setModelData(QWidget *editor, QAbstractItemModel *model,
    13. const QModelIndex &index) const;
    14. };
    15. #endif /* COMBOBOXDELEGATE_H */
    To copy to clipboard, switch view to plain text mode 

    ComboBoxDelegate.cpp

    Qt Code:
    1. #include <QtGui>
    2. #include "ComboBoxDelegate.h"
    3.  
    4. /*----------------------------------------------------------------------------*/
    5.  
    6. ComboBoxDelegate::ComboBoxDelegate(QObject *parent) : QItemDelegate(parent)
    7. {
    8.  
    9. }
    10.  
    11. /*----------------------------------------------------------------------------*/
    12.  
    13. void ComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
    14. {
    15. if (!editor->metaObject()->userProperty().isValid())
    16. {
    17. if (editor->property("currentIndex").isValid())
    18. {
    19. editor->setProperty("currentIndex", index.data());
    20. return;
    21. }
    22. }
    23. QItemDelegate::setEditorData(editor, index);
    24. }
    25.  
    26. /*----------------------------------------------------------------------------*/
    27.  
    28. void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
    29. const QModelIndex &index) const
    30. {
    31. if (!editor->metaObject()->userProperty().isValid())
    32. {
    33. QVariant value = editor->property("currentIndex");
    34. if (value.isValid())
    35. {
    36. model->setData(index, value);
    37. return;
    38. }
    39. }
    40. QItemDelegate::setModelData(editor, model, index);
    41. }
    To copy to clipboard, switch view to plain text mode 

    but I'm havinga strange compiler error:

    ./debug\ComboBoxDelegate.o: In function `ZN16ComboBoxDelegateC2EP7QObject':
    D:/DEV_QT/GESCON_STD/project/src/ComboBoxDelegate.cpp:21: undefined reference to `vtable for ComboBoxDelegate'
    ./debug\ComboBoxDelegate.o: In function `ZN16ComboBoxDelegateC1EP7QObject':
    D:/DEV_QT/GESCON_STD/project/src/ComboBoxDelegate.cpp:21: undefined reference to `vtable for ComboBoxDelegate'

    Unbeliveble!!!

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

    Smile Re: QItemDelegate, QDataWidgetMapper and QComboBox

    Ok, the previous code works perfectly, the compiler error showed in the last post dues to incorrect end of line(CRLF for Win and LF for UNIX)!
    Thanks to wysota!!!

Similar Threads

  1. QDataWidgetMapper and QComboBox
    By mazurekwrc in forum Qt Programming
    Replies: 0
    Last Post: 31st March 2009, 14:02
  2. QDataWidgetMapper and QCombobox
    By miraks in forum Qt Programming
    Replies: 4
    Last Post: 6th December 2008, 18:53
  3. QComboBox QSqlQueryModel & relation.
    By matheww in forum Qt Programming
    Replies: 2
    Last Post: 20th June 2007, 05:56
  4. QDataWidgetMapper <=> QComboBox best practice
    By saknopper in forum Qt Programming
    Replies: 1
    Last Post: 18th January 2007, 11: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
  •  
Qt is a trademark of The Qt Company.