Results 1 to 5 of 5

Thread: Setting a QAbstractGraphicsShapeItem's pen fields

  1. #1
    Join Date
    Aug 2010
    Posts
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Setting a QAbstractGraphicsShapeItem's pen fields

    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.

  2. #2
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Setting a QAbstractGraphicsShapeItem's pen fields

    http://www.qtcentre.org/threads/2626...pen().setColor

    The QPen object returned is const.

  3. #3
    Join Date
    Aug 2010
    Posts
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Setting a QAbstractGraphicsShapeItem's pen fields

    Ah OK. Somehow I missed that.
    So does that mean that basically the only way to set, say, the pen's colour to red is with the following statement?

    Qt Code:
    1. myRect->setPen(QPen(Qt::red));
    To copy to clipboard, switch view to plain text mode 
    Last edited by dohzer; 14th August 2010 at 14:19. Reason: Accidentally wrote "Brush" instead of "Pen"

  4. #4
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Setting a QAbstractGraphicsShapeItem's pen fields

    Quote Originally Posted by dohzer View Post
    Ah OK. Somehow I missed that.
    Indeed. I would say that *that* is the bigger problem, i.e., Qt apparently (?) didn't tell you that you were attempting to change a const variable.

  5. #5
    Join Date
    Aug 2010
    Posts
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Setting a QAbstractGraphicsShapeItem's pen fields

    Quote Originally Posted by Urthas View Post
    Qt apparently (?) didn't tell you that you were attempting to change a const variable.
    Well there's no warning during compilation, or error while running, but the pen colour doesn't change, so I'm not sure exactly what's going on.

Similar Threads

  1. automatically complete fields on the form
    By TomASS in forum General Programming
    Replies: 1
    Last Post: 13th July 2010, 19:14
  2. Trouble with QAbstractGraphicsShapeItem
    By lni in forum Qt Programming
    Replies: 0
    Last Post: 21st February 2010, 03:36
  3. QWizard - mandatory fields
    By slava in forum Qt Programming
    Replies: 12
    Last Post: 12th February 2008, 13:52
  4. QTableView showing empty fields
    By ederbs in forum Qt Programming
    Replies: 5
    Last Post: 4th October 2007, 00:30
  5. tr() and static fields
    By drkbkr in forum Qt Programming
    Replies: 6
    Last Post: 5th June 2007, 16:01

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.