How to read the color of an rectitem
-
wrote on 25 Nov 2011, 07:41 last edited by
hi,in my app i have a graphicsscene and lot of rectitems with different colours...i want to read the color of an perticular rectitem ... how to read the color of an rectitem ?
-
wrote on 25 Nov 2011, 08:51 last edited by
QGraphicsRectItem is derived from QGraphicsShapeItem, which as properties called pen and brush. The first is used for the outline of the shape, the second for the fill. Both contain a color.
-
wrote on 25 Nov 2011, 09:23 last edited by
Thanks for the reply..i already done setBresh to the rectitem now my doubt is
@
QGraphicsRectItem *rectItem=new QGraphicsRectItem(20,30,40,50);
rectItem->setBrush( QBrush(Qt::green));
@
i added color like this and then i added this rectitem to the scene...@void View ::mousePressEvent(QMouseEvent* event) {
if (event->button() == Qt::LeftButton){
if (QGraphicsRectItem *item = itemAt(event->pos())) {
//here i have the rectitem i want to find the color of this item
}}
}
@ -
wrote on 25 Nov 2011, 09:48 last edited by
First of all "itemAt":http://doc.qt.nokia.com/latest/qgraphicsview.html#itemAt return QGraphicsItem , so you shoud cast it to your QGraphicsRectItem and check pointer on null.
@
void View ::mousePressEvent(QMouseEvent event) {if (event->button() == Qt::LeftButton){
QGraphicsRectItem *item = static_cast<QGraphicsRectItem *>(itemAt(event->pos()));
if (item) {
QBrush brush = item->brush(); // and so on
//here i have the rectitem i want to find the color of this item
}
}
}
@
Try it, I don't test it. -
wrote on 25 Nov 2011, 12:08 last edited by
it is showing rgb format ...how can it convet it into name format(ex:blue,green)...
-
wrote on 25 Nov 2011, 12:34 last edited by
[quote author="phani438" date="1322222885"]it is showing rgb format ...how can it convet it into name format(ex:blue,green)...
[/quote]Other than building your own table of rgb->name mappings, you can not.
1/6