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. Type casting in Qt
Forum Updated to NodeBB v4.3 + New Features

Type casting in Qt

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

    Hi all,

    What is a better way to do typecasting in Qt
    I'm getting warning saying "use of old-style cast" by typecasting following way:

    function((uint8_t*)data.data(), (size_t)data.length());
    

    What is the better way to do this?

    Thanks in advance.

    JonBJ 1 Reply Last reply
    0
    • V viniltc

      Hi all,

      What is a better way to do typecasting in Qt
      I'm getting warning saying "use of old-style cast" by typecasting following way:

      function((uint8_t*)data.data(), (size_t)data.length());
      

      What is the better way to do this?

      Thanks in advance.

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

      @viniltc
      You're "not allowed" to use those (lovely) C-style casts any longer. This isn't really Qt, it's C++. You should Google for generic static_cast<> & dynamic_cast<> for use in C++ code (e.g. https://en.cppreference.com/w/cpp/language/static_cast), and for qobject_cast<> in the Qt docs for a specific one useful in some Qt circumstances.

      I believe the compiler will be happier with your code if you try:

      function(static_cast<uint8_t*>(data.data()), static_cast<size_t>(data.length()));
      

      or probably better If it's OK with the type coercing:

      function(reinterpret_cast<uint8_t*>(data.data()), reinterpret_cast<size_t>(data.length()));
      
      1 Reply Last reply
      7
      • V Offline
        V Offline
        viniltc
        wrote on last edited by viniltc
        #3

        @JonB
        Thanks a lot for the feedback :)

        aha_1980A 1 Reply Last reply
        1
        • V viniltc

          @JonB
          Thanks a lot for the feedback :)

          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi @viniltc,

          so is your problem solved? Then pleas mark this topic as SOLVED too. Thanks!

          Qt has to stay free or it will die.

          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