Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Where to put own functions for use in several classes
Forum Updated to NodeBB v4.3 + New Features

Where to put own functions for use in several classes

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 5 Posters 360 Views 2 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.
  • P Offline
    P Offline
    Philipp DE
    wrote on last edited by
    #1

    Hi,

    I need to create a function which I need to use in several windows. In PHP I could create a function anywhere any use it anywhere.

    How can I do this in qt5?

    Do I need to create a new class and create static functions or do I just create a new header and new cpp file and put the functions there, include the header and use them?

    What will be the right way in qt5?

    1 Reply Last reply
    0
    • Kent-DorfmanK Offline
      Kent-DorfmanK Offline
      Kent-Dorfman
      wrote on last edited by
      #2

      Both methods will work. The more accepted C++ way is to use static class methods and include the header where needed.

      1 Reply Last reply
      3
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @Kent-Dorfman said in Where to put own functions for use in several classes:

        is to use static class methods and include the header where needed.

        Then the function must not be static ...

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        aha_1980A 1 Reply Last reply
        1
        • Christian EhrlicherC Christian Ehrlicher

          @Kent-Dorfman said in Where to put own functions for use in several classes:

          is to use static class methods and include the header where needed.

          Then the function must not be static ...

          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Christian-Ehrlicher said in Where to put own functions for use in several classes:

          @Kent-Dorfman said in Where to put own functions for use in several classes:

          is to use static class methods and include the header where needed.

          Then the function must not be static ...

          I think @Kent-Dorfman talked about static member functions, a la:

          // Header
          class Foo {
          public:
            static void bar();
          }
          
          // C++ file
          void Foo::bar()
          {
           // ...
          }
          

          while you meant static functions:

          // C++ file
          static void bar()
          {
          }
          

          which are indeed not visible outside this C++ file.

          Uhm, C++ is hard, actually...

          Regards

          Qt has to stay free or it will die.

          1 Reply Last reply
          2
          • M Offline
            M Offline
            mpergand
            wrote on last edited by
            #5

            Or using a namespace.

            namespace MyVeryUsefullFunctions
            {
                inline int Cube(int n ) { return n*n*n; }
            }
            ...
            using namespace  MyVeryUsefullFunctions;
            
            int c=Cube(10);
            
            1 Reply Last reply
            1

            • Login

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