Results 1 to 6 of 6

Thread: HightLight particular Row in QStandardItemModel QTableView

  1. #1
    Join Date
    Dec 2010
    Posts
    41
    Thanks
    12

    Default HightLight particular Row in QStandardItemModel QTableView

    Hello every one i am working a data representing software where i show some pressure type data in a QTableView.. I have written a function to tell if the pressure data has exceeded the limits which is set by the user in the GUI. But i wanted to highlight the QTableView Cell which has crossed the limit. I could not find out how to hightlight. Can some one pls help me out with this problem i don't know how to hightlight the a particular row data in table view.. The Data in the Table View is refreshed every 3 seconds.

    Qt Code:
    1. for(int kt=0; kt<pressuredata.count(); kt++)
    2. {
    3. if((maxval < pressuredata.at(kt).toFloat())||(minval > pressuredata.at(kt).toFloat()))
    4. {
    5. qLed2_2->setColor(Qt::red);
    6. label_9->setText("Pressure Limit Excceded");
    7. setlimiflag =1;
    8. limitposi = kt;
    9. break;
    10. }
    11.  
    12. else
    13. setlimiflag =0;
    To copy to clipboard, switch view to plain text mode 

    I am thinking the position kt can be used to highlight the particular row in the table view which is out of limits.. Give me some suggestions on how i could go about this. and how should i highlight a row in QTableView. ??

    Regards

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: HightLight particular Row in QStandardItemModel QTableView

    You can set background color for all columns in the row u want.
    Other way is to use a delegate. In delegate you can check for a condition and then paint accordingly

  3. The following user says thank you to aamer4yu for this useful post:

    rex (19th March 2011)

  4. #3
    Join Date
    Nov 2010
    Posts
    20
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: HightLight particular Row in QStandardItemModel QTableView

    Currently, I see two ways of doing this :

    try setting the Qt::BackgroundRole of the designated cell in your model.

    Otherwise, you could try defining your own delegate (QStyledItemDelegate) and overriding the
    Qt Code:
    1. QStyledItemDelegate::paint
    To copy to clipboard, switch view to plain text mode 
    method to highlight your cell :

    Qt Code:
    1. void MyDelegate::::paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
    2. {
    3. int value = index.model()->data().toInt();
    4. if (value > max)
    5. {
    6. painter->fillRect(option.rect, Qt::red);
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    (didn't try the code).

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

    rex (19th March 2011)

  6. #4
    Join Date
    Dec 2010
    Posts
    41
    Thanks
    12

    Default Re: HightLight particular Row in QStandardItemModel QTableView

    thank you so much i will try it Octal. i will read upon Qt::BackgroundRole too.

    regards


    Added after 1 17 minutes:


    Hey i tried looking at the code but din't understand how to use it exactly. . This is what i am to doing after retrieving the data from the db to update the table and show a Red QLed when there is some data out of limit. But i wanted to highlight the cells of the crossed limits. i could't figureout how to use it.
    Qt Code:
    1. void datadisplay::Updatetable()
    2. {
    3. model = new QStandardItemModel();
    4. model->clear();
    5. // Data is appended to pressuredata every second from the db.
    6. for(int i=0;i< pressuredata.count(); i++)
    7. {
    8. QStandardItem *item = new QStandardItem(pressuredata.at(i));
    9. model->setItem(i, 0, item);
    10.  
    11. }
    12. model->setHeaderData(0, Qt::Horizontal, tr("ID"));
    13. model->setHeaderData(1, Qt::Horizontal, tr("Pressure"));
    14. model->setHeaderData(2, Qt::Horizontal, tr("RAW"));
    15.  
    16. tableView->setModel(model);
    17. tableView->setAlternatingRowColors(true);
    18.  
    19.  
    20. for(int kt=0; kt<pressuredata.count(); kt++) //QVector<QString> pressuredata;
    21. {
    22. if((maxval < pressuredata.at(kt).toFloat())||(minval > pressuredata.at(kt).toFloat()))
    23. {
    24. qLed2_2->setColor(Qt::red);
    25. label_9->setText("Pressure Limit Excceded");
    26. setlimiflag =1;
    27. // limtposi = kt;
    28. break;
    29. }
    30.  
    31. else
    32. setlimiflag =0;
    33. }
    To copy to clipboard, switch view to plain text mode 

    can you guide me to how to use the code you suggested .,.

    regards
    Last edited by rex; 19th March 2011 at 12:32.

  7. #5
    Join Date
    Nov 2010
    Posts
    20
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: HightLight particular Row in QStandardItemModel QTableView

    Examine this code :

    Qt Code:
    1. #include "mainwindow.h"
    2.  
    3. #include <QtGui>
    4. #include <QTime>
    5.  
    6. static const int Rows = 5;
    7. static const int Columns = 5;
    8. static const int Max = 55;
    9.  
    10. MainWindow::MainWindow(QWidget *parent)
    11. : QMainWindow(parent)
    12. {
    13. view = new QTableView;
    14. model = new QStandardItemModel(Rows, Columns, this);
    15.  
    16. setCentralWidget(view);
    17. qsrand(QTime::currentTime().msec());
    18. setupModel();
    19. checkLimits();
    20. }
    21.  
    22. MainWindow::~MainWindow()
    23. {
    24.  
    25. }
    26.  
    27. void MainWindow::setupModel()
    28. {
    29. for (int i = 0; i < Rows; ++i)
    30. {
    31. for (int j = 0; j < Columns; ++j)
    32. {
    33. int v = qrand() % 100;
    34. model->setData(model->index(i, j), v);
    35. }
    36. }
    37. view->setModel(model);
    38. }
    39.  
    40. void MainWindow::checkLimits()
    41. {
    42. for (int i = 0; i < model->rowCount(); ++i)
    43. {
    44. for (int j = 0; j < model->columnCount(); ++j)
    45. {
    46. int value = model->item(i, j)->text().toInt();
    47. if (value > Max)
    48. {
    49. model->setData(model->index(i, j), Qt::red, Qt::BackgroundRole);
    50. }
    51. }
    52. }
    53. }
    To copy to clipboard, switch view to plain text mode 

    in setupModel, I'm putting some random datas into a QStandardItemModel. Then, in checkLimits function, I'm checking each cell, and setting the corresponding Qt::BackgroundRole to Qt::red if the value exceeded the limit.

    this is the BackgroundRole approach described above.

  8. The following user says thank you to Octal for this useful post:

    rex (19th March 2011)

  9. #6
    Join Date
    Dec 2010
    Posts
    41
    Thanks
    12

    Default Re: HightLight particular Row in QStandardItemModel QTableView

    Thank you so much for the help Octal.. That solved my problem.. The Cells in which the data is above the limit is highlighted.

    Regards

Similar Threads

  1. QStandardItemModel / QTableView sorting issue
    By AlphaWolfXV in forum Qt Programming
    Replies: 2
    Last Post: 31st August 2010, 05:42
  2. Replies: 1
    Last Post: 30th August 2010, 21:24
  3. Hightlight not lost
    By wirasto in forum Qt Programming
    Replies: 0
    Last Post: 5th August 2010, 13:16
  4. qtableview QStandardItemModel read only
    By JeanC in forum Qt Programming
    Replies: 2
    Last Post: 9th February 2008, 16:42
  5. Extract QStandardItemModel from QTableView
    By patrik08 in forum Qt Programming
    Replies: 1
    Last Post: 16th January 2008, 08:34

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.