Hi! I'm using a QTableView and want to color the background of the rows, but i can't figure out how. THANKS FOR YOUR HELP!!
Hi! I'm using a QTableView and want to color the background of the rows, but i can't figure out how. THANKS FOR YOUR HELP!!
take a look at Qt::BackgroundRole, i.e. you need to set data for a needed item,
you can do it (in general way) as:
Qt Code:
... model->setData(index, Qt::red, Qt::BackgroundRole); ...To copy to clipboard, switch view to plain text mode
Qt Assistant -- rocks!
please, use tags [CODE] & [/CODE].
Hi, subclass the your Model (QSqlQuery for example) and reimplement virtual method data, like this:
Qt Code:
{ if (role == Qt::BackgroundColor) { } return (v); }To copy to clipboard, switch view to plain text mode
Marcelo E. Geyer
grub87 (17th June 2009)
here is a compilable example
Qt Code:
#include <QtGui> #include <QApplication> int main(int argc, char **argv) { for (int row = 0; row < table.rowCount(); ++row) { for (int column = 0; column < table.columnCount(); ++column) { table.setItem(row, column, newItem); } } table.show(); return app.exec(); }To copy to clipboard, switch view to plain text mode
Qt Assistant -- rocks!
please, use tags [CODE] & [/CODE].
Hi!! I try to put your code in a cycle, so it will color all the tableView, but it didn't work:
Qt Code:
int row = 0; if (query.isActive()) while(query.next()){ model->setData(index, Qt::red, Qt::BackgroundRole); row++; }To copy to clipboard, switch view to plain text mode
if you use some of QSql*Model then you should approach which estanisgeyer suggested of write your own delegate.
an exmaple
applying a delegate for a tableQt Code:
{ public: public: virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { drawBackground(painter, option, index); } protected: virtual void drawBackground(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { Q_UNUSED(index); } };To copy to clipboard, switch view to plain text mode
but in this case when a table is being resized items colors is being changed to.Qt Code:
... table->setItemDelegate(new ColorDelegate(table)); ...To copy to clipboard, switch view to plain text mode
Qt Assistant -- rocks!
please, use tags [CODE] & [/CODE].
grub87 (17th June 2009), NicholasSmith (6th October 2010)
Excelent!! thanks!! that get me out of trouble, and i'm also changing the colors of rows like this:
Qt Code:
ui.latabladiario->setItemDelegateForRow(0, new ColorDelegate(ui.latabladiario)); ui.latabladiario->setItemDelegateForRow(3, new ColorDelegate(ui.latabladiario));To copy to clipboard, switch view to plain text mode
Last edited by grub87; 17th June 2009 at 17:07.
add a map or hash to your delegate for keeping color by id (or even better add a proper method in a delegate in which you can pass this map/hash). then you have an index (QModelIndex) using it you can get your id from a table, this should look like this
usageQt Code:
{ public: public: virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { drawBackground(painter, option, index); } void setColorMap(const QMap<int, QColor> &colorMap) { m_colorMap = colorMap; } protected: virtual void drawBackground(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { if (index.column() == 1) {//assume that id keeps in a second column const int id = index.data().toInt(); painter->fillRect(option.rect, m_colorMap.value(id)); } } private: QMap<int, QColor> m_colorMap; };To copy to clipboard, switch view to plain text mode
Qt Code:
... ColorDelegate *delegate = new ColorDelegate(&table); QMap<int, QColor> map; for (int i = 0; i < 100; ++i) delegate->setColorMap(map); table->setItemDelegate(delegate); ...To copy to clipboard, switch view to plain text mode
Qt Assistant -- rocks!
please, use tags [CODE] & [/CODE].
There are several ways to do this, see the example below, if the date is less than the current, applying background color of red.
Example:
Qt Code:
{ if ((role == Qt::BackgroundRole) && { } return (v); }To copy to clipboard, switch view to plain text mode
Marcelo E. Geyer
grub87 (17th June 2009)
This is my final code, i only put in a for cycle to move along the rows:
Qt Code:
int polizaInicial = 1; for (int row = 0; row < numRows; row++) { if(index.data().toInt() == polizaInicial){ ui.latabladiario->setItemDelegateForRow(row, new ColorDelegate(ui.latabladiario)); } else{ polizaInicial++; }To copy to clipboard, switch view to plain text mode
THANKS!!
You can also use style sheets for selection:
http://doc.trolltech.com/4.5/stylesh...ing-qtableview
I think background color and color should work as well, but that needs to be tested.
Bookmarks