Results 1 to 4 of 4

Thread: [SOLVED] Get QModelIndex data

  1. #1
    Join Date
    Jun 2015
    Posts
    28
    Qt products
    Qt5
    Platforms
    Unix/X11

    Thumbs up [SOLVED] Get QModelIndex data

    Greetings!

    Currently I have the class below, which paints the lines of column 94 of a QTableView.

    I need to get the values of each line of this column, to check which color it will paint in that row/column

    Qt Code:
    1. void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
    2. const QModelIndex &index) const
    3. {
    4. if (index.column() == 94)
    5. {
    6. if (GetValueOfColumn94 <= 20)
    7. painter->fillRect(option.rect, Qt::green);
    8. else if (GetValueOfColumn94 >=21<=60)
    9. painter->fillRect(option.rect, Qt::yellow);
    10. if (GetValueOfColumn94 >=61<=100)
    11. painter->fillRect(option.rect, Qt::red);
    12. }
    13.  
    14. return QItemDelegate::paint(painter, option, index);
    15. }
    To copy to clipboard, switch view to plain text mode 

    in code above, the "GetValueOfColumn94" is what i need. I've tried with this 2 methods below, but not work

    Qt Code:
    1. index.sibling(index.row(),94).data().toInt();
    2. index.model()->data(index.model()->index(index.row(),94),Qt::DisplayRole).toInt();
    To copy to clipboard, switch view to plain text mode 

    someone could help me?
    Thanks!
    Juliano

    Greetings!

    Currently I have the class below, which paints the lines of column 94 of a QTableView.

    I need to get the values of each line of this column, to check which color it will paint in that row/column

    Qt Code:
    1. void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
    2. const QModelIndex &index) const
    3. {
    4. if (index.column() == 94)
    5. {
    6. if (GetValueOfColumn94 <= 20)
    7. painter->fillRect(option.rect, Qt::green);
    8. else if (GetValueOfColumn94 >=21<=60)
    9. painter->fillRect(option.rect, Qt::yellow);
    10. if (GetValueOfColumn94 >=61<=100)
    11. painter->fillRect(option.rect, Qt::red);
    12. }
    13.  
    14. return QItemDelegate::paint(painter, option, index);
    15. }
    To copy to clipboard, switch view to plain text mode 

    in code above, the "GetValueOfColumn94" is what i need. I've tried with this 2 methods below, but not work

    Qt Code:
    1. index.sibling(index.row(),94).data().toInt();
    2. index.model()->data(index.model()->index(index.row(),94),Qt::DisplayRole).toInt();
    To copy to clipboard, switch view to plain text mode 

    someone could help me?
    Thanks!
    Juliano


    Added after 1 27 minutes:


    Solved!


    Qt Code:
    1. QVariant GetValueOfColumn94 = index.model()->data(index.model()->index(index.row(),94),Qt::DisplayRole);
    2.  
    3. if (GetValueOfColumn94 == "Green")
    4. painter->fillRect(option.rect, QColor(170, 216, 0));
    5. else if (GetValueOfColumn94 == "Yellow")
    6. painter->fillRect(option.rect, QColor(254, 251, 24));
    7. if (GetValueOfColumn94 == "Red")
    8. painter->fillRect(option.rect, QColor(255, 40, 0));
    To copy to clipboard, switch view to plain text mode 

    Thanks!
    Juliano
    Last edited by juliano.gomes; 20th November 2015 at 20:18.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: [SOLVED] Get QModelIndex data

    Or just
    Qt Code:
    1. index.data()
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  3. #3
    Join Date
    Jun 2015
    Posts
    28
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: [SOLVED] Get QModelIndex data

    Quote Originally Posted by anda_skoa View Post
    Or just
    Qt Code:
    1. index.data()
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _
    Yes anda_skoa, much more simple than i've thought.

    changing the subject a little, if I need to paint a fraction of the cell, instead of painting it all, as in the last column of the picture attached, could I do this this without a lot of work?
    paint.png

    hugs!
    Juliano

    Yes anda_skoa, much more simple than i've thought.

    changing the subject a little, if I need to paint a fraction of the cell, instead of painting it all, as in the last column of the picture attached, could I do this this without a lot of work?
    paint.png

    hugs!
    Juliano

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: [SOLVED] Get QModelIndex data

    You can do that with a custom delegate.
    E.g. derive from QStyledItemDelegate, implement paint() such that it calls the base class for the usual painting and paints your customization either before or after that.

    Cheers,
    _

Similar Threads

  1. QModelIndex question?
    By QXiaoWang in forum Qt Programming
    Replies: 1
    Last Post: 19th October 2010, 09:47
  2. getting data & QModelIndex
    By alexandernst in forum Newbie
    Replies: 3
    Last Post: 6th August 2009, 20:43
  3. Replies: 6
    Last Post: 6th August 2009, 18:18
  4. QModelIndex problem!
    By landonmkelsey in forum Qt Programming
    Replies: 10
    Last Post: 28th August 2008, 20:46
  5. Replies: 3
    Last Post: 7th May 2006, 18:07

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.