Results 1 to 4 of 4

Thread: Creating static const QColors

  1. #1
    Join Date
    Jan 2006
    Location
    Ohio
    Posts
    332
    Thanks
    37
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Question Creating static const QColors

    I have a class where I declare some public static const QColors in my .h file:

    Qt Code:
    1. class myClass
    2. {
    3. public:
    4. ...
    5. static const QColor GREEN;
    6. static const QColor BLUE;
    7. static const QColor RED;
    8. static const QColor ORANGE;
    9. ...
    10. }
    To copy to clipboard, switch view to plain text mode 

    Then at the top of my .cpp file, I have:
    Qt Code:
    1. const QColor myClass::GREEN = QColor(0, 204, 0);
    2. const QColor myClass::BLUE = QColor(0, 102, 255);
    3. const QColor myClass::RED = QColor( 255, 0, 0);
    4. const QColor myClass::ORANGE = QColor(255, 153, 0);
    To copy to clipboard, switch view to plain text mode 

    Later, in a function inside myClass, when I try to say:

    Qt Code:
    1. QColor newColor = myClass::GREEN; // Also tried QColor newColor = GREEN;
    2. qDebug( ) << newColor;
    To copy to clipboard, switch view to plain text mode 

    It prints out that newColor is "QColor(Invalid)"

    What is wrong with declaring these QColors this way?

  2. #2
    Join Date
    Jan 2006
    Location
    Ohio
    Posts
    332
    Thanks
    37
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Creating static const QColors

    Forgot to mention this is running Qt 4.8.5 since I am required to still provide support for Solaris 10. I am currently running into this problem on Windows 7 however.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Creating static const QColors

    This works as expected:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class Class {
    4. public:
    5. static const QColor GREEN;
    6. };
    7.  
    8. const QColor Class::GREEN = QColor(0, 255, 0);
    9.  
    10. int main(int argc, char **argv) {
    11. QApplication app(argc, argv);
    12. QColor test = Class::GREEN;
    13. qDebug() << test << Class::GREEN;
    14. return 0;
    15. }
    To copy to clipboard, switch view to plain text mode 
    whether Class in defined in main.cpp or separate files.

    Where is this code in your program?
    Qt Code:
    1. QColor newColor = myClass::GREEN; // Also tried QColor newColor = GREEN;
    2. qDebug( ) << newColor;
    To copy to clipboard, switch view to plain text mode 
    In main(), some non-static function, a static function, or the constructor of a class that you are creating static instances of? You may be falling foul of static initialisation order: http://www.parashift.com/c++-faq/static-init-order.html

  4. The following user says thank you to ChrisW67 for this useful post:

    ToddAtWSU (24th December 2013)

  5. #4
    Join Date
    Jan 2006
    Location
    Ohio
    Posts
    332
    Thanks
    37
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default [SOLVED] Re: Creating static const QColors

    Quote Originally Posted by ChrisW67 View Post
    In main(), some non-static function, a static function, or the constructor of a class that you are creating static instances of? You may be falling foul of static initialisation order: http://www.parashift.com/c++-faq/static-init-order.html
    Thanks a bunch. I had a couple static const class objects calling the constructor but they were declared prior to my static const QColors being called which your link explained to me right away that those probably had not been initialized yet. Moving the QColor declarations to the top fixed my issue! Thanks again!
    Last edited by ToddAtWSU; 24th December 2013 at 12:44. Reason: Title Edit

Similar Threads

  1. Replies: 4
    Last Post: 9th May 2016, 12:30
  2. Side effect to const static variable - do not know how
    By Prototyp144 in forum General Programming
    Replies: 0
    Last Post: 4th June 2013, 14:25
  3. Replies: 1
    Last Post: 23rd September 2010, 17:12
  4. Creating Static library with QT
    By QPlace in forum Qt Programming
    Replies: 2
    Last Post: 21st December 2008, 04:50
  5. typedef static const QString Tag;
    By sunil.thaha in forum General Programming
    Replies: 13
    Last Post: 21st January 2006, 17:05

Tags for this Thread

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.