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. void* to QString C++
Forum Updated to NodeBB v4.3 + New Features

void* to QString C++

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 5 Posters 2.5k Views 2 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
    derric
    wrote on last edited by derric
    #1

    I have following code:

    void check()
    {
        QString stringdat = "Hello Qt";
        foo(&stringdat);
    }
    
    void foo(void* ptr)
    {
        QString* data = (QString*) ptr; 
    
         if(data != NULL)
        {
             qDebug() << "Data is " << data; 
        }
    
    }
    

    However the challenge is :

    QString* data = (QString*) ptr;
    

    How do it achieve the void* to QString conversion ?

    JonBJ 1 Reply Last reply
    0
    • D derric

      I have following code:

      void check()
      {
          QString stringdat = "Hello Qt";
          foo(&stringdat);
      }
      
      void foo(void* ptr)
      {
          QString* data = (QString*) ptr; 
      
           if(data != NULL)
          {
               qDebug() << "Data is " << data; 
          }
      
      }
      

      However the challenge is :

      QString* data = (QString*) ptr;
      

      How do it achieve the void* to QString conversion ?

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

      @derric said in void* to QString C++:

      QString* data = (QString*) ptr; 
      qDebug() << "Data is " << data; 
      

      As @eyllanesc has said: since data is a QString *, if you output just that you get a pointer value, like 0x65fe9c. If you want to see what it points to, you need qDebug() << "Data is " << *data;, which dereferences the pointer and will print the QString, which is Hello Qt here.

      There is nothing special in this context because you chose to pass the (address of the) original QString as a void * here.

      As I'm sure you are aware, if you use that C-style cast (QString*) ptr and the ptr passed in does not point to a QString, disaster will occur when you go *data! :)

      D 1 Reply Last reply
      2
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi and welcome to devnet,

        You need to dereference the pointer.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        D 1 Reply Last reply
        4
        • Kent-DorfmanK Offline
          Kent-DorfmanK Offline
          Kent-Dorfman
          wrote on last edited by Kent-Dorfman
          #3

          void pointers are highly discouraged in C++...just sayin. Look at the plethora of "safe coding" standards out there and you'll see what I mean. I'd suggest either using the type system or making templates and avoiding void* altogether.

          Your example use case is a prime example of why it's bad ju-ju. What happens when the internal foo() QString tries to reference a pointer that is NOT a conforming QString?

          D 1 Reply Last reply
          4
          • Kent-DorfmanK Kent-Dorfman

            void pointers are highly discouraged in C++...just sayin. Look at the plethora of "safe coding" standards out there and you'll see what I mean. I'd suggest either using the type system or making templates and avoiding void* altogether.

            Your example use case is a prime example of why it's bad ju-ju. What happens when the internal foo() QString tries to reference a pointer that is NOT a conforming QString?

            D Offline
            D Offline
            derric
            wrote on last edited by
            #4

            @Kent-Dorfman I have posted the above for a reason and i needed to know how? i'm expecting answers not suggestions or questions back.

            If you could put the question about what happens when the internal QString tries to reference a pointer thats is not QString at https://forum.qt.io/, i'll be happy to answer your question.

            Here i'm not looking forward for why i should not be using void pointers

            1 Reply Last reply
            -2
            • SGaistS SGaist

              Hi and welcome to devnet,

              You need to dereference the pointer.

              D Offline
              D Offline
              derric
              wrote on last edited by
              #5

              @SGaist Thank you. But my point is i have de referenced it at function call as below:

              foo(&stringdat);
              

              The output i get is as follows :

              Data is  0x65fe9c
              

              However i was expecting

              Data is Hello Qt
              

              Please help me out here. I'm new

              eyllanescE 1 Reply Last reply
              0
              • D derric

                @SGaist Thank you. But my point is i have de referenced it at function call as below:

                foo(&stringdat);
                

                The output i get is as follows :

                Data is  0x65fe9c
                

                However i was expecting

                Data is Hello Qt
                

                Please help me out here. I'm new

                eyllanescE Offline
                eyllanescE Offline
                eyllanesc
                wrote on last edited by
                #6

                @derric use qDebug() << "Data is " << *data;

                If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

                D 1 Reply Last reply
                2
                • D derric

                  I have following code:

                  void check()
                  {
                      QString stringdat = "Hello Qt";
                      foo(&stringdat);
                  }
                  
                  void foo(void* ptr)
                  {
                      QString* data = (QString*) ptr; 
                  
                       if(data != NULL)
                      {
                           qDebug() << "Data is " << data; 
                      }
                  
                  }
                  

                  However the challenge is :

                  QString* data = (QString*) ptr;
                  

                  How do it achieve the void* to QString conversion ?

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

                  @derric said in void* to QString C++:

                  QString* data = (QString*) ptr; 
                  qDebug() << "Data is " << data; 
                  

                  As @eyllanesc has said: since data is a QString *, if you output just that you get a pointer value, like 0x65fe9c. If you want to see what it points to, you need qDebug() << "Data is " << *data;, which dereferences the pointer and will print the QString, which is Hello Qt here.

                  There is nothing special in this context because you chose to pass the (address of the) original QString as a void * here.

                  As I'm sure you are aware, if you use that C-style cast (QString*) ptr and the ptr passed in does not point to a QString, disaster will occur when you go *data! :)

                  D 1 Reply Last reply
                  2
                  • eyllanescE eyllanesc

                    @derric use qDebug() << "Data is " << *data;

                    D Offline
                    D Offline
                    derric
                    wrote on last edited by
                    #8

                    @eyllanesc Thanks a ton

                    1 Reply Last reply
                    0
                    • JonBJ JonB

                      @derric said in void* to QString C++:

                      QString* data = (QString*) ptr; 
                      qDebug() << "Data is " << data; 
                      

                      As @eyllanesc has said: since data is a QString *, if you output just that you get a pointer value, like 0x65fe9c. If you want to see what it points to, you need qDebug() << "Data is " << *data;, which dereferences the pointer and will print the QString, which is Hello Qt here.

                      There is nothing special in this context because you chose to pass the (address of the) original QString as a void * here.

                      As I'm sure you are aware, if you use that C-style cast (QString*) ptr and the ptr passed in does not point to a QString, disaster will occur when you go *data! :)

                      D Offline
                      D Offline
                      derric
                      wrote on last edited by
                      #9

                      @JonB Thank you for the explanation. that really helped me realize the problem in detail

                      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