Results 1 to 8 of 8

Thread: QItemDelegate, QDataWidgetMapper and QComboBox

  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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QItemDelegate, QDataWidgetMapper and QComboBox

    So what exactly is the problem? What happens when you run the above code?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    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 Re: QItemDelegate, QDataWidgetMapper and QComboBox

    Is it possible to delegate a QComboBox to show text items instead of integer values?
    How can I do it?

    PS: QComboBos is mapped with QDataWidgetMapper.
    thanks.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QItemDelegate, QDataWidgetMapper and QComboBox

    Quote Originally Posted by cydside View Post
    Is it possible to delegate a QComboBox to show text items instead of integer values?
    Yes.

    How can I do it?
    By setting the delegate handling setEditorData() and setModelData() on the widget mapper. From what I see you have set it on the combobox instead of the data widget mapper.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    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 Re: QItemDelegate, QDataWidgetMapper and QComboBox

    Ok, I'm in the mess. So I've tried to delegate the QDataWidgetMapper ('mapper' in the code) as following:

    Qt Code:
    1. mapper = new QDataWidgetMapper(this);
    2. mapper->setModel(modelAME);
    3. mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
    4. mapper->addMapping(ui.lneDescrizione, 1); //lneDescrizione is a QLineEdit
    5. mapper->addMapping(ui.cboText, 2); //cbotext is a QComboBox
    6. mapper->setItemDelegate(&boxEU); //ComboBoxDelegate boxEU; from frmCategorie_AME.h
    7. mapper->setCurrentIndex(id);
    To copy to clipboard, switch view to plain text mode 

    but it doesn't work!
    I'm sorry, missing somthing...

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QItemDelegate, QDataWidgetMapper and QComboBox

    Did you change setEditorData() and setModelData() of the delegate? The ones you posted before should have segfaulted your application.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    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 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!!!

  8. #8
    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

    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.