I get the wrong color in paint function.
Solved
General and Desktop
-
//ShowImgScene void ShowImgScene::AddRect(QRect &rect, QColor color /*= QColor(Qt::red)*/) { QPen pen; QGraphicsRectItem * pRectItem = new GraphicsRectItem(rect); // GraphicsRectItem pen.setWidth(0); pen.setColor(color); pRectItem->setPen(pen); // at here i set the pen }
I wan to get the color in
GraphicsRectItem::paint
function.//GraphicsRectItem void GraphicsRectItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { QGraphicsRectItem::paint(painter,option,widget); QColor color = painter->pen().color(); // but the color always is black if( option->state & QStyle::State_Selected ){ painter->fillRect(option->rect,color); } }
where is i wrong? and, how to get the color of before set in paint function?
About this problem, i can use the
setData
anddata
. But, i think there must be a better way. -
@joeQ I think you should use "this->pen()" to access the colour of the graphics item. You're accessing the last pen set on the painter instead.
Please try this…
QColor color = pen().color();
(I'm working from memory, apologise if I'm wrong!)
-
Excellent, Your memory is good! Thank u very much.