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. constant functions in Qt Classes
Forum Updated to NodeBB v4.3 + New Features

constant functions in Qt Classes

Scheduled Pinned Locked Moved Solved C++ Gurus
14 Posts 7 Posters 1.5k 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.
  • S Offline
    S Offline
    Swati777999
    wrote on 9 Dec 2021, 09:11 last edited by
    #1

    In different classes of Qt, I've come across functions followed by const keyword. Refer to the example below:

    In QLabel Class; in public functions, there are following functions:
    Function 1: QString text() const
    Function 2: void setMargin(int)

    In what way Function1 differ from Function2 except the operations they perform?

    “ In order to be irreplaceable, one must always be different” – Coco Chanel

    J J 2 Replies Last reply 9 Dec 2021, 09:20
    0
    • S Swati777999
      9 Dec 2021, 09:11

      In different classes of Qt, I've come across functions followed by const keyword. Refer to the example below:

      In QLabel Class; in public functions, there are following functions:
      Function 1: QString text() const
      Function 2: void setMargin(int)

      In what way Function1 differ from Function2 except the operations they perform?

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 9 Dec 2021, 09:20 last edited by jsulm 12 Sept 2021, 09:29
      #2

      @Swati777999 said in constant functions in Qt Classes:

      In what way Function1 differ from Function2 except the operations they perform?

      You should really read a C++ book!

      A const method is not allowed to change the state of the object on which it is called.
      That means that text() will not change the internal state of the object (it will not change any internal variables). If you try to change the state of the object in a const method you will get compiler error. Especially getter methods are usually const methods because they simply return the value of an internal variable without changing anything.
      A non-const method is allowed to change internal object state. For example setMargin(...) will change the margin in the object.

      Also, please ask such questions in https://forum.qt.io/category/34/c-gurus

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      S 1 Reply Last reply 9 Dec 2021, 09:33
      5
      • S Swati777999
        9 Dec 2021, 09:11

        In different classes of Qt, I've come across functions followed by const keyword. Refer to the example below:

        In QLabel Class; in public functions, there are following functions:
        Function 1: QString text() const
        Function 2: void setMargin(int)

        In what way Function1 differ from Function2 except the operations they perform?

        J Offline
        J Offline
        J.Hilk
        Moderators
        wrote on 9 Dec 2021, 09:28 last edited by J.Hilk 12 Sept 2021, 09:29
        #3

        @Swati777999 interpret it as readonly


        and moved to "c++ gurus"


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        J 1 Reply Last reply 9 Dec 2021, 09:31
        1
        • J J.Hilk
          9 Dec 2021, 09:28

          @Swati777999 interpret it as readonly


          and moved to "c++ gurus"

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 9 Dec 2021, 09:31 last edited by
          #4

          @J-Hilk This is way shorter explanation than mine :-)

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • J jsulm
            9 Dec 2021, 09:20

            @Swati777999 said in constant functions in Qt Classes:

            In what way Function1 differ from Function2 except the operations they perform?

            You should really read a C++ book!

            A const method is not allowed to change the state of the object on which it is called.
            That means that text() will not change the internal state of the object (it will not change any internal variables). If you try to change the state of the object in a const method you will get compiler error. Especially getter methods are usually const methods because they simply return the value of an internal variable without changing anything.
            A non-const method is allowed to change internal object state. For example setMargin(...) will change the margin in the object.

            Also, please ask such questions in https://forum.qt.io/category/34/c-gurus

            S Offline
            S Offline
            Swati777999
            wrote on 9 Dec 2021, 09:33 last edited by
            #5

            @jsulm
            Qt is a combination of C++, Python, and Java Script while speaking in terms of syntax. Out of these 3, I'm not familiar with JavaScript.

            “ In order to be irreplaceable, one must always be different” – Coco Chanel

            J J 2 Replies Last reply 9 Dec 2021, 09:37
            0
            • S Swati777999
              9 Dec 2021, 09:33

              @jsulm
              Qt is a combination of C++, Python, and Java Script while speaking in terms of syntax. Out of these 3, I'm not familiar with JavaScript.

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 9 Dec 2021, 09:37 last edited by
              #6

              @Swati777999 Qt is a C++ framework, so C++ syntax applies in C++ Qt applications. QML is based on JavaScript syntax. There are binding for Python also.
              What you asked in this thread has nothing to do with Qt but belongs to basic C++ syntax, so please use correct forum for such questions.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              3
              • S Swati777999
                9 Dec 2021, 09:33

                @jsulm
                Qt is a combination of C++, Python, and Java Script while speaking in terms of syntax. Out of these 3, I'm not familiar with JavaScript.

                J Offline
                J Offline
                JonB
                wrote on 9 Dec 2021, 09:37 last edited by
                #7

                @Swati777999
                Qt is not a combination of any languages. It is not a language, it is a library of functions. It is available for use from C++ or from Python (quite separately from one another). There is no JavaScript in anything you show, and JS is barely used in anything much from Qt.

                Your questions, or your question here, is just to do with C++. Nothing Qt about it.

                1 Reply Last reply
                3
                • JoeCFDJ Offline
                  JoeCFDJ Offline
                  JoeCFD
                  wrote on 9 Dec 2021, 17:24 last edited by JoeCFD 12 Sept 2021, 18:13
                  #8

                  https://www.geeksforgeeks.org/const-keyword-in-cpp/
                  const is a useful keyword in C++ and try to use it as much as possible whereever it is applied. It can avoid some silly bugs.

                  1 Reply Last reply
                  1
                  • Kent-DorfmanK Offline
                    Kent-DorfmanK Offline
                    Kent-Dorfman
                    wrote on 10 Dec 2021, 04:21 last edited by
                    #9

                    @JoeCFD said in constant functions in Qt Classes:

                    const is a useful keyword in C++

                    const is a PITA keyword...too much culture of "protect people from themselves" for this old guy to stomach.

                    JKSHJ JoeCFDJ J 3 Replies Last reply 10 Dec 2021, 04:58
                    0
                    • Kent-DorfmanK Kent-Dorfman
                      10 Dec 2021, 04:21

                      @JoeCFD said in constant functions in Qt Classes:

                      const is a useful keyword in C++

                      const is a PITA keyword...too much culture of "protect people from themselves" for this old guy to stomach.

                      JKSHJ Offline
                      JKSHJ Offline
                      JKSH
                      Moderators
                      wrote on 10 Dec 2021, 04:58 last edited by
                      #10

                      @Kent-Dorfman said in constant functions in Qt Classes:

                      @JoeCFD said in constant functions in Qt Classes:

                      const is a useful keyword in C++

                      const is a PITA keyword...too much culture of "protect people from themselves" for this old guy to stomach.

                      It's not just for protection. It also conveys the designer's intent for the API and it facilitates optimizations.

                      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                      1 Reply Last reply
                      5
                      • Kent-DorfmanK Kent-Dorfman
                        10 Dec 2021, 04:21

                        @JoeCFD said in constant functions in Qt Classes:

                        const is a useful keyword in C++

                        const is a PITA keyword...too much culture of "protect people from themselves" for this old guy to stomach.

                        JoeCFDJ Offline
                        JoeCFDJ Offline
                        JoeCFD
                        wrote on 10 Dec 2021, 15:05 last edited by
                        #11

                        @Kent-Dorfman I swallowed it without any side effects. I know you will hate it.

                        1 Reply Last reply
                        0
                        • Kent-DorfmanK Kent-Dorfman
                          10 Dec 2021, 04:21

                          @JoeCFD said in constant functions in Qt Classes:

                          const is a useful keyword in C++

                          const is a PITA keyword...too much culture of "protect people from themselves" for this old guy to stomach.

                          J Offline
                          J Offline
                          JonB
                          wrote on 10 Dec 2021, 17:58 last edited by
                          #12

                          @Kent-Dorfman said in constant functions in Qt Classes:

                          const is a PITA keyword...too much culture of "protect people from themselves" for this old guy to stomach.

                          Wow! Did you start out with C when it had no const keyword?

                          Kent-DorfmanK 1 Reply Last reply 11 Dec 2021, 02:39
                          0
                          • J JonB
                            10 Dec 2021, 17:58

                            @Kent-Dorfman said in constant functions in Qt Classes:

                            const is a PITA keyword...too much culture of "protect people from themselves" for this old guy to stomach.

                            Wow! Did you start out with C when it had no const keyword?

                            Kent-DorfmanK Offline
                            Kent-DorfmanK Offline
                            Kent-Dorfman
                            wrote on 11 Dec 2021, 02:39 last edited by Kent-Dorfman 12 Nov 2021, 02:43
                            #13

                            @JonB

                            K&R C:

                            fn(a,b)
                            int a;
                            int b; 
                            {
                            /* stuff */
                            }
                            
                            J 1 Reply Last reply 11 Dec 2021, 08:11
                            0
                            • Kent-DorfmanK Kent-Dorfman
                              11 Dec 2021, 02:39

                              @JonB

                              K&R C:

                              fn(a,b)
                              int a;
                              int b; 
                              {
                              /* stuff */
                              }
                              
                              J Offline
                              J Offline
                              JonB
                              wrote on 11 Dec 2021, 08:11 last edited by
                              #14

                              @Kent-Dorfman
                              I'm with you :) Originally there wasn't any void either!

                              1 Reply Last reply
                              0

                              1/14

                              9 Dec 2021, 09:11

                              • Login

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