Results 1 to 5 of 5

Thread: Access data from QSqlTableModel

  1. #1
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Red face Access data from QSqlTableModel

    Hi,

    I am very raw with model/view hence the many questions..

    On my QSqlTableModel I have the following code to present the data:

    Qt Code:
    1. QVariant maintModel::data(const QModelIndex &index, int role) const
    2. {
    3. if (role == Qt::DisplayRole)
    4. {
    5. QString var;
    6. var = record(index.row()).value("cnty_nam").toString();
    7. qDebug() << index.row() << displayColumnCode << var;
    8. return var;
    9. }
    10. return QVariant();
    11. }
    To copy to clipboard, switch view to plain text mode 

    I am just displaying one column thus I just use row. From the debug I can see that the row numbers are fine, also the column exists, but in this case var is always empty! because record(index.row()).value("cnty_nam").isValid() is always false!!!

    I initialize the model with:

    Qt Code:
    1. m_mainmodel = new maintModel(this,db);
    2. m_mainmodel->setTable("country");
    3. m_mainmodel->setEditStrategy(QSqlTableModel::OnManualSubmit);
    4. if (m_mainmodel->select())
    5. {
    6. ui->ListView1->setModel(m_mainmodel);
    7. }
    To copy to clipboard, switch view to plain text mode 

    For sure is something very silly... but I don't know what is happening!!!

    Thanks,
    Carlos.
    Last edited by qlands; 5th July 2011 at 14:32.

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Access data from QSqlTableModel

    One question:

    "Why are you re-implementing this function?"

    You can use QSqlTableModel as it is.
    A camel can go 14 days without drink,
    I can't!!!

  3. #3
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Access data from QSqlTableModel

    Well I have more than the display role. For example I need to display a different pixmap based on some extra data attached to every record (that is not stored in the database). I use this under Qt:ecorationRole. That's why i am reimplementing this.

  4. #4
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Access data from QSqlTableModel

    Ok,

    I suggest you to call the parent implementation when you have to fetch data and add your code only to get information specific to your implementation.

    Qt Code:
    1. QVariant maintModel::data(const QModelIndex &index, int role) const
    2. {
    3. QVariant result;
    4. if (role == Qt::DecorationRole) {
    5. // Your code
    6. }
    7. else
    8. result = QSqlTableModel::data(index, role);
    9.  
    10. return result;
    11. }
    To copy to clipboard, switch view to plain text mode 
    A camel can go 14 days without drink,
    I can't!!!

  5. The following user says thank you to mcosta for this useful post:

    qlands (5th July 2011)

  6. #5
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Access data from QSqlTableModel

    Yep!!!.

    Many thanks,
    Carlos

Similar Threads

  1. QSqlTableModel set Integer data to NULL
    By lynnH in forum Qt Programming
    Replies: 2
    Last Post: 21st April 2011, 17:11
  2. QTableView, QSqlTableModel - data disappears
    By msh in forum Qt Programming
    Replies: 1
    Last Post: 15th November 2008, 11:50
  3. setting UserRole data in QSqlTableModel
    By orgads in forum Qt Programming
    Replies: 1
    Last Post: 2nd June 2008, 09:40
  4. QSqlTableModel submitAll access query
    By patrik08 in forum Qt Programming
    Replies: 4
    Last Post: 27th April 2007, 08:04
  5. Access to QSqlTableModel::isDirty Question.
    By patrik08 in forum Qt Programming
    Replies: 3
    Last Post: 12th April 2007, 17:49

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.