Results 1 to 11 of 11

Thread: QSqlTableModel setData not working?

  1. #1
    Join Date
    Apr 2009
    Posts
    10
    Thanks
    3
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Red face QSqlTableModel setData not working?

    Please Help!
    I've been fighting this for a long time and would be more than greatfull someone could help...Basicly I'v got a sql table (a QTableView and a QSqlTableModel) I'v got everything working ***Except*** One thing!
    I can't find any way to set the data of a field...Here's what I'm trying to do: I'v got a table with a buch of columns and rows and a text box where the user enters the tag and it appends the current date time and adds 1 to the daysAttended column

    Here's the function that is executed when the user hit's enter in the textBox

    Qt Code:
    1. void MainWindow::locate()
    2. {
    3. if(db.isOpen())
    4. {
    5. QString datesAttended = "";
    6. int daysAttended = 0;
    7. int daysMissed = 0;
    8. int matchingRow = 0;
    9.  
    10. QString searchTag = barcodeEdit->text().toUpper();
    11.  
    12. if(query->exec(QString("select * from persons where UID = \"%0\" LIMIT 0,1").arg(searchTag)))
    13. {
    14. if(query->isSelect() && query->next() && query->value(0).isValid())
    15. {
    16. int matchingRow = query->value(0).toInt();
    17.  
    18. daysAttended = model->record(matchingRow).value(6).toInt();
    19. datesAttended = model->record(matchingRow).value(5).toString();
    20.  
    21. if(model->setData(model->index(matchingRow,6),QVariant(daysAttended + 1)) && model->setData(model->index(matchingRow,5),QVariant(datesAttended + QDateTime::currentDateTime().toString())))
    22. {barcodeEdit->setText("");} else {
    23. QMessageBox::critical(0, qApp->tr("Error Setting Record!"),
    24. qApp->tr(" %0.\n \n"
    25. "Click Close to continue.").arg(model->lastError().text()), QMessageBox::Close);}
    26. } else {
    27. QMessageBox::critical(0, qApp->tr("Could not find an entry with the matching criticia!"),
    28. qApp->tr("Unable to find a person with the matching criticia! \n\n"
    29. "Click Close to continue."), QMessageBox::Close);}
    30. } else {
    31. QMessageBox::critical(0, qApp->tr("Error Executing Query!"),
    32. qApp->tr(" %0.\n \n"
    33. "Click Close to continue.").arg(query->lastError().text()), QMessageBox::Close);}
    34. } else {
    35. QMessageBox::critical(0, qApp->tr("Database Not Open!"),
    36. qApp->tr("You must have a database open before you can use this function!. \n\n"
    37. "Click Close to continue."), QMessageBox::Close);}
    38. }
    To copy to clipboard, switch view to plain text mode 

    That works and it dose what I want accept on thing...after that saving dosn't work! when I save the changes it returns the following error:

    database is locked Unable to fetch row and it dosn't save!

    Here's my save function

    Qt Code:
    1. bool MainWindow::save()
    2. {
    3. if(db.isOpen()){
    4.  
    5. model->database().transaction();
    6. if (model->submitAll()) {
    7. model->database().commit();
    8. return true;
    9. } else {
    10. model->database().rollback();
    11. QMessageBox::warning(this, tr("Cached Table"),
    12. tr("The database reported an error: %1")
    13. .arg(model->lastError().text()));
    14. return false;
    15. }
    16. } else {
    17. QMessageBox::warning(this, tr("Database Not Open!"),
    18. tr("No database is open!"));
    19. return false;
    20. }
    21. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Apr 2009
    Posts
    10
    Thanks
    3
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Unhappy Re: QSqlTableModel setData not working?

    can't anybody please!!!! help me? I've been sturgling with this thing so much!
    Oh and one more thing...setData dose update the view but changes cannot be saved

  3. #3
    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: QSqlTableModel setData not working?

    The problem is with the database not with setData(). You probably have more than one connection to the same database and you operate on the same table with both of them. Seems that the database is set in a way that prevents such situations. If I were to guess, I'd say this was a MySQL installation.
    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.


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

    jon-ecm (7th May 2009)

  5. #4
    Join Date
    Apr 2009
    Posts
    10
    Thanks
    3
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: QSqlTableModel setData not working?

    It's using SQLITE

  6. #5
    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: QSqlTableModel setData not working?

    Quote Originally Posted by jon-ecm View Post
    It's using SQLITE
    Yeah, I had issues with locks with that one too. You are facing a database issue. Try simplifying your code (especially parts that use SQL queries), maybe you can avoid the locks.
    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. The following user says thank you to wysota for this useful post:

    jon-ecm (7th May 2009)

  8. #6
    Join Date
    Apr 2009
    Posts
    10
    Thanks
    3
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: QSqlTableModel setData not working?

    Ok! Thank you so much!!!
    Also do you have any better idea of searching for the user with the matching tag?
    Is there a way to do this without talking directly to the database?
    Last edited by jon-ecm; 7th May 2009 at 01:40. Reason: spelling error

  9. #7
    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: QSqlTableModel setData not working?

    If your model keeps records from the "persons" table then you can query the model instead of asking the database. I'm not convinced this will help though.
    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.


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

    jon-ecm (7th May 2009)

  11. #8
    Join Date
    Apr 2009
    Posts
    10
    Thanks
    3
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Smile Re: QSqlTableModel setData not working?

    Thanks! I tried what you said but it didn't help
    Thank you so much for helping me though! This is the first time I'v ever even been anwsered on this fourm!

    If you have any ideas though I'd love it!

  12. #9
    Join Date
    Apr 2009
    Posts
    10
    Thanks
    3
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: QSqlTableModel setData not working?

    I got it working, at least I think so lol

    Here's the function I'm using now!

    Qt Code:
    1. for(int i = 0; i <= model->rowCount();i++)
    2. {
    3. if(model->data(model->index(i,1)).toString() == searchTag){
    4. qDebug() << "Found Match At: " << i;
    5. barcodeEdit->setText("");
    6. model->setData(model->index(i,6),model->data(model->index(i,6)).toInt() + 1);//Add one to the days attended field that corresponds to the user
    7. model->setData(model->index(i,5),model->data(model->index(i,5)).toString() + QDateTime::currentDateTime().toString() + '\n');//Add one to the dates attended field that corresponds to the user
    8. break;
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

    It's very unpolished right now and I need to add error catching but that's the idea! If you see anything that I'm doing wrong Please let me know!

  13. #10
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSqlTableModel setData not working?

    maybe it's better to use QAbstractItemModel::match for seching items?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  14. #11
    Join Date
    Apr 2009
    Posts
    10
    Thanks
    3
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: QSqlTableModel setData not working?

    Thanks! I hadn't noticed that! I'll give it a shot!

Similar Threads

  1. Qt+QWebKit+Java+Flash not working
    By progDes in forum Qt Programming
    Replies: 2
    Last Post: 30th April 2009, 17:49
  2. QSqlTableModel and possible bug with setData?
    By sylvainb in forum Qt Programming
    Replies: 2
    Last Post: 25th February 2009, 22:43
  3. QDevelop and CTags -> Not working
    By philwinder in forum Qt-based Software
    Replies: 13
    Last Post: 9th May 2008, 22:40
  4. Don't want QTextBrowser to look in working directory
    By magland in forum Qt Programming
    Replies: 1
    Last Post: 21st October 2007, 00:14
  5. GUI thread and Working thread comunication
    By FasTTo in forum Qt Programming
    Replies: 2
    Last Post: 13th September 2007, 16:31

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.