Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Static cast other then int, char and boolean not possible?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved C++ Gurus
7 Posts 4 Posters 5.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vinb
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • L Offline
      L Offline
      leon.anavi
      wrote on last edited by
      #2

      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.

      http://anavi.org/

      1 Reply Last reply
      0
      • V Offline
        V Offline
        vinb
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • G Offline
          G Offline
          giesbert
          wrote on last edited by
          #4

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

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

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #5

            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!

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0
            • V Offline
              V Offline
              vinb
              wrote on last edited by
              #6

              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 to testlistview::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.

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on last edited by
                #7

                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;
                

                }
                @

                http://www.catb.org/~esr/faqs/smart-questions.html

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved