Results 1 to 4 of 4

Thread: cant read data from my model of QIdentityProxyModel

  1. #1
    Join Date
    Jan 2012
    Location
    Argentina
    Posts
    167
    Thanks
    33
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default cant read data from my model of QIdentityProxyModel

    Hi everyone, im having trouble trying to fetch the data from my QTableView. I have the following structure:

    I get the data from the database with a QSqlQueryModel, then I create my model extending from QIdentityProxyModel and set as its source model the Qsqlquerymodel. Finally I set my model as the model of the tableview. The thing is i can get the rows and collumns but not the data of a cell.

    Here is the code from:

    widget using the table
    Qt Code:
    1. model_bajo_stock = new QSqlQueryModel ();
    2. if(db.isOpen ()){
    3. model_bajo_stock->setQuery("SELECT Descripcion, Stock_unidades, fecha_modificacion, proveedor FROM TABLE_PRODUCTOS WHERE stock_unidades < 50 ORDER BY Descripcion", db);
    4. if (model_bajo_stock->lastError().type() != QSqlError::NoError)
    5. ui->label_error->setText(model_bajo_stock->lastError().text());
    6. else{
    7. myBajoStock= new MyModelView();
    8. myBajoStock->setSourceModel(model_bajo_stock);
    9. ui->tableViewBajoStock->setModel(myBajoStock);
    10. }
    11. ui->tableViewBajoStock->setAlternatingRowColors(true);
    12. ui->tableViewBajoStock->show();
    To copy to clipboard, switch view to plain text mode 

    my model: (WHERE I GET THAT I HAVE NO DATA {segmentation fault})
    Qt Code:
    1. QVariant MyModelView::data(const QModelIndex &proxyIndex, int role) const{
    2. if (role== Qt::DisplayRole && proxyIndex.column()==1){
    3. QVariant val =QIdentityProxyModel::data(proxyIndex, role);
    4. qDebug()<<val.toString();
    5. }
    6. if (role == Qt::BackgroundRole && proxyIndex.column()==1){
    7. qDebug()<<((QSqlQueryModel*)proxyIndex.model())->record(proxyIndex.column()).value(1).toString();
    8. return QBrush(Qt::red);
    9. }
    10. return QIdentityProxyModel::data(proxyIndex, role);
    11. }
    To copy to clipboard, switch view to plain text mode 

    "5"
    ""
    "49"
    ""
    "2"
    ""
    I get 3 rows from the Select query as you can see. The values are there but i cannot fetch them when the role is not display.

    Thank you all in advance.
    Last edited by KillGabio; 11th July 2013 at 09:23. Reason: new foundings

  2. #2
    Join Date
    Jan 2012
    Location
    Argentina
    Posts
    167
    Thanks
    33
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: cant read data from my model of QIdentityProxyModel

    I modified the code, now it paints red the values i want but i cant change the font because i cant feth the value:

    Qt Code:
    1. QVariant MyModelView::data(const QModelIndex &proxyIndex, int role) const{
    2. if (role == Qt::BackgroundRole && proxyIndex.column()==1){
    3. int value= proxyIndex.model()->data(proxyIndex.model()->index(proxyIndex.row(),1),Qt::DisplayRole).toInt();
    4. if (value < 6){
    5. //proxyIndex.model()->setData(proxyIndex.model()->index(proxyIndex.row(),0), QFont("Courier", 8, QFont::Bold, true), Qt::FontRole);
    6. return QBrush(Qt::red);
    7. }
    8. }
    9. /*if (role == Qt::FontRole && proxyIndex.column()==0){
    10.   int value= proxyIndex.model()->data(proxyIndex.model()->index(proxyIndex.row(),1),Qt::DisplayRole).toInt();
    11.   if (value < 6){
    12.   return QFont("Courier", 8, QFont::Bold, true);
    13.   }
    14.   }*/
    15. return QIdentityProxyModel::data(proxyIndex, role);
    16. }
    To copy to clipboard, switch view to plain text mode 

    (I want to make the commented "if" to work ->when executed it throws segmentation faul)

  3. #3
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: cant read data from my model of QIdentityProxyModel

    try this
    Qt Code:
    1. QVariant data(const QModelIndex & proxyIndex, int role) const
    2. {
    3. if((role == Qt::BackgroundRole) && (proxyIndex.column() == 1))
    4. {
    5. int value = QIdentityProxyModel::data(index(proxyIndex.row(), 1), Qt::DisplayRole).toInt();
    6. if(value < 6)
    7. return QBrush(Qt::red);
    8. }
    9.  
    10. if((role == Qt::FontRole) && (proxyIndex.column() == 0))
    11. {
    12. int value = QIdentityProxyModel::data(index(proxyIndex.row(), 1), Qt::DisplayRole).toInt();
    13. if(value < 6)
    14. return QFont("Courier", 8, QFont::Bold, true);
    15. }
    16.  
    17. return QIdentityProxyModel::data(proxyIndex, role);
    18. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  4. The following 2 users say thank you to Santosh Reddy for this useful post:

    code.yash (25th July 2014), KillGabio (12th July 2013)

  5. #4
    Join Date
    Jan 2012
    Location
    Argentina
    Posts
    167
    Thanks
    33
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: cant read data from my model of QIdentityProxyModel

    If you were from Argentina, i d be yelling: "GROSOOOOO"

Similar Threads

  1. read data from website
    By sujan.dasmahapatra in forum Qt Programming
    Replies: 4
    Last Post: 27th June 2013, 11:32
  2. Replies: 9
    Last Post: 14th February 2013, 19:39
  3. how to use QIdentityProxyModel?
    By ecanela in forum Qt Programming
    Replies: 4
    Last Post: 3rd August 2012, 20:29
  4. Replies: 1
    Last Post: 24th February 2011, 05:54
  5. can`t read a binary data!
    By blm in forum Qt Programming
    Replies: 8
    Last Post: 18th September 2008, 16:56

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.