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. const member function can't emit a signal that is not const
Forum Update on Monday, May 27th 2025

const member function can't emit a signal that is not const

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 2 Posters 1.8k Views
  • 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.
  • jronaldJ Offline
    jronaldJ Offline
    jronald
    wrote on last edited by jronald
    #1

    UPDATE 1

    It is by design
    https://github.com/KDE/clazy/blob/master/docs/checks/README-const-signal-or-slot.md


    Env

    OS: Linux
    Qt: 6.3.0
    gcc: 12.1.0

    Problem

    class A : public QObject
    {
        Q_OBJECT
    
        ...
    
    Q_SIGNALS:
        void Log(const LogItem & log_item);
    
    public:
        void TestSignal() const { emit Log(...));
    }
    

    A::TestSignal leads to a build error

    error: passing ‘const A as ‘this’ argument discards qualifiers [-fpermissive]
    

    If make the signal as const

    void Log(const LogItem & log_item) const;
    

    The build passes, but there is a warning for the signal

    signal A::Log shouldn't be const [clazy-const-signal-or-slot]
    

    Does this warning make sense?
    Any better solution?

    JonBJ 1 Reply Last reply
    0
    • jronaldJ jronald

      @JonB said in const member function can't emit a signal that is not const:

      Since signals change state, TestSignal should not be const.

      In the view of an API user, emitting a signal should not change anything, so it should be const natively.
      For this restriction, I have to do log in another way instead of by signals.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #4

      @jronald said in const member function can't emit a signal that is not const:

      In the view of an API user, emitting a signal should not change anything, so it should be const natively.

      That is just something you are choosing to state. Signals do change object state, as per your link, else you would not get the error/warning.

      For this restriction, I have to do log in another way instead of by signals.

      Don't emit a signal from a const method, as the link states. Why does a const method need to emit a signal anyway?

      Or rather than abandon the logic of signals/slots, find a way to achieve your goal. E.g. maybe you could suppress warning "clazy-const-signal-or-slot" if you are determined to do things your current way.

      I have not looked, but if you Google for something like qt signal const I would imagine if others have the same intention as you there will be hits....

      jronaldJ 1 Reply Last reply
      0
      • jronaldJ jronald

        UPDATE 1

        It is by design
        https://github.com/KDE/clazy/blob/master/docs/checks/README-const-signal-or-slot.md


        Env

        OS: Linux
        Qt: 6.3.0
        gcc: 12.1.0

        Problem

        class A : public QObject
        {
            Q_OBJECT
        
            ...
        
        Q_SIGNALS:
            void Log(const LogItem & log_item);
        
        public:
            void TestSignal() const { emit Log(...));
        }
        

        A::TestSignal leads to a build error

        error: passing ‘const A as ‘this’ argument discards qualifiers [-fpermissive]
        

        If make the signal as const

        void Log(const LogItem & log_item) const;
        

        The build passes, but there is a warning for the signal

        signal A::Log shouldn't be const [clazy-const-signal-or-slot]
        

        Does this warning make sense?
        Any better solution?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #2

        @jronald said in const member function can't emit a signal that is not const:

        A::TestSignal leads to a build error

        As your lionk states:

        Prevents you from emitting signals from const methods, as these methods shouldn't change state, and a signal implies state was changed

        Since signals change state, TestSignal should not be const.

        jronaldJ 1 Reply Last reply
        0
        • JonBJ JonB

          @jronald said in const member function can't emit a signal that is not const:

          A::TestSignal leads to a build error

          As your lionk states:

          Prevents you from emitting signals from const methods, as these methods shouldn't change state, and a signal implies state was changed

          Since signals change state, TestSignal should not be const.

          jronaldJ Offline
          jronaldJ Offline
          jronald
          wrote on last edited by jronald
          #3

          @JonB said in const member function can't emit a signal that is not const:

          Since signals change state, TestSignal should not be const.

          In the view of an API user, emitting a signal should not change anything, so it should be const natively.
          For this restriction, I have to do log in another way instead of by signals.

          JonBJ 1 Reply Last reply
          0
          • jronaldJ jronald

            @JonB said in const member function can't emit a signal that is not const:

            Since signals change state, TestSignal should not be const.

            In the view of an API user, emitting a signal should not change anything, so it should be const natively.
            For this restriction, I have to do log in another way instead of by signals.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #4

            @jronald said in const member function can't emit a signal that is not const:

            In the view of an API user, emitting a signal should not change anything, so it should be const natively.

            That is just something you are choosing to state. Signals do change object state, as per your link, else you would not get the error/warning.

            For this restriction, I have to do log in another way instead of by signals.

            Don't emit a signal from a const method, as the link states. Why does a const method need to emit a signal anyway?

            Or rather than abandon the logic of signals/slots, find a way to achieve your goal. E.g. maybe you could suppress warning "clazy-const-signal-or-slot" if you are determined to do things your current way.

            I have not looked, but if you Google for something like qt signal const I would imagine if others have the same intention as you there will be hits....

            jronaldJ 1 Reply Last reply
            0
            • JonBJ JonB

              @jronald said in const member function can't emit a signal that is not const:

              In the view of an API user, emitting a signal should not change anything, so it should be const natively.

              That is just something you are choosing to state. Signals do change object state, as per your link, else you would not get the error/warning.

              For this restriction, I have to do log in another way instead of by signals.

              Don't emit a signal from a const method, as the link states. Why does a const method need to emit a signal anyway?

              Or rather than abandon the logic of signals/slots, find a way to achieve your goal. E.g. maybe you could suppress warning "clazy-const-signal-or-slot" if you are determined to do things your current way.

              I have not looked, but if you Google for something like qt signal const I would imagine if others have the same intention as you there will be hits....

              jronaldJ Offline
              jronaldJ Offline
              jronald
              wrote on last edited by jronald
              #5

              @JonB said in const member function can't emit a signal that is not const:

              Why does a const method need to emit a signal anyway?

              Emitting a signal still keeps the method constant.
              If it is allowed I'll do it, but it isn't allowed now, so I need to find another way.

              Thanks

              JonBJ 1 Reply Last reply
              0
              • jronaldJ jronald

                @JonB said in const member function can't emit a signal that is not const:

                Why does a const method need to emit a signal anyway?

                Emitting a signal still keeps the method constant.
                If it is allowed I'll do it, but it isn't allowed now, so I need to find another way.

                Thanks

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #6

                @jronald said in const member function can't emit a signal that is not const:

                Emitting a signal still keeps the method constant.

                But it does not, since a signal method is not const, as per the error message/debate we are having.

                Anyway the question was why does a method which is const need to emit a signal anyway? If nothing is being altered (const method) what is it emitting a signal for?

                jronaldJ 1 Reply Last reply
                0
                • JonBJ JonB

                  @jronald said in const member function can't emit a signal that is not const:

                  Emitting a signal still keeps the method constant.

                  But it does not, since a signal method is not const, as per the error message/debate we are having.

                  Anyway the question was why does a method which is const need to emit a signal anyway? If nothing is being altered (const method) what is it emitting a signal for?

                  jronaldJ Offline
                  jronaldJ Offline
                  jronald
                  wrote on last edited by
                  #7

                  @JonB said in const member function can't emit a signal that is not const:

                  If nothing is being altered (const method) what is it emitting a signal for?

                  In my app, the const method is to parse data from a pdf file, when it fails, it's helpful to see the logs, and GUI is more powerful to show the logs. If signals are emitted at key points in the method, the signals can be connected to a widgets,then the logs could be shown instantly and nicely.

                  JonBJ 1 Reply Last reply
                  1
                  • jronaldJ jronald

                    @JonB said in const member function can't emit a signal that is not const:

                    If nothing is being altered (const method) what is it emitting a signal for?

                    In my app, the const method is to parse data from a pdf file, when it fails, it's helpful to see the logs, and GUI is more powerful to show the logs. If signals are emitted at key points in the method, the signals can be connected to a widgets,then the logs could be shown instantly and nicely.

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #8

                    @jronald
                    I do understand. But wanting to do logging, from a const object, can often lead to such an issue, not just in Qt.

                    It is a shame C++ does not seem to allow methods as mutable, like you can for variables, so you can use them from const objects/methods!

                    Did you consider sticking with your const signal and suppressing the clazy-const-signal-or-slot warning?

                    jronaldJ 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @jronald
                      I do understand. But wanting to do logging, from a const object, can often lead to such an issue, not just in Qt.

                      It is a shame C++ does not seem to allow methods as mutable, like you can for variables, so you can use them from const objects/methods!

                      Did you consider sticking with your const signal and suppressing the clazy-const-signal-or-slot warning?

                      jronaldJ Offline
                      jronaldJ Offline
                      jronald
                      wrote on last edited by
                      #9

                      @JonB said in const member function can't emit a signal that is not const:

                      Did you consider sticking with your const signal and suppressing the clazy-const-signal-or-slot warning?

                      Not yet, I'm finding a proper way.

                      JonBJ 1 Reply Last reply
                      0
                      • jronaldJ jronald

                        @JonB said in const member function can't emit a signal that is not const:

                        Did you consider sticking with your const signal and suppressing the clazy-const-signal-or-slot warning?

                        Not yet, I'm finding a proper way.

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by
                        #10

                        @jronald
                        Consider either of the "workarounds" suggested in https://stackoverflow.com/a/5787589/489865.

                        jronaldJ 1 Reply Last reply
                        1
                        • JonBJ JonB

                          @jronald
                          Consider either of the "workarounds" suggested in https://stackoverflow.com/a/5787589/489865.

                          jronaldJ Offline
                          jronaldJ Offline
                          jronald
                          wrote on last edited by jronald
                          #11

                          @JonB said in const member function can't emit a signal that is not const:

                          @jronald
                          Consider either of the "workarounds" suggested in https://stackoverflow.com/a/5787589/489865.

                          It suggests adding const to signal.
                          In this way no warning when building, only Qt Creator shows a warning.
                          In another way removing const for the methods that emits signals meets Qt's convention.
                          It should be a personal choice.

                          Thanks

                          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