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:
    @
    static const int x = 1; //everything is working as expected here.
    //static const QIcon fileicon = ":/icon/pic4"; //doesnt work.
    //static const QIcon fileicon(":/icon/pic4"); //doesnt work.
    static const char y = 'd'; //everything working as expected.

    @
    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?



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

    @
    class Example
    {
    private:
    static const int x = 1; //everything is working as expected here.
    static const QIcon fileicon;
    static const char y = 'd'; //everything working as expected.
    //..
    };

    const QIcon Example::fileicon(":/icon/pic4");
    @

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



  • 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.



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



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

    @
    // this works
    static QIcon myIcon(":/abc.png");

    // this gives an error
    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!



  • 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.
    [quote author="Volker" date="1306623835"]
    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![/quote]

    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:
    @
    static QIcon myIcon(":/icon/pic4"); //like Volker suggested,
    //Gives error../TestAll/testlistview.h:20: error: expected identifier before string constant
    // ../TestAll/testlistview.h:20: error: expected ‘,’ or ‘...’ before string constant

    in class:
    static QIcon myIcon;
    in constructor:
    myIcon(":/icon/pic4");
    //Gives error: ../TestAll/testlistview.cpp:14: error: no match for call to ‘(QIcon) (const char [12])’
    myIcon.addfile(":/icon/pic4");.
    //Gives error: /home/koster/QtApps/TestAll-build/../TestAll/testlistview.cpp:14: undefined reference to testlistview::fileIcon' //home/koster/QtApps/TestAll-build/../TestAll/testlistview.cpp:17: undefined reference totestlistview::fileIcon'

    If i try leon.anavi 's suggestion:
    in class:
    static QIcon myIcon;
    somewhere in .cpp file
    QIcon testlistview::myIcon(":/icon/pic4");
    Gives a crash with the words:
    //QPixmap: Must construct a QApplication before a QPaintDevice
    //The program has unexpectedly finished.
    //Is this what you meant Volker?
    @

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



  • 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;

    @
    class MyIconProviderClass {
    public:
    // your other public stuff goes here
    static const QIcon &myIcon();

    private:
        static QIcon *myIconPtr;
    

    @

    -------- myiconproviderclass.cpp --------

    @
    #include "myiconproviderclass.h"
    #include <QIcon>

    QIcon* MyIconProviderClass::myIconPtr = 0;

    const QIcon& MyIconProviderClass::myIcon()
    {
    if(!myIconPtr)
    myIconPtr = new QIcon(":/path/to/the/icon");

    return *myIconPtr;
    

    }
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.