May 28, 2011

vinb vinb
Lab Rat
205 posts

static cast other then int, char and boolean not possible?

 

Hi all,
I recon this might be a stupid question, but ok.

I want to have an qicon to be cast static in a class, because i will never change its value.

When i try:

  1.     static const int x = 1;   //everything is working as expected here.
  2.     //static const QIcon fileicon = ":/icon/pic4"; //doesnt work.
  3.     //static const QIcon fileicon(":/icon/pic4"); //doesnt work.
  4.     static const char y = 'd';  //everything working as expected.
  5.    

I understand this is standard knowledge, but i clearly dont have that and cant find it. So mayby someone can post a link with the info im looking for?

6 replies

May 28, 2011

leon.anavi leon.anavi
Area 51 Engineer
1217 posts

Try to define it outside the class body. It should be something like:

  1. class Example
  2. {
  3. private:
  4.     static const int x = 1;   //everything is working as expected here.
  5.     static const QIcon fileicon;
  6.     static const char y = 'd';  //everything working as expected.
  7. //..
  8. };
  9.  
  10. const QIcon Example::fileicon(":/icon/pic4");

P.S.
This is a pseudo code and I have tried it myself.

 Signature 

http://anavi.org/

May 28, 2011

vinb vinb
Lab Rat
205 posts

Hi leon.anavi,
thanks for your reply.
I tried your suggestion to define outside the class, but i was not able to get it working.
For me its not really needed to get it working, but i just want to learn why its not possible with an icon but without problem with an int.

May 28, 2011

Gerolf Gerolf
Robot Herder
3287 posts

What exactly does not work?
Dioes the compiler complain about it? does it get no initial value?

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

May 29, 2011

Volker Volker
Ant Farmer
5428 posts

You cannot assign a const char * (= “:/icon/pic4”) to a QIcon object, there is no operator overload. Use proper object initialization syntax:

  1. // this works
  2. static QIcon myIcon(":/abc.png");
  3.  
  4. // this gives an error
  5. static QIcon myFalseIcon = ":/def.png";

EDIT:
Oh, I jut saw that you tried the object initializer too. This is tricky! The initialization order of static global objects is not defined! It varies from compiler to compiler and it can be that the object is created before the Qt resource system is setup, thus leading to an empty icon!

May 29, 2011

vinb vinb
Lab Rat
205 posts

Hi Gerolf and Volker,
Thanks both for your reply.
Sorry for reacting so late, but i was doing things wich i shouldnt have done :)

Gerolf: Yes my compiler complains in the way it dont compile.
At the end of this post i show you what the output is.

Volker:
Your line 2 gives me an error.

Volker wrote:

EDIT:
Oh, I jut saw that you tried the object initializer too. This is tricky! The initialization order of static global objects is not defined! It varies from compiler to compiler and it can be that the object is created before the Qt resource system is setup, thus leading to an empty icon!

Do you mean that i cannot use a resource file item in a class, because its creation order is not clear/defined? So i have to use the complete path?

To be clear i want to define a static QIcon in a class.
If i write:

  1. static QIcon myIcon(":/icon/pic4"); //like Volker suggested,
  2. //Gives error../TestAll/testlistview.h:20: error: expected identifier before string constant
  3. //                  ../TestAll/testlistview.h:20: error: expected ‘,’ or ‘...’ before string constant
  4.  
  5. in class:
  6. static QIcon myIcon;
  7. in constructor:
  8. myIcon(":/icon/pic4");
  9. //Gives error: ../TestAll/testlistview.cpp:14: error: no match for call to ‘(QIcon) (const char [12])’
  10. myIcon.addfile(":/icon/pic4");.
  11. //Gives error: /home/koster/QtApps/TestAll-build/../TestAll/testlistview.cpp:14: undefined reference to `testlistview::fileIcon'
  12. //home/koster/QtApps/TestAll-build/../TestAll/testlistview.cpp:17: undefined reference to `testlistview::fileIcon'
  13.  
  14. If i try leon.anavi 's suggestion:
  15. in class:
  16. static QIcon myIcon;
  17. somewhere in .cpp file
  18. QIcon testlistview::myIcon(":/icon/pic4");
  19. Gives a crash with the words:
  20. //QPixmap: Must construct a QApplication before a QPaintDevice
  21. //The program has unexpectedly finished.
  22. //Is this what you meant Volker?

I hope this output makes it clearer for you, for me it didnt.

May 29, 2011

Volker Volker
Ant Farmer
5428 posts

Can you provide the complete file that produces the error, please. It’s hard to analyze without knowing the details.

The second try is just leon’s without acutally defining the variable in a .cpp file. So it’s failing at compile time.

The third approach (leon’s suggestion) is trapped by the undefined initialization order of static members (the QIcon is initializize – via a QPixmap – before QApplication has started), that’s the reason for the bail out.

I would suggest to make a private static pointer variable; initialize this to null (that works always), and have a static const method return the dereferenced pointer (and initialize it once).

The following sample code is not tested, but should give you a clue how it works:

———— myiconproviderclass.h ————
class QIcon;

  1. class MyIconProviderClass {
  2.     public:
  3.         // your other public stuff goes here
  4.         static const QIcon &myIcon();
  5.  
  6.     private:
  7.         static QIcon *myIconPtr;

———— myiconproviderclass.cpp ————

  1. #include "myiconproviderclass.h"
  2. #include <QIcon>
  3.  
  4. QIcon* MyIconProviderClass::myIconPtr = 0;
  5.  
  6. const QIcon& MyIconProviderClass::myIcon()
  7. {
  8.     if(!myIconPtr)
  9.         myIconPtr = new QIcon(":/path/to/the/icon");
  10.  
  11.     return *myIconPtr;
  12. }

 
  ‹‹ It is a bug?      How to catch unhanded exception ? ››

You must log in to post a reply. Not a member yet? Register here!