Results 1 to 2 of 2

Thread: retrieve QSqlTableModel from QTableView?

  1. #1
    Join Date
    Jul 2010
    Posts
    11
    Qt products
    Qt4
    Platforms
    MacOS X

    Default retrieve QSqlTableModel from QTableView?

    I have a QTableView that gets assigned its QSqlTableModel in a function.
    Later, I need to access that QSqlTableModel, but the function which created it has passed out of scope. As far as I understand it (which is not very far, because I am starting to get confused by all of this) the only thing that is still in scope for me is the QTableView, which I access via ui->QTableView.
    Is it possible to get access to the original QSqlTableModel via the QTableView? I have tried
    QSqlTableModel *newModel = ui->QTableView->model()
    but that doesn't seem to get me my QSqlTableModel. ui->QTableView->model() returns a QAbstractItemModel rather than a QSqlTableModel.
    What should I do? Should I just declare the original QSqlTableModel as global? It's just a pointer, but even so, isn't that bad form in objected oriented programming?

    My goal is actually quite simple: all I want to do is get the data from the selected row in the QTableView. I know how to get the rowid of the selected row, but I don't know how to get the data once i have the rowid. I think I need the model->record(rowid)->field(), but to get at the record I think I need the QSqlTableModel.

    As you can see, I am tying myself up in knots! I'm new to object oriented programming and QT.

    Any advice (other than the obvious advice that I should stick to the shallow end of the pool)?

    thanks in advance

    John
    Learning, one problem at a time.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: retrieve QSqlTableModel from QTableView?

    model() does return the QSqlTableModel, but not directly. It would be impossible to create a list of functions that return everything and the kitchen sink. The model() function returns the most common baseclass of a model, and that's an abstract class.

    Therefor there's a technique called casting. You need to tell the compiler that it's not an abstract class but a QSqlTableModel via a cast.

    However, casting is ugly and can lead to unexpected and difficult to find errors. Therefor it's much better to not do any casting at all.
    And that's why you are very close when you say that the model needs to be defined globally. This means you want to define your model at the same place you define your view.

    Example:

    Qt Code:
    1. MyClass
    2. {
    3. Private:
    4. QTableView *view;
    5. };
    To copy to clipboard, switch view to plain text mode 

    Then the model is available in the complete class.

Similar Threads

  1. Applying QSqlTableModel to QTableView
    By SykeS in forum Newbie
    Replies: 1
    Last Post: 20th May 2010, 09:34
  2. Replies: 2
    Last Post: 2nd March 2010, 04:24
  3. QSqlTableModel and QTableView critics.
    By Avrohom in forum Qt Programming
    Replies: 5
    Last Post: 11th September 2009, 03:17
  4. Custom SQL in QSqlTableModel/QTableView
    By estanisgeyer in forum Qt Programming
    Replies: 3
    Last Post: 1st February 2008, 13:18
  5. QTableView and QSQLTableModel
    By raphaelf in forum Qt Programming
    Replies: 6
    Last Post: 4th March 2006, 18:09

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.