I've created a rectangle (QGraphicsRectItem) for use in a QGraphicsScene. I want to change the colour of the shape (say, a blue pen) while leaving all other fields in their default state. I've been able to do this by creating a new QPen and setting it to the one use by the rectangle, like so:

Qt Code:
  1. ...
  2. myScene = new QGraphicsScene(this);
  3. myScene->setSceneRect(0, 0, 500.0, 500.0 ); //Set the scene's outline
  4.  
  5.  
  6. QPen *myPen = new QPen(Qt::blue); //Create a new, blue pen
  7. myRect->setPen(*myPen); //Set the rectangle's pen
  8.  
  9. myRect->setRect(100, 100, 100, 100); //Set rectangle size and position
  10. graphScene->addItem(pixel); //Add the rectangle to the scene
  11. ...
To copy to clipboard, switch view to plain text mode 

Probably a really basic question, but what is wrong with trying to set the colour with the following line?

Qt Code:
  1. myRect->pen().setColor(Qt::blue);
To copy to clipboard, switch view to plain text mode 

Do I not have access to the "setColor" function in the rectangle's pen?
Am I accessing it in an incorrect manner?
If I place that line in the above code instead of lines 7 & 8, it throws no errors/warnings, but it does nothing (the colour of the rectangle doesn't change).

If I only want to change the pen's colour, it seems weird to me (someone inexperienced with OO programming) to be required to create a new pen and assign it to the rectangle rather than just changing the color in the existing default pen created when the rectangle is created, which is why I thought I'd ask and see if there is an easier way.