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. What types can you pass as signal args on QT ?
Forum Updated to NodeBB v4.3 + New Features

What types can you pass as signal args on QT ?

Scheduled Pinned Locked Moved General and Desktop
16 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.
  • A Offline
    A Offline
    aabc
    wrote on last edited by
    #1

    What types can you pass as signal args on QT ?

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      I think you can pass anything. I never bothered to check, but it always worked :)

      (Z(:^

      1 Reply Last reply
      0
      • A Offline
        A Offline
        aabc
        wrote on last edited by
        #3

        Did you passes enums ?

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          I don't think I have tried that. But enums are ints, so there should not be any problems, especially if you wrap them as QFlag/ QFlags.

          (Z(:^

          1 Reply Last reply
          0
          • A Offline
            A Offline
            aabc
            wrote on last edited by
            #5

            How do you wrap them ?

            1 Reply Last reply
            0
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              QFlags introduces some nice features, like more rigid compile-time checks and easy int conversion. Here is the doc for that: "QFlags":http://qt-project.org/doc/qt-4.8/qflags.html.

              (Z(:^

              1 Reply Last reply
              0
              • R Offline
                R Offline
                rschaub
                wrote on last edited by
                #7

                you can pass anything as arguments as long as the types are registered using "qRegisterMetaType":http://qt-project.org/doc/qt-4.8/qmetatype.html#qRegisterMetaType and "Q_DECLARE_METATYPE":http://qt-project.org/doc/qt-4.8/qmetatype.html#Q_DECLARE_METATYPE

                note that using pointers and/or references as arguments might have unforseen side-effects:
                If you're passing a pointer or reference to a local instance to a queued signal-slot connection, you might end up with an invalid argument in the slot since the original instance might already be gone by the time the slot is called.

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  aabc
                  wrote on last edited by
                  #8

                  Can I register just an ENUM from some QT class or do I have to register the all class ?

                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    rschaub
                    wrote on last edited by
                    #9

                    It's possible to register just the enum. If you're registering it outside the class, you will have to use the fully qualified path (enum name including all the namespace/class names, e.g. "CMyClass::EMyEnum"). I'm not sure what the behaviour is if you're registering the enum within the class itself and then emit/invoke outside the class. It's possible that you'll have to register the enum on the same level you're using it from (e.g. "EMyEnum" when using it within the class, "CMyClass::EMyEnum" when using it outside CMyClass), but I haven't tried that.

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      aabc
                      wrote on last edited by
                      #10

                      Did you ever tried pass QT class enum as signal parameter ?

                      1 Reply Last reply
                      0
                      • R Offline
                        R Offline
                        rschaub
                        wrote on last edited by
                        #11

                        I think I did, but I can't tell with absolute certainty.
                        If you can't get it to work, you could try moving the enum from within the class to its own namespace.

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          aabc
                          wrote on last edited by
                          #12

                          The enum is part of a QT class

                          1 Reply Last reply
                          0
                          • V Offline
                            V Offline
                            viktor.benei
                            wrote on last edited by
                            #13

                            I did use enums as signal/slot parameters and generally you have two easy solutions:

                            1. Use Q_ENUMS() to register your enum in the class you want to use
                            2. use the mentioned qRegisterMetaType() to register it as a meta-type for Qt

                            I prefer the 2nd solution, it's more error-proof in most of the cases.

                            1 Reply Last reply
                            0
                            • A Offline
                              A Offline
                              aabc
                              wrote on last edited by
                              #14

                              I mean that the enum I want to use is part of a Qt class that was provided by Qt (not a class I created)

                              1 Reply Last reply
                              0
                              • V Offline
                                V Offline
                                viktor.benei
                                wrote on last edited by
                                #15

                                Qt defined enums are nothing special. The same rules apply for them.

                                Before you could connect a signal/slot with an enum parameter you have to register it (either with qRegisterMetaType() or with Q_ENUMS). Qt may register the enum you want to use (which is part of Qt) but may not, or will register it later when it uses somewhere but you would like to use it before Qt would registers it.

                                So the principle is you should register any enum you want to use before you would actually use it. It doesn't matter whether it's your own or it's part of Qt.

                                Of course you can experiment as well -> if Qt cannot use it in a signal/slot it will print an error message for you in the log and will point you to the exact location where it cannot handle it.

                                1 Reply Last reply
                                0
                                • A Offline
                                  A Offline
                                  aabc
                                  wrote on last edited by
                                  #16

                                  Thank you.
                                  I will try it

                                  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