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 ??
Printable View
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 ??
For example with a custom delegate.
Inherit QItemDelegate and override drawFocus().
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.
//implementation
Code:
QGrid *table = new QGrid(5,5); //this creates a table for(int i=0;i<5;i++) { for(int j =0;j<5;j++) { 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 ...
Create a new class which inherits QItemDelegate, and override:
At simplest possible, the implementation of drawFocus() could look something like this:Quote:
void QItemDelegate::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.
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.. :)Code:
void GridDelegate::drawFocus(QPainter* painter, const QStyleOptionViewItem &option, const QRect &rect) const { { pen.setWidth(3); painter->setPen(pen); painter->drawRect(rect); } }
Then, for example in the constructor of Grid, you would set the item delegate to the one you created:
Code:
setItemDelegate(new GridDelegate(this));
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.
Code:
//griddelegate.h { public: };
Code:
//griddelegate.cpp #include <QtGui> #include <GridDelegate.h> { } { { painter->setBrush(brsh); } { painter->setBrush(brsh1); } } { { pen.setWidth(3); painter->setPen(pen); painter->drawRect(rect); } else { pen.setWidth(5); painter->setPen(pen); painter->drawRect(rect); } else { 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.
Thanx buddy... i was able to set the background color by using palette.... thanx a lot ...:D
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
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....
Try setting the focus to your table widget during the startup:
Code:
table->setFocus(Qt::ActiveWindowFocusReason);
thanx jpn,
it works now by with setCurrentCell ...:p