PDA

View Full Version : Highlighting the border of cell in Table



ankurjain
14th March 2006, 07:20
Hi all,

i want to highlight the boundry of a cell in a table instead of changing the background, same as it happens in excel.

how can i do it ??

jpn
14th March 2006, 08:25
For example with a custom delegate.
Inherit QItemDelegate and override drawFocus().

ankurjain
16th March 2006, 06:57
can u explain a bit more ... i m new to qt and not able to get how to do it.

i made a new class QGrid inherited QTablewidget in it.

now how to get the individual cell dimension in it.

rect is a inherited function of QTableWidget, but not of QTableWidgetItem, so how to get the dim. of cell.

//class declaration.


class QGrid : public QTableWidget
{
public:
QGrid(int x,int y,QWidget *parent = 0);
}

//implementation


QGrid *table = new QGrid(5,5); //this creates a table

for(int i=0;i<5;i++)
{
for(int j =0;j<5;j++)
{
QTableWidgetItem *t = new QTableWidgetItem;
t->setText("");
table->setItem(j,i,t);
}
}

where can i get the cell dimensions to make the boundry darker if selected.

if i m going wrong .... please correct it ...

jpn
16th March 2006, 08:39
Create a new class which inherits QItemDelegate, and override:

void QItemDelegate::drawFocus (http://doc.trolltech.com/4.1/qitemdelegate.html#drawFocus) ( QPainter * painter, const QStyleOptionViewItem & option, const QRect & rect ) const [virtual protected]
Renders the region within the rectangle specified by rect, indicating that it has the focus, using the given painter and style option.
At simplest possible, the implementation of drawFocus() could look something like this:


void GridDelegate::drawFocus(QPainter* painter, const QStyleOptionViewItem &option, const QRect &rect) const
{
if (option.state & QStyle::State_HasFocus)
{
QPen pen(Qt::black);
pen.setWidth(3);
painter->setPen(pen);
painter->drawRect(rect);
}
}

Note: above code snippet is just a dummy example for you to get started. I suggest you to take a quick look at QItemDelegate sources, there you will get a neat reference from.. :)

Then, for example in the constructor of Grid, you would set the item delegate to the one you created:


setItemDelegate(new GridDelegate(this));

ankurjain
18th March 2006, 10:31
hi jpn,
today i m finally done with the border highlighting part. Thanks a lot for help.

But there is one more thing that is happening. i want that only the border is highlighted and not the background turns blue. i used the paint function and overrided it in my class's .cpp file. now one problem occurs.
the paint function paints the table created to white and not the color i selected. also it suppresses the effect of the drawFocus function and no focus is seen in the table.

i tried using palette in the table but can't make it work.

can u suggest somethng in that.

my code is as follows.



//griddelegate.h
class GridDelegate : public QItemDelegate
{

public:

GridDelegate(QObject *parent = 0);

void drawFocus(QPainter* painter,
const QStyleOptionViewItem &option,
const QRect &rect) const;

void paint(QPainter * painter,
const QStyleOptionViewItem & option,
const QModelIndex & index ) const;


};


//griddelegate.cpp

#include <QtGui>
#include <GridDelegate.h>

GridDelegate::GridDelegate(QObject *parent) : QItemDelegate(parent)
{

}

void GridDelegate::paint(QPainter * painter,
const QStyleOptionViewItem & option,
const QModelIndex & index ) const
{

if (option.state & QStyle::State_Selected)
{
QBrush brsh(Qt::red);
painter->setBrush(brsh);
}
else if (option.state & QStyle::State_HasFocus)
{
QBrush brsh1(Qt::red);
painter->setBrush(brsh1);
}

}

void GridDelegate::drawFocus(QPainter* painter,
const QStyleOptionViewItem &option,
const QRect &rect) const

{
if (option.state & QStyle::State_HasFocus)

{

QPen pen(Qt::black);

pen.setWidth(3);

painter->setPen(pen);

painter->drawRect(rect);

} else
if (option.state & QStyle::State_Active)

{

QPen pen(Qt::green);

pen.setWidth(5);

painter->setPen(pen);

painter->drawRect(rect);

} else
if (option.state & QStyle::State_KeyboardFocusChange)

{

QPen pen(Qt::black);

pen.setWidth(5);

painter->setPen(pen);

painter->drawRect(rect);

}
}



in the constructor of my main program, i merely call the setItemDelegate function as suggested by u.

ankurjain
18th March 2006, 11:01
Thanx buddy... i was able to set the background color by using palette.... thanx a lot ...:D

ankurjain
20th March 2006, 06:19
one more problem jpn,

i want to set one cell, say (0,0), as default if the form opens. i was able to set focus on the cell by function, setCurrentCell(0,0), but not able to draw that default focus (thicken the boundry) how can it be done ??

http://img99.imageshack.us/img99/1673/temp3dv.th.jpg (http://img99.imageshack.us/my.php?image=temp3dv.jpg)


Right now the window looks like above ... as i select the events in the left pane .... the table in the right pane opens up with the cell (0,0) selected ... it is indicated by the blue color.... as i had changed the palette ... now when i want the boundry of the selected cell must also get thick, as it is the behaviour i had made on click of mouse or selection by keyboard.

please help....

jpn
20th March 2006, 10:00
Try setting the focus to your table widget during the startup:

table->setFocus(Qt::ActiveWindowFocusReason);

ankurjain
21st March 2006, 09:20
thanx jpn,
it works now by with setCurrentCell ...:p