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. connect syntax
Forum Updated to NodeBB v4.3 + New Features

connect syntax

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 817 Views 3 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.
  • D Offline
    D Offline
    Dan203
    wrote on last edited by Dan203
    #1

    Is there any functional difference between using this connect syntax...

    connect(&m_fileWatcher, &QFileSystemWatcher::directoryChanged, this, &MyClass::on_directoryChanged);
    

    and this syntax...

    connect(&m_fileWatcher, SIGNAL(directoryChanged(const QString&)), SLOT(on_directoryChanged(const QString&));
    

    ?

    I've seen samples online using both. To me the former is a lot cleaner, especially for slots/signals with a lot of parameters, but I want to make sure there are no issues using that syntax.

    Is either one preferred over the other? In my limited testing they seem to work identically, but I'm new to Qt so I want to be sure

    1 Reply Last reply
    0
    • gde23G Offline
      gde23G Offline
      gde23
      wrote on last edited by
      #2

      https://doc.qt.io/qt-5/signalsandslots-syntaxes.html

      1 Reply Last reply
      3
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #3

        Hi
        Yes The first syntax catches errors at compile time ( called the new syntax hereafter)
        The SLOT macros will eat anything at compile and silently fail at runtime.

        The new syntax also allows to use c++ lambdas and basically anything callable as a slot.

        For the compile check alone, the new syntax is preferred. BUt being able to use lambdas also is a huge plus for some code.

        Its explained here
        https://wiki.qt.io/New_Signal_Slot_Syntax

        jeremy_kJ 1 Reply Last reply
        3
        • D Offline
          D Offline
          Dan203
          wrote on last edited by
          #4

          Thanks.

          I'm using Widgets not QML and I'd rather have a compile time error than a run time error, so it seems like the "Functor-Based Connections" are the best option for me.

          mrjjM 1 Reply Last reply
          0
          • D Dan203

            Thanks.

            I'm using Widgets not QML and I'd rather have a compile time error than a run time error, so it seems like the "Functor-Based Connections" are the best option for me.

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Dan203
            Hi
            The only real downside to the "new syntax" is function overload.
            Like for some Widgets that have the same signal with different parameters, where the syntax can be somewhat
            awkward.

            like
            https://doc.qt.io/qt-5/qcombobox.html#activated

            But if you have ever used SLOT/SIGNAL macros in a larger project and have features break due to someone
            renamed a slot or signal, you will want the new syntax. :)

            1 Reply Last reply
            4
            • mrjjM mrjj

              Hi
              Yes The first syntax catches errors at compile time ( called the new syntax hereafter)
              The SLOT macros will eat anything at compile and silently fail at runtime.

              The new syntax also allows to use c++ lambdas and basically anything callable as a slot.

              For the compile check alone, the new syntax is preferred. BUt being able to use lambdas also is a huge plus for some code.

              Its explained here
              https://wiki.qt.io/New_Signal_Slot_Syntax

              jeremy_kJ Offline
              jeremy_kJ Offline
              jeremy_k
              wrote on last edited by
              #6

              @mrjj said in connect syntax:

              The SLOT macros will eat anything at compile and silently fail at runtime.

              Failures for invalid or non-matching signature strings are detected at runtime. They're not "silent". There are several qWarnings that may be triggered, based on the specifics of the failure. The returned QMetaObject::Connection object's operator bool() should return false.

              QObject::connect using functors that successfully compile may also fail at runtime, and can be detected in the same way.

              Asking a question about code? http://eel.is/iso-c++/testcase/

              mrjjM 1 Reply Last reply
              1
              • jeremy_kJ jeremy_k

                @mrjj said in connect syntax:

                The SLOT macros will eat anything at compile and silently fail at runtime.

                Failures for invalid or non-matching signature strings are detected at runtime. They're not "silent". There are several qWarnings that may be triggered, based on the specifics of the failure. The returned QMetaObject::Connection object's operator bool() should return false.

                QObject::connect using functors that successfully compile may also fail at runtime, and can be detected in the same way.

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by mrjj
                #7

                @jeremy_k
                Hi
                Yes you are right, if you are running the app in creator and/or can show
                such warnings. I adjusted "silent" in above post.

                In any case, the new syntax is recommended.

                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