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. QEvent::registerEventType()

QEvent::registerEventType()

Scheduled Pinned Locked Moved General and Desktop
13 Posts 4 Posters 10.7k 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.
  • F Offline
    F Offline
    Flurite
    wrote on last edited by
    #1

    The function QEvent::registerEventType() finds an un-created event type (between 1000 and 65535) and I believe it returns the first one it sees. How does this work, when it has to dynamically update enum Type in order to account for the new event?

    Is an array involved to track the unregistered events?

    Also, when you allocate an event, does it add it to the QEvent::Type enum list?

    1 Reply Last reply
    0
    • F Offline
      F Offline
      Flurite
      wrote on last edited by
      #2

      Are there any ideas on how this function works?

      1 Reply Last reply
      0
      • L Offline
        L Offline
        lgeyer
        wrote on last edited by
        #3

        http://qt.gitorious.org/qt/qt/blobs/4.8/src/corelib/kernel/qcoreevent.cpp#line363

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          Sure, sure. But the question is, I think, how do you use this generated unique id? The problem seems to be that QEvent doesn't take an integer, but a QEvent::Type enum in the constructor, and QEvent::type() returns a QEvent::Type. So, what do you actually do with your shiney new generated id? How do you use it to distinguish your event from other event types? Or can you just ignore the QEvent::Type and treat it as an int?

          1 Reply Last reply
          0
          • F Offline
            F Offline
            Flurite
            wrote on last edited by
            #5

            In the link, I didn't really understand what the userEventRegistrationHelper() function did, and I didn't see a definition for it, but that's a different problem.

            What I did see was that it loops through all possible custom event enum values (65535 all the way to 1000), and uses these member functions/variables of userEventRegistrationHelper (which also seems to be an instance).

            Andre, I think I saw an "example on StackOverflow":http://stackoverflow.com/a/4269757/1276964 (it uses a cheesy typecast from an int to an enum) that helps me understand the problem.

            My main question is how Qt tracks an enum to find un-registered enum numbers and if I can have my event be part of QEvent::Type enum.

            This is all just curiosity, I'm just a hobbyist.

            1 Reply Last reply
            0
            • L Offline
              L Offline
              lgeyer
              wrote on last edited by
              #6

              userEventRegistrationHelper() is a static function returning a global "QEventUserEventRegistration":http://qt.gitorious.org/qt/qt/blobs/4.8/src/corelib/kernel/qcoreevent.cpp#line355 instance (in a lazy-loaded, thread-safe manner due to Q_GLOBAL_STATIC).

              I thought the easiest way to answer your question is to link to the correspoding code, because it is the most obvious explaination: If the desired type (hint) is valid and not in the list of already taken types (userEventRegistration.set), take it. Otherwise find a non-taken type by iterating from the maximum value (QEvent::MaxUser) to the minimum value (QEvent::User), which is not in the list of already taken types.

              [quote author="Flurite" date="1348864532"]...how Qt tracks an enum to find un-registered enum numbers...[/quote]Qt keeps track of all registered user event types in a global list (userEventRegistrationHelper()->set), which is used in QEvent::registerEventType() to determine an unused event type.

              [quote author="Flurite" date="1348864532"]...and if I can have my event be part of QEvent::Type enum.[/quote]No, you can't, at least not without modifying the Type enum found in the qcoreevent.h header (no QEvent::Type type = QEvent::MyEventType).

              However, you can store any arbitrary value of the enum's underlying datatype (which can be any integral type, usually int) using an explicit static cast (QEvent::Type type = static_castQEvent::Type(42)), as show in the SO example.

              1 Reply Last reply
              0
              • D Offline
                D Offline
                dangelog
                wrote on last edited by
                #7

                [quote author="Andre" date="1348832220"]Sure, sure. But the question is, I think, how do you use this generated unique id? The problem seems to be that QEvent doesn't take an integer, but a QEvent::Type enum in the constructor, and QEvent::type() returns a QEvent::Type. So, what do you actually do with your shiney new generated id? How do you use it to distinguish your event from other event types? Or can you just ignore the QEvent::Type and treat it as an int?[/quote]

                I usually follow this pattern:

                • every QEvent custom class has a static member variable, typed int, to hold the registered event type
                • in main() I initialize all those variables using registerEventType
                • in their constructors I call the base-class constructor with QEvent(static_castQEvent::Type(id)), plus a Q_ASSERT(id != 0) to be 100% sure I don't miss an initialization and leave the value zero-initialized

                Software Engineer
                KDAB (UK) Ltd., a KDAB Group company

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

                  So I conclude, that QEvent::id() and QEvent::QEvent(<type> id>) should have been int based, and not use QEvent::Type as the type. It is weird that you need static casts to and from an enum to hack in values that don't really belong inside the enum in there anyway. Thanks!

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    lgeyer
                    wrote on last edited by
                    #9

                    Well, I think it is a classical trade-off.

                    You will have to use a static cast anyways, either when using a custom type in the case of QEvent::Type or using a built-in type in the case of int, as there is no implicit conversion from int to QEvent::Type.

                    On the supposition that in the majority of cases you will use a built-in type using QEvent::Type will leave you with a static cast in the minority of cases (instead of the other way round).

                    You can add a QEvent::QEvent(int) constructor, which will act as an implicit conversion constructor, but you will nullify the reason then why you've used an enum in the first place: type safety - which is of course quite debatable as QEvent is meant to take types not beeing part of QEvent::Type anyways ;-)

                    Interestingly enough QEvent itself uses an integer to store the type (more precisely ushort) internally, so there might be other reasons for having an QEvent::Type based interface.

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

                      Well, you can directly compare an int against an enum value, right? You don't need static casts for that, AFAIK. Enums are used not only for type savety, they are also convenient containers for a set of const values. That is done all over Qt already. And as you say: QEvent already takes values not in that enum, and is obviously designed this way too. So, I think int would have been a better choice.

                      1 Reply Last reply
                      0
                      • L Offline
                        L Offline
                        lgeyer
                        wrote on last edited by
                        #11

                        [quote author="Andre" date="1349089282"]Well, you can directly compare an int against an enum value, right? You don't need static casts for that, AFAIK.[/quote]Yes, you can, because there is an implicit cast from enum to int - but not from int to enum.

                        -So the problem would not be merely the comparison, but rather instantiation. Having an int based interface would mean that <code>QEvent(QEvent::WhateverEvent)</code> would become <code>QEvent(static_castQEvent::Type(QEvent::WhateverEvent))</code>, as used hundreds of times in Qt itself - this isn't a viable option.-

                        -So the only thing I see happening is having both, a QEvent::Type and int constructor.-

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

                          No, it would not need a cast there, as as you say enum to int is automatic. So if it would have been QEvent::QEvent(int), then QEvent(QEvent::WhateverEvent) would have worked just fine. Only the other way around needs the cast, as we've already seen.

                          I am not saying that I will see this change happening, I was just arguing that the current API is a bit strange if you want it to be extendible by 3rd party code defining custom events, as is obviously the case.

                          1 Reply Last reply
                          0
                          • L Offline
                            L Offline
                            lgeyer
                            wrote on last edited by
                            #13

                            Well, of course there would be. Sometimes you are just beaten by your own logic. ;-)

                            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