Results 1 to 11 of 11

Thread: QSqlTableModel problem

  1. #1
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QSqlTableModel problem

    This is really a c++ problem but I'm confused and I'm sure someone can enlighten me easily....
    In the function openLog, I'm trying to set up a TableModel and a View and pass back the model so I can write to the model in a different function.
    checkPrefs() works fine and the view displays the data ok.
    When writeLog is called, it creates a new model/view instead of inserting into the model created in openLog. I guess the question is how to refer to the model passed back by openLog?

    Qt Code:
    1. QSqlTableModel *MainWindow::openLog(QString logName) {
    2. model->setTable("log");
    3. model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    4.  
    5. QTableView *view = new QTableView;
    6. view->setModel(model);
    7. view->setMinimumSize(1200,100);
    8. view->move(35,80);
    9. view->setSortingEnabled(TRUE);
    10. view->setWindowTitle("Log: " + logName);
    11. QHeaderView *header = view->horizontalHeader();
    12. header->setMovable(TRUE);
    13. view->show();
    14. return model;
    15. }
    16.  
    17. void MainWindow::checkPrefs() {
    18. ControlDB ctrl;
    19. QString open = ctrl.getOpenLastLog();
    20. QString log = ctrl.getLastLog();
    21.  
    22. if (open == "Y") {
    23. if (log != "") {
    24. QString logName = log + "_log";
    25. Connection conn;
    26. bool base = conn.createConnection(logName);
    27. openLog(log);
    28. }
    29. }
    30. }
    31.  
    32. void MainWindow::writeLog() {
    33. ControlDB ctrl;
    34. QString logTitle = ctrl.getLastLog();
    35. QString logName = logTitle + "_log.sqlite";
    36. int newID = ctrl.getMaxID(logName) + 1;
    37. QTime t(QTime::currentTime());
    38. QDate d(QDate::currentDate());
    39. Connection conn;
    40.  
    41. bool base = conn.createConnection(logName);
    42. QSqlTableModel *model = openLog(logTitle);
    43.  
    44. QSqlRecord record;
    45. record = model->record();
    46. record.setValue("id", newID);
    47. record.setValue("call", ui->mwCall->text());
    48. record.setValue("sent", ui->mwSent->text());
    49. record.setValue("rcvd", ui->mwRcvd->text());
    50. record.setValue("date", d);
    51. record.setValue("timeon", t);
    52. bool insertMe = model->insertRecord(-1, record);
    53.  
    54. if (insertMe) {
    55. model->select();
    56. clear(); // clear the mainwindow fields after a database save
    57. }else{
    58. qDebug() << model->lastError();
    59. }
    60. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSqlTableModel problem

    OOOPS! I can see that openLog gets called twice, so of course there are 2 models/views. Maybe I need some help restructuring this.

  3. #3
    Join Date
    Jan 2006
    Location
    Shanghai, China
    Posts
    52
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlTableModel problem

    Keep the view pointer as a member of your MainWindow.
    and you can get the model of the View by
    Qt Code:
    1. view->model();
    To copy to clipboard, switch view to plain text mode 
    1. Users don't have the manual, and if they did, they wouldn't read it.
    2. In fact, users can't read anything, and if they could, they wouldn't want to.

  4. #4
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSqlTableModel problem

    When I put the following in the header:

    Qt Code:
    1. QSqlTableView *view;
    To copy to clipboard, switch view to plain text mode 

    and the following in the class:

    Qt Code:
    1. QSqlTableView *view = new QTableView;
    To copy to clipboard, switch view to plain text mode 


    I get a compile error: forbids declaration of `QSqlTableView' with no type
    on the header line.

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QSqlTableModel problem

    There is no class called QSqlTableView in Qt... It has to be QTableView.

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

    waynew (14th December 2009)

  7. #6
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSqlTableModel problem

    Thanks Lykurg. I have been staring at this code too long!

    But, now when I try to reference the view in a local function, like

    view->show()

    The application crashes.

  8. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QSqlTableModel problem

    In the code you pasted you don't use any layout. I would recomend to use any kind of QLayout/QHBoxLayout... Then you don't have to call show().

    And if you don't know why your app crashes, debug your application.

  9. #8
    Join Date
    Dec 2009
    Posts
    18
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlTableModel problem

    Hi

    I'm using the same code

    But I don't understand how to edit ( alter some values ) in an already inserted record ....

    ... I need to in a first step populate some values and in a second step add some others ... but I can't get the correct row and I don't know ( after found it ) if I risk to delete already written values

  10. #9
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSqlTableModel problem

    I'm not using the model anywhere in my code for updates.
    Only inserts and selects.

    But if I needed to do updates, the only way I would know how would be to
    make your original table with an id primary key column (unique number value). Then use QsqlQuery to update the record where id = <id of row you want updated> The only column values that will be updated are the ones specified in your update statement - the other column values will remain unchanged.

    Good Luck!

  11. #10
    Join Date
    Dec 2009
    Posts
    18
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlTableModel problem

    QSqlTableView is fantastic .... I need only to give to it table and then I could also edit it on the fly !!! Without a lot of code !!! The only problem I found was the one I wrote before .

    I supposed that a "hidden" (for me) function provides a way to find the correct row.

    I will found a solution

    Thank you Bye

  12. #11
    Join Date
    Sep 2009
    Location
    Tashkent, Uzbekistan
    Posts
    107
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSqlTableModel problem

    You can subclass directly from the QAbstractView or overwrite that function
    -- Tanuki

    per cauda vel vaculus cauda

Similar Threads

  1. Very strange socket programming problem
    By montylee in forum Qt Programming
    Replies: 5
    Last Post: 11th November 2008, 13:05
  2. Problem in using QHttp with QTimer
    By Ferdous in forum Newbie
    Replies: 2
    Last Post: 6th September 2008, 13:48
  3. QTableView and QSqlTableModel problem
    By Misenko in forum Qt Programming
    Replies: 5
    Last Post: 19th August 2008, 22:58
  4. Grid Layout Problem
    By Seema Rao in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2006, 13:45
  5. fftw problem
    By lordy in forum General Programming
    Replies: 1
    Last Post: 16th March 2006, 22:36

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.