PDA

View Full Version : HightLight particular Row in QStandardItemModel QTableView



rex
19th March 2011, 10:29
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.



for(int kt=0; kt<pressuredata.count(); kt++)
{
if((maxval < pressuredata.at(kt).toFloat())||(minval > pressuredata.at(kt).toFloat()))
{
qLed2_2->setColor(Qt::red);
label_9->setText("Pressure Limit Excceded");
setlimiflag =1;
limitposi = kt;
break;
}

else
setlimiflag =0;

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

aamer4yu
19th March 2011, 10:31
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

Octal
19th March 2011, 10:38
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
QStyledItemDelegate::paint method to highlight your cell :



void MyDelegate::::paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
int value = index.model()->data().toInt();
if (value > max)
{
painter->fillRect(option.rect, Qt::red);
}
}


(didn't try the code).

rex
19th March 2011, 12:25
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.


void datadisplay::Updatetable()
{
QStandardItemModel *model ;
model = new QStandardItemModel();
model->clear();
// Data is appended to pressuredata every second from the db.
for(int i=0;i< pressuredata.count(); i++)
{
QStandardItem *item = new QStandardItem(pressuredata.at(i));
model->setItem(i, 0, item);

}
model->setHeaderData(0, Qt::Horizontal, tr("ID"));
model->setHeaderData(1, Qt::Horizontal, tr("Pressure"));
model->setHeaderData(2, Qt::Horizontal, tr("RAW"));

tableView->setModel(model);
tableView->setAlternatingRowColors(true);


for(int kt=0; kt<pressuredata.count(); kt++) //QVector<QString> pressuredata;
{
if((maxval < pressuredata.at(kt).toFloat())||(minval > pressuredata.at(kt).toFloat()))
{
qLed2_2->setColor(Qt::red);
label_9->setText("Pressure Limit Excceded");
setlimiflag =1;
// limtposi = kt;
break;
}

else
setlimiflag =0;
}

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

regards

Octal
19th March 2011, 12:54
Examine this code :



#include "mainwindow.h"

#include <QtGui>
#include <QTime>

static const int Rows = 5;
static const int Columns = 5;
static const int Max = 55;

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
view = new QTableView;
model = new QStandardItemModel(Rows, Columns, this);

setCentralWidget(view);
qsrand(QTime::currentTime().msec());
setupModel();
checkLimits();
}

MainWindow::~MainWindow()
{

}

void MainWindow::setupModel()
{
for (int i = 0; i < Rows; ++i)
{
for (int j = 0; j < Columns; ++j)
{
int v = qrand() % 100;
model->setData(model->index(i, j), v);
}
}
view->setModel(model);
}

void MainWindow::checkLimits()
{
for (int i = 0; i < model->rowCount(); ++i)
{
for (int j = 0; j < model->columnCount(); ++j)
{
int value = model->item(i, j)->text().toInt();
if (value > Max)
{
model->setData(model->index(i, j), Qt::red, Qt::BackgroundRole);
}
}
}
}



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.

rex
19th March 2011, 16:44
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