Results 1 to 5 of 5

Thread: Problems with QSqlRelationalTableModel

  1. #1
    Join Date
    Apr 2009
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Problems with QSqlRelationalTableModel

    I'm having a bit of trouble with QSqlRelationalTableModel. Every time I submitAll(), the value of a relational column (Customers.name) is written to my table instead of the integer key (Projects.customerId). Below is some pseudo code. Any suggestions for how to debug and/or resolve this issue?

    The field correctly reads (customerNameLineEdit gets populated with the correct value) however a problem occurs on write. I've tried with and without foreign keys. I'm using MySql as my database.

    Qt Code:
    1. CREATE TABLE IF NOT EXISTS `projects` (
    2. `id` int(11) NOT NULL AUTO_INCREMENT,
    3. `customerId` int(11) NOT NULL,
    4. `industryId` int(11) DEFAULT NULL,
    5. `opCode` varchar(255) DEFAULT NULL,
    6. `statusId` int(11) DEFAULT NULL,
    7. `startDate` date DEFAULT NULL,
    8. `endDate` date DEFAULT NULL,
    9. `notes` text,
    10. PRIMARY KEY (`id`),
    11. UNIQUE KEY `opCode` (`opCode`),
    12. KEY `customerId` (`customerId`),
    13. KEY `status` (`statusId`),
    14. KEY `industryId` (`industryId`),
    15. KEY `customerId_2` (`customerId`)
    16. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=39 ;
    17.  
    18. CREATE TABLE IF NOT EXISTS `customers` (
    19. `id` int(11) NOT NULL AUTO_INCREMENT,
    20. `name` text,
    21. PRIMARY KEY (`id`)
    22. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=16 ;
    23.  
    24.  
    25. // SETUP THE TABLE
    26. projectsModel.setTable("Projects");
    27. projectsModel.setRelation(1, QSqlRelation("Customers", "id", "name"));
    28. projectsModel.setRelation(2, QSqlRelation("refIndustries", "naicCode", "name"));
    29. projectsModel.setRelation(4, QSqlRelation("refStatuses", "id", "name"));
    30. projectsModel.select();
    31.  
    32. mapper = new QDataWidgetMapper(this);
    33. mapper->setModel(projectModel);
    34. mapper->setItemDelegate(new QSqlRelationalDelegate(mapper));
    35. mapper->addMapping(ui->customerNameLineEdit, projectModel->fieldIndex("Customers_Name"));
    36. mapper->addMapping(ui->industryComboBox, projectModel->fieldIndex("refIndustries_name"));
    37. mapper->addMapping(ui->projectCodeLineEdit, projectModel->fieldIndex("opCode"));
    38. mapper->addMapping(ui->statusComboBox, projectModel->fieldIndex("refStatuses_name"));
    39. mapper->addMapping(ui->startDateDateEdit, projectModel->fieldIndex("startDate"));
    40. mapper->addMapping(ui->endDateDateEdit, projectModel->fieldIndex("endDate"));
    41. mapper->addMapping(ui->notesTextEdit, projectModel->fieldIndex("notes"));
    42. mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
    43. mapper->setCurrentIndex(rowId);
    44.  
    45.  
    46. // LATER I DO A SUBMIT LIKE THIS
    47. mapper->submit())
    48. projectModel->submitAll()
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2008
    Posts
    141
    Thanks
    10
    Thanked 9 Times in 9 Posts

    Default Re: Problems with QSqlRelationalTableModel

    Hi,

    take a look at the sqlwidgetmapper exmaple. As far as I remember you need to set the comboBox Model to the relationModel of the specific column.

  3. #3
    Join Date
    Apr 2009
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Problems with QSqlRelationalTableModel

    I was trying to map a QLineEdit, but I'm guessing this is not supported with the mapper. I have it working now but I had to combine two SQL tables, which I was trying to avoid. I'm guessing there might be a way to get a QComboBox to behave more like a QLineEdit, but I'll have to come back to that later.

    Another interesting feature is that the mapper looses it's index after submits and commits. The work around is to save the index before hand and then reset it after all submits/commits.

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

    Default Re: Problems with QSqlRelationalTableModel

    Hi, sorry for the late reply to this post but I was struggling with the same problem and think I have worked out the solution.

    The problem seems to be that the QDataWidgetMapper doesn't re-populate itself correctly after updates to the model when the submit policy is QDataWidgetMapper::AutoSubmit. This is true of both the standard item delegate and the QSqlRelationalDelegate (which just calls the standard delegate if it is not populating a combo box anyway).

    To get a QLineEdit to work well with a QDataWidgetMapper and a QRelationalTableModel it is necessary to subclass either QItemDelegate or QSqlRelationalDelegate as follows:
    Qt Code:
    1. void MyItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
    2. {
    3. if (!strcmp(editor->metaObject()->className(),"QLineEdit")) {
    4. QLineEdit *le = qobject_cast<QLineEdit *>(editor);
    5. le->setText(index.data().toString());
    6. }
    7. else
    8. QItemDelegate::setEditorData(editor, index);
    9. }
    To copy to clipboard, switch view to plain text mode 
    Then install MyItemDelegate in the same way as you installed QSqlRelationalDelegate:
    Qt Code:
    1. mapper->setItemDelegate(new MyItemDelegate);
    To copy to clipboard, switch view to plain text mode 
    Hope this helps someone, or if I have got something wrong I would be happy to hear why.
    Last edited by certqt; 3rd December 2010 at 16:12. Reason: Corrected typo

  5. #5
    Join Date
    Jan 2014
    Posts
    4
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Problems with QSqlRelationalTableModel

    Sorry for digging out this old thread, but I have the same issue. Unfortunately subclassing QSqlRelationalDelegate as described above did not work for me: my QLineEdit is still empty.

    Anything else I could do?

    Sorry for digging out this old thread, but I have the same issue. Unfortunately subclassing QSqlRelationalDelegate as described above did not work for me: my QLineEdit is still empty.

    Anything else I could do?


    Added after 45 minutes:


    After struggling with this issue for several days, I finally figured out that using
    Qt Code:
    1. mapper->addMapping(someLineEdit, model->fieldIndex("fieldName"));
    To copy to clipboard, switch view to plain text mode 
    does not work. I already had issues using QSqlTableModel::fieldIndex() before, but it worked for the relational QComboBox, so I didn't think this could be the issue. After switching to plain ints, everything works as expected. Sorry again for digging out this thread.
    Last edited by frankb; 5th January 2014 at 02:57.

Similar Threads

  1. problems creating toolbar...(do see the attachment)!
    By sumit in forum Qt Programming
    Replies: 15
    Last Post: 10th September 2008, 12:23
  2. Problems with scope and C header functions
    By waldowoc in forum Newbie
    Replies: 5
    Last Post: 5th August 2008, 12:29
  3. flicker and wierd resize problems ...
    By momesana in forum Qt Programming
    Replies: 1
    Last Post: 12th May 2008, 19:00
  4. Replies: 2
    Last Post: 8th March 2007, 23:22

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.