Results 1 to 4 of 4

Thread: Custom QLineEdit to store NULL with QDataWidgetMapper

  1. #1
    Join Date
    Sep 2010
    Posts
    14
    Thanks
    5

    Default Custom QLineEdit to store NULL with QDataWidgetMapper

    I have a QLineEdit which is mapped to a table column using a QDataWidgetMapper.

    Qt Code:
    1. CREATE TABLE mytable (EMP_REF varchar(10) NOT NULL, EMP_NAME varchar(40));
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. model = new QSQLRelationalTableModel(this)
    2. model->setTable("mytable");
    3. model->select();
    4.  
    5. mapper = new QDataWidgetMapper(this);
    6. mapper->setModel(model);
    7. mapper->addMapping(ui->empRefEdit, 1);
    To copy to clipboard, switch view to plain text mode 

    The problem is that if the user does not enter a reference, the mapper simply populates the field with an empty string "" instead of a NULL and the database referential integrity is defeated.

    I read another post suggesting that QLineEdit should be subclassed and a SQLText property added, but I haven't been able to work out a good way to update this property.

    The QDataWidgetMapper appears not to use the text() getter method, but accesses the text property directly (I guess it has to!).

    Is the only way to create a shadow SQLText property to override the event handler and update it on every keypress event? This seems a little clumsy.

    Many thanks for help.

  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: Custom QLineEdit to store NULL with QDataWidgetMapper

    You need to employ a custom delegate for the widget mapper to map between the widget and the model.
    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. The following user says thank you to wysota for this useful post:

    certqt (9th November 2010)

  4. #3
    Join Date
    Sep 2010
    Posts
    14
    Thanks
    5

    Default Re: Custom QLineEdit to store NULL with QDataWidgetMapper

    Thanks for pointing me in the right direction. For anyone who is interested, this is my final solution to the problem:

    Qt Code:
    1. model = new QSqlRelationalTableModel(this);
    2. model->setTable("mytable");
    3. model->select();
    4.  
    5. mapper = new QDataWidgetMapper(this);
    6. mapper->setModel(model);
    7. mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
    8. mapper->setItemDelegate(new NullDelegate);
    9. mapper->addMapping(ui->empRefEdit, 1);
    To copy to clipboard, switch view to plain text mode 

    And the delegate:

    Qt Code:
    1. class NullDelegate : public QItemDelegate
    2. {
    3. Q_OBJECT
    4. public:
    5. NullDelegate (QObject *parent = 0);
    6. void setEditorData(QWidget *editor, const QModelIndex &index) const;
    7. void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
    8. };
    9.  
    10. void NullDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
    11. {
    12. model->setData(index, editor->property("text") == "" ?
    13. editor->property("text"));
    14. }
    15.  
    16. void NullDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
    17. {
    18. editor->setProperty("text", index.data());
    19. }
    To copy to clipboard, switch view to plain text mode 

    Now, the next problem..... what if I need to use a QSqlRelationalDelegate as well to populate some combo boxes?

  5. #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: Custom QLineEdit to store NULL with QDataWidgetMapper

    Make your delegate a subclass of QSqlRelationalDelegate.
    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.


  6. The following user says thank you to wysota for this useful post:

    certqt (10th November 2010)

Similar Threads

  1. Replies: 1
    Last Post: 8th August 2013, 15:57
  2. QDataWidgetMapper and QLineEdit
    By GuL in forum Newbie
    Replies: 3
    Last Post: 13th August 2008, 15:58
  3. How to store custom class in QHash?
    By Misenko in forum Qt Programming
    Replies: 3
    Last Post: 7th August 2008, 09:57
  4. how to force QLineEdit not null ?
    By lovelypp in forum Qt Programming
    Replies: 3
    Last Post: 25th July 2008, 10:26
  5. Store an custom enum in QSettings
    By Lykurg in forum Qt Programming
    Replies: 1
    Last Post: 14th March 2007, 21:06

Tags for this Thread

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.