Results 1 to 10 of 10

Thread: A library for pens and brushes not working

  1. #1
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default A library for pens and brushes not working

    Hello,

    I find myself creating a lot of QBrush and QPen objects when I draw things on the screen or create items for a graphics scene. I decided to create a class for easy access to QBrushes like so:

    Qt Code:
    1. class ColorUtil
    2. {
    3. public:
    4. ColorUtil();
    5. ~ColorUtil(){}
    6.  
    7. QBrush brushYellow;
    8. QBrush brushRed;
    9. QBrush brushBlue;
    10. };
    11.  
    12. ColorUtil::ColorUtil()
    13. {
    14. brushYellow.setColor(QColor::fromRgb(255,255,0,125));
    15. brushRed.setColor(QColor::fromRgb(255,125,125,255));
    16. brushBlue.setColor(QColor::fromRgb(0,0,255,125));
    17. }
    To copy to clipboard, switch view to plain text mode 

    Now when I try to use the brushes from the ColorUtil class above for example in the constructor of a graphics scene:

    Qt Code:
    1. class PocScene : public QGraphicsScene
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6.  
    7.  
    8. public:
    9. PocScene(QObject *parent = 0);
    10. ~PocScene(){}
    11. };
    12.  
    13. PocScene::PocScene(QObject *parent) : QGraphicsScene(parent)
    14. {
    15. setSceneRect(-1000, -1000, 2000, 2000);
    16.  
    17. double wheelRadius = 0.05;
    18. double carHeight = 0.15;
    19. double carWidth = 0.3;
    20.  
    21. QPen pen;
    22. pen.setCosmetic(true);
    23. pen.setWidth(2);
    24.  
    25. QBrush yellowBrush(QColor::fromRgb(255,255,0,125));
    26. QBrush redBrush(QColor::fromRgb(255,125,125,255));
    27. QBrush blueBrush(QColor::fromRgb(0,0,255,125));
    28.  
    29. ColorUtil colorUtil;
    30.  
    31. car = addRect(-0.5*carWidth, -0.5*carHeight, carWidth, carHeight, pen, yellowBrush);
    32. lWheel = addEllipse(-wheelRadius, -wheelRadius, 2.0*wheelRadius, 2.0*wheelRadius, pen, colorUtil.brushRed); // ColorUtil used here!
    33. rWheel = addEllipse(-wheelRadius, -wheelRadius, 2.0*wheelRadius, 2.0*wheelRadius, pen, blueBrush);
    34. }
    To copy to clipboard, switch view to plain text mode 

    ...it turns out that it does not work and the left wheel of the car is not colored while the right one is. I don't understand why this does not work. Printing of color values inside the ColorUtil class shows me that the QBrushes have been instantiated correctly, so what is the difference between the QBrushes inside the ColorUtil class and the locally instantiated QBrush objects that do work?

  2. #2
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: A library for pens and brushes not working

    I am not sure of exact problem but

    Qt Code:
    1. lWheel = addEllipse(-wheelRadius, -wheelRadius, 2.0*wheelRadius, 2.0*wheelRadius, pen, colorUtil.brushRed); // ColorUtil used here!
    2. rWheel = addEllipse(-wheelRadius, -wheelRadius, 2.0*wheelRadius, 2.0*wheelRadius, pen, blueBrush);
    To copy to clipboard, switch view to plain text mode 

    If you are using these two lines, they will over lap as you are using same geometry for both wheels & you would see only 2nd wheel with overlapping color.

    But if you are using them one at a time & still you have a problem, I am not sure of what is the wrong.
    Thanks :-)

  3. #3
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: A library for pens and brushes not working

    Oh, when trying to make the code as simple as possible I cut away these lines:

    Qt Code:
    1. lWheel->setParentItem(car);
    2. lWheel->setPos(car->rect().x() + 0.5*lWheel->rect().width(), car->rect().y() - 0.5*lWheel->rect().height());
    3.  
    4. rWheel->setParentItem(car);
    5. rWheel->setPos(car->rect().x() + car->rect().width() - 0.5*rWheel->rect().width(), car->rect().y() - 0.5*rWheel->rect().height());
    To copy to clipboard, switch view to plain text mode 

    The wheels don't overlap and the car looks nice. However, the left wheel is not colored and the right wheel is and I don't understand why.

  4. #4
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: A library for pens and brushes not working

    hmm,
    I could see that with the following code

    Qt Code:
    1. PocScene::PocScene(QObject *parent) : QGraphicsScene(parent)
    2. {
    3. setSceneRect(-1000, -1000, 2000, 2000);
    4.  
    5. ColorUtil colorUtil;
    6. addRect(0,0,20,20, QPen(Qt::red), QBrush(Qt::yellow));
    7. addRect(20,20,40,40, QPen(Qt::red), colorUtil.brushYellow);
    8. }
    To copy to clipboard, switch view to plain text mode 

    Sorry for the spamming but I am now waiting for some one to answer this.
    Thanks :-)

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: A library for pens and brushes not working

    The difference is in how you are constructing the brushes.

    In your ColorUtil class, you are using the default QBrush constructor, which sets the fill style to Qt:: NoBrush. Calling setColor() after construction simply tells the QBrush to set its color, but it still has the NoBrush style. In the case of your local QBrush variables, you are calling the QBrush( const QColor & ) constructor, which has a default second argument of the brush style that in this case defaults to Qt:: SolidPattern. Changing your ColorUtil constructor to this will solve the problem:

    Qt Code:
    1. ColorUtil::ColorUtil()
    2. : brushYellow(QColor::fromRgb(255,255,0,125))
    3. , brushRed(QColor::fromRgb(255,125,125,255))
    4. , brushBlue.setColor(QColor::fromRgb(0,0,255,125))
    5. {
    6. }
    To copy to clipboard, switch view to plain text mode 

    This class is sort of a waste of time anyway - I don't know what you are trying to optimize, because the object instances you are setting the brushes on are making a copy of whatever brush you pass in, so either QBrush:: operator=() or the QBrush copy constructor is being called in any case. Each time you create a ColorUtil instance on the stack, you are in fact creating three QBrush instances, not one, but you are using only one of the three instances, and making a copy of it at that.

    If you want to continue with this approach, then the way to optimize it is to make the ColorUtil class a singleton (only one of them exists in the program) created on the heap (not stack) so that you only construct your brush or pen instances once and you simply make copies of them as needed.

    Qt Code:
    1. // ColorUtil.h
    2.  
    3. class ColorUtil
    4. {
    5. public:
    6. static ColorUtil * instance()
    7. {
    8. if ( !sColorUtil )
    9. sColorUtil = new ColorUtil;
    10. return sColorUtil;
    11. }
    12.  
    13. // You should never have public member variables, but if you're going to go down that unhappy road, here it is:
    14. QBrush yellowBrush;
    15. QBrush redBrush;
    16. QBrush whateverBrush;
    17.  
    18. private:
    19. ColorUtil();
    20. ColorUtil( const ColorUtil & ) {}
    21. ~ColorUtil() {}
    22.  
    23. static ColorUtil * sColorUtil;
    24. }
    25.  
    26. // in ColorUtil.cpp:
    27. ColorUtil * ColorUtil::sColorUtil = 0;
    28.  
    29. ColorUtil::ColorUtil()
    30. : brushYellow(QColor::fromRgb(255,255,0,125))
    31. , brushRed(QColor::fromRgb(255,125,125,255))
    32. , brushBlue.setColor(QColor::fromRgb(0,0,255,125))
    33. {
    34. }
    35.  
    36. // OtherCode.cpp
    37. // You use this as follows:
    38.  
    39. ColorUtil * pCU = ColorUtil::instance();
    40. whatever->setBrush( pCU->yellowBrush );
    To copy to clipboard, switch view to plain text mode 
    Last edited by d_stranz; 7th October 2015 at 04:58. Reason: Added singleton code

  6. The following 2 users say thank you to d_stranz for this useful post:

    Cruz (7th October 2015), prasad_N (7th October 2015)

  7. #6
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: A library for pens and brushes not working

    Hi!

    First of all, thank you d_stranz for solving my problem.

    What I am trying to optimize is the simplicity of my drawing code. For a while now I grew tired of manually assigning colors to pens and brushes, so I ended up dragging a large block of preconstructed pens and brushes with me, such as

    Qt Code:
    1. QBrush yellowBrush(QColor::fromRgb(255,255,0,125));
    2. QBrush redBrush(QColor::fromRgb(255,125,125,255));
    3. QBrush blueBrush(QColor::fromRgb(0,0,255,125));
    To copy to clipboard, switch view to plain text mode 

    so that later on in the code I can simply refer to

    Qt Code:
    1. whatever.setBrush(yellowBrush);
    To copy to clipboard, switch view to plain text mode 

    And now I moved this block of preconstructed drawing tools (I picked up the habit of copy pasitng it between projects) into a separate ColorUtil object, that apart from readily providing pens and brushes in my favorite colors, can do some other nifty color related things too.

    I am a strong believer in code simplicity and thus I will happily invest a few microseconds at construction time to construct several copies of even never used objects, if this makes the rest of my code so much easier to shape. I am also willing to break the holy commandments of OO programming, such as don't use public access variables. To shock you entirely, I have made the ColorUtil a globally accessible object like so:

    Qt Code:
    1. struct ColorUtil
    2. {
    3. ColorUtil();
    4. ~ColorUtil(){}
    5.  
    6. QBrush brushYellow;
    7. QBrush brushRed;
    8. QBrush brushBlue;
    9. };
    10.  
    11. extern ColorUtil colorUtil;
    To copy to clipboard, switch view to plain text mode 

    such that now anywhere I feel the need to draw, after including the ColorUtil class, I can go ahead and just

    Qt Code:
    1. whatever.setBrush(colorUtil.brushYellow);
    To copy to clipboard, switch view to plain text mode 

    I have been using this programming paradigm now for many many years, and the prophecies of the universe ending when using this construct simply failed to come true. On the other hand, I have avoided amassing pointless setters and getters, and enjoyed simple, IDE aided code use.

    Thank you again for pointing out the NoBrush problem, you have helped me on more than one occasions in the past.

    Cruz

  8. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: A library for pens and brushes not working

    I have been using this programming paradigm now for many many years, and the prophecies of the universe ending when using this construct simply failed to come true.
    Yet...

    All well and good if you are writing code for your own use. I have been doing library design and implementation for many years now, both for in-house use as well as for external licensing, and trust me, if there is a way for someone to misuse a class, they will. There is often as much code associated with handling conditions that can't possibly happen as there is in implementing the actual use cases.

    Jack might understand that ColorUtil::yellowBrush is supposed to be read-only, but Fred is working on a different part of the program and doesn't like the shade yellowBrush uses, so he changes it to his liking. Now Jack suddenly finds all of his yellow things have changed color, and he doesn't have a clue why.

    Glad I could be of help.
    Last edited by d_stranz; 7th October 2015 at 17:50.

  9. #8
    Join Date
    Oct 2014
    Posts
    81
    Thanks
    20
    Thanked 9 Times in 9 Posts
    Qt products
    Qt5
    Platforms
    Windows
    Wiki edits
    7

    Default Re: A library for pens and brushes not working

    If you don't plan to change the brush colours after they are constructed, you can declare all those members as 'const QBrush.'
    It sends a stronger message that they're read-only.

  10. #9
    Join Date
    Oct 2015
    Posts
    7
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: A library for pens and brushes not working

    Quote Originally Posted by Cruz View Post
    Oh, when trying to make the code as simple as possible I cut away these lines:

    Qt Code:
    1. lWheel->setParentItem(car);
    2. lWheel->setPos(car->rect().x() + 0.5*lWheel->rect().width(), car->rect().y() - 0.5*lWheel->rect().height());
    3.  
    4. rWheel->setParentItem(car);
    5. rWheel->setPos(car->rect().x() + car->rect().width() - 0.5*rWheel->rect().width(), car->rect().y() - 0.5*rWheel->rect().height());
    To copy to clipboard, switch view to plain text mode 

    The wheels don't overlap and the car looks nice. However, the left wheel is not colored and the right wheel is and I don't understand why.
    i agree on this

  11. #10
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: A library for pens and brushes not working

    i agree on this
    Agree on what? That the wheels don't overlap and the car looks nice? Or that the left wheel is not colored and the right wheel is? Or that you don't understand why?

Similar Threads

  1. Qt Creator Working Qt Creator + Qt library
    By been_1990 in forum Qt Tools
    Replies: 1
    Last Post: 7th March 2013, 16:26
  2. Working with a static library I have written in Qt
    By Guilo in forum Qt Programming
    Replies: 3
    Last Post: 12th November 2011, 13:13
  3. QT Library stop working on all applications
    By ikeban in forum Qt Programming
    Replies: 2
    Last Post: 21st January 2011, 08:25
  4. Qt Creator Breakpoings in my shared library project not working
    By Lendrick in forum Qt Tools
    Replies: 1
    Last Post: 26th September 2010, 18:31
  5. Brushes and window/viewport transformations
    By blukske in forum Qt Programming
    Replies: 1
    Last Post: 15th May 2006, 13:59

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.