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. static_cast for downcasting Event

static_cast for downcasting Event

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 1.0k 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.
  • S Offline
    S Offline
    sandro4912
    wrote on last edited by Chris Kawa
    #1

    I have a code like this:

    void CellInputHandler::handleMouseButtonReleaseEvents(
            QObject *watched, QEvent *event)
    {
        auto mouseEvent = static_cast<QMouseEvent*>(event);
        //....
    }
    

    Now if I run Clang-Tidy it complains that i should use dynamic_cast. However even in some offical examples for stuff like this i saw example still with static_cast. Doesn't it slice the object and should be dynamic_cast or even qobject_cast ?

    1 Reply Last reply
    0
    • Chris KawaC Online
      Chris KawaC Online
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      You shouldn't do that. Can you show an official example that does that? All places I've seen deal with a pointer to event and there's no problem with static_casting that. Apart from the slicing your example copies the event in the parameter, which is also a bad thing, as events are usually an in-out parameter, meaning you modify them (e.g. calling accept() or reject() on them). Passing them by value doesn't really make sense.

      1 Reply Last reply
      1
      • S Offline
        S Offline
        sandro4912
        wrote on last edited by
        #3

        your'e right we just cast the pointer here. And still clan complains i should use dynamic_cast ...

        1 Reply Last reply
        0
        • Chris KawaC Online
          Chris KawaC Online
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Ah, sorry, the forum formatting ate the stars :) I fixed that for you. Please place code between ``` to make it format like code.

          Well technically it could be an invalid cast in some circumstances, as you could cast to an unrelated type, but if you check the type of the event before you cast (which you should anyway) there's no problem. dynamic_cast is kinda expensive so doing an if + static_cast for types you know it's ok for is just cheaper, but static analysis tools won't like it. I'd say it's safe to say it's a false positive in this case. The tool just can't know better.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            sandro4912
            wrote on last edited by sandro4912
            #5

            And what about q_object_cast? I read it should be a lot faster than dynamic_cast. Of course I think youre right static_cast should be faster still since theres no real check.

            1 Reply Last reply
            0
            • Chris KawaC Online
              Chris KawaC Online
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #6

              qobject_cast works with classes that derive from QObject and have the Q_OBJECT macro. QEvent is a simple "data" class and it does not derive from QObject so qobject_cast can't be used for it.

              1 Reply Last reply
              2

              • Login

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