Results 1 to 5 of 5

Thread: QSqlTableModel Insertion Problem

  1. #1
    Join Date
    Dec 2011
    Posts
    18
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QSqlTableModel Insertion Problem

    Hello everyone,

    I'm working on a project and in the "add" feature I want to show a single row from a QSQlTableModel where the user can input data, on a "simple" database table, it worked like a charm, but when I wanted to replicate the same pattern on my project It didn't behave as expected, here's the scenario:

    1- the program is launched, displaying a newly added row and hiding the previous ones

    2- The user adds data and submits it through a button

    3- A slot is called through the button to submit the newly added data, hide that row and display a new one

    Here's the code of the slot:

    Qt Code:
    1. {
    2. model->submit();
    3.  
    4. int row = model->rowCount();
    5. for (int i=0;i<row;i++)
    6. ui->tabsave->hideRow(row); //hide the previous rows
    7.  
    8. model->setData(model->index(row,5),QTime::currentTime());
    9.  
    10.  
    11. }
    To copy to clipboard, switch view to plain text mode 

    Here's the code regarding the formatting and display of the Model

    Qt Code:
    1. ui->tabsave->setModel(model);
    2. //model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    3. model->select();
    4.  
    5. for (int i=16;i<20;i++)
    6. {
    7. ui->tabsave->hideColumn(i); //hiding some columns from the user
    8. }
    9.  
    10.  
    11. int row = model->rowCount();
    12. for (int i=0;i<row;i++)
    13. {
    14. ui->tabsave->hideRow(i);
    15.  
    16. }
    17. model->insertRow(row);
    18. model->setData(model->index(row,5),QTime::currentTime());
    19. ui->tabsave->setRowHeight(row,80);
    20.  
    21. ////////////////SLOTS//////////////
    22.  
    23. QObject::connect(ui->next_one,SIGNAL(clicked()),this,SLOT(added_next()));
    To copy to clipboard, switch view to plain text mode 

    Voila, Hope I'll get some help around here. Thank you very much

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QSqlTableModel Insertion Problem

    It didn't behave as expected how? Crashed, failed to write to the actual database, failed to hide rows, wrote rows with rubbish data, wept uncontrollably for Kim Jong-Il

    QSqlTableModel::submit() does not necessarily write things to the database, depending on your QSqlTableModel::editStrategy()

  3. #3
    Join Date
    Dec 2011
    Posts
    18
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSqlTableModel Insertion Problem

    haha! Like the Kim hint xD!

    Well I've managed to solve the problem (partially!)

    Now everytime I invoke the "next" slot, I get one nice row, where the user can input data, the unsolved part,however, is that I cannot write that row to the database.

    In another example, I didn't set any edit strategy, and it worked fine, but with this particular model, I need some sort of control, I tried

    Qt Code:
    1. model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    To copy to clipboard, switch view to plain text mode 

    and later on in the slot I would issue

    Qt Code:
    1. model->submitAll();
    To copy to clipboard, switch view to plain text mode 

    Yet no data is written, I'm confused with this one!

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QSqlTableModel Insertion Problem

    It could be failing because the data you are trying to write violates table constraints. For example, if a certain column must be not null, or a primary key field is not populated or a duplicate, a foreign key constraint is violated, or a value fails a check constraint.

  5. #5
    Join Date
    Dec 2011
    Posts
    18
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSqlTableModel Insertion Problem

    Thanks a lot Chris! You rule!

    I hid some columns, and wasn't paying attention that I was submitting QStrings into TinyInt fields, Thanks again man

    EDIT: How can I label the thread asa [SOLVED] ?
    Last edited by Pluvius; 23rd December 2011 at 06:14.

Similar Threads

  1. problem with qsqltablemodel
    By ditsikts in forum Newbie
    Replies: 1
    Last Post: 30th October 2011, 22:34
  2. Replies: 4
    Last Post: 19th December 2010, 06:15
  3. Replies: 1
    Last Post: 16th June 2010, 20:19
  4. QSqlTableModel problem
    By waynew in forum Newbie
    Replies: 10
    Last Post: 8th January 2010, 00:48
  5. Multiple insertion using QSqlTableModel
    By munna in forum Newbie
    Replies: 3
    Last Post: 9th May 2006, 20:20

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.