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. [SOLVED]QByteArray, pointer as data

[SOLVED]QByteArray, pointer as data

Scheduled Pinned Locked Moved General and Desktop
14 Posts 6 Posters 9.7k 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.
  • J Offline
    J Offline
    Jake007
    wrote on 21 Sept 2012, 17:13 last edited by
    #1

    Hi!

    I'm looking for a way how to send a pointer via QByteArray class ( sent with drag and drop event).

    I can't get correct value when casting data back to pointer.
    In current (working) code I'm converting address to string, and on the other side back to pointer. Which is extremely slow, and don't even want to know how stable in real life ( it hasn't crashed, yet).

    If someone could please provide code snippet/required methods or recommend an alternative to my problem.

    I've tried with QByteArray::fromRawData(), QByteArray::simplified(), reading with QDataStream and a few other ways, but all unsuccessful.

    And after long summer of C# I'm finally back at C++ and Qt :)

    Regards,
    Jake


    Code is poetry

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mohsen
      wrote on 22 Sept 2012, 04:14 last edited by
      #2

      i don't know what's really the aim doing such unstable job since pointers would be recreated or deleted anytime. anyway you may do something like this
      @
      QWidget *SOURCE=new QWidget();

      quintptr address=(quintptr)SOURCE;
      
      QByteArray b(QString::number(address).toAscii());
      
      QWidget *recover=(QWidget*)b.toULongLong();
      recover->show();
      

      @

      Edited: int data type has changed to quintptr to avoid problems on different platforms

      1 Reply Last reply
      0
      • J Offline
        J Offline
        JKSH
        Moderators
        wrote on 22 Sept 2012, 04:28 last edited by
        #3

        Could you explain your needs, and why you want to convert the pointer into bytes?

        Personally, I don't think QByteArray is a good choice. For one thing, you need to cast a pointer (which is essentially an integer) into an array of chars, like Mohsen has shown; there are nicer ways to store a pointer. Also, pointers can have different sizes on different machines -- usually 4 bytes on a 32-bit PC and 8 bytes on a 64-bit PC, but these sizes aren't guaranteed.

        If you need to send multiple pointers at the same time, I recommend storing them in a QVector, as no casting is required. If you only need to send one pointer at a time, you could just send the pointer directly.

        QVector example:
        @
        MyObject *obj1 = new MyObject();
        MyObject *obj2 = new MyObject();
        MyObject *obj3 = new MyObject();

        QVector<MyObject*> vectorOfPointers;
        vectorOfPointers << obj1 << obj2 << obj3;

        ...

        MyObject* retrievedObj1 = vectorOfPointers[0];
        MyObject* retrievedObj2 = vectorOfPointers[1];
        MyObject* retrievedObj3 = vectorOfPointers[2];
        @

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        1 Reply Last reply
        0
        • D Offline
          D Offline
          DerManu
          wrote on 22 Sept 2012, 11:49 last edited by
          #4

          [quote author="Mohsen" date="1348287297"]i don't know what's really the aim doing such unstable job since pointers would be recreated or deleted anytime. anyway you may do something like this
          @ int address=(int)SOURCE;
          @[/quote]

          This will lead to problems on different architectures (32-/64-Bit). Use quintptr instead.

          1 Reply Last reply
          0
          • J Offline
            J Offline
            Jake007
            wrote on 22 Sept 2012, 12:09 last edited by
            #5

            bq. Could you explain your needs, and why you want to convert the pointer into bytes? bq.

            I need it because data will be sent via drag and drop event, where data is sent as QByteArray.

            This pointers will stay in memory as long as the program remains open. So there won't be a problem with pointers being created or deleted.

            And there shouldn't be a problem in different architectures. Program compiled as 32-bit will be 32-bit even on 64-bit machines.

            @Mohsen
            You solution works perfectly. Thanks!

            Regards,
            Jake


            Code is poetry

            1 Reply Last reply
            0
            • D Offline
              D Offline
              DerManu
              wrote on 22 Sept 2012, 12:30 last edited by
              #6

              [quote author="Jake007" date="1348315792"]
              And there shouldn't be a problem in different architectures. Program compiled as 32-bit will be 32-bit even on 64-bit machines.
              @Mohsen
              You solution works perfectly. Thanks![/quote]

              Well "there shouldn't be a problem" says it all. Why introduce the potential to fail in future releases when you decide you might want native 64-bit applications, in a time where you've long forgotten about how you pass those pointers around. Just use quintptr like I proposed. Everything else stays the same, it just has benefits: it's much more future-proof and the programmer's intention is more clear than with int.

              1 Reply Last reply
              0
              • J Offline
                J Offline
                Jake007
                wrote on 22 Sept 2012, 12:47 last edited by
                #7

                Because int in 64-bit compile is 8 Bytes large :). As much as a pointer.

                And I considered your proposal the first time and used it in Mohsen's solution :).

                Regards,
                Jake


                Code is poetry

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  tobias.hunger
                  wrote on 22 Sept 2012, 13:18 last edited by
                  #8

                  Actually IIRC a int is defined to be at least 16bit long... so storing a pointer in an int is relying on undefined behavior -- even on 32bit machines!

                  Relying on undefined behavior is giving the compiler a free hand to break (== optimize) your code however it sees fit! So even if your code works today, it might break whenever you upgrade your compiler...

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    twsimpson
                    wrote on 22 Sept 2012, 13:25 last edited by
                    #9

                    What size int is and what the machine word length is are unrelated. It just so happened that "int" happens to be 32bit in most common architectures, but "quintptr":http://qt-project.org/doc/qt-4.8/qtglobal.html#quintptr-typedef is "guaranteed to be the same size as a pointer on all platforms supported by Qt."

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      mohsen
                      wrote on 22 Sept 2012, 14:15 last edited by
                      #10

                      [quote author="Jake007" date="1348315792"]
                      @Mohsen
                      You solution works perfectly. Thanks!
                      [/quote]
                      you're welcome

                      [quote author="DerManu" date="1348314572"]
                      This will lead to problems on different architectures (32-/64-Bit). Use quintptr instead.[/quote]
                      you're right. that was only an example. you would try it with quintptr

                      1 Reply Last reply
                      0
                      • D Offline
                        D Offline
                        DerManu
                        wrote on 22 Sept 2012, 16:57 last edited by
                        #11

                        [quote author="Jake007" date="1348318060"]Because int in 64-bit compile is 8 Bytes large :). As much as a pointer.

                        And I considered your proposal the first time and used it in Mohsen's solution :).

                        Regards,
                        Jake[/quote]

                        Whatever. If people deliberately chose to write bad software, I can't change it, and it explains alot of what's going on in the software world. Your assumption about ints on 64 bit is wrong. See (L)LP64 data model.

                        1 Reply Last reply
                        0
                        • J Offline
                          J Offline
                          Jake007
                          wrote on 22 Sept 2012, 17:54 last edited by
                          #12

                          It's not an assumption. sizeof(int) wrote out 8. Unless I did something wrong (button run instead of build and run etc... happens to me a lot lately).

                          I did use quintptr in the first place as suggested. I also wrote that I used it ( unless I awkwardly expressed myself).
                          And I don't see the problem with "deliberately".


                          Code is poetry

                          1 Reply Last reply
                          0
                          • D Offline
                            D Offline
                            DerManu
                            wrote on 22 Sept 2012, 18:00 last edited by
                            #13

                            [quote author="Jake007" date="1348336462"]It's not an assumption. sizeof(int) wrote out 8. Unless I did something wrong (button run instead of build and run etc... happens to me a lot lately).[/quote]
                            Then you were on a system that uses ILP64 (int, long, pointer are 64 bit). Most 64-bit systems use LP64 (long, pointer are 64 bit) or even LLP64 (longlong, pointer are 64 bit) though.

                            [quote author="Jake007" date="1348336462"]I did use quintptr in the first place as suggested. I also wrote that I used it ( unless I awkwardly expressed myself)[/quote]
                            Excellent :). I misunderstood it as "I considered your suggestion but used Mohsen's original suggestion with int instead".

                            1 Reply Last reply
                            0
                            • J Offline
                              J Offline
                              Jake007
                              wrote on 22 Sept 2012, 18:24 last edited by
                              #14

                              I just tried sizeof(int) again ( on my computer this time) under x64 compile and the result was 4.
                              It's possible that I was on ILP64 system, or that it was my mistake. Probably second. Today I first heard about LP models.


                              Code is poetry

                              1 Reply Last reply
                              0

                              2/14

                              22 Sept 2012, 04:14

                              12 unread
                              • Login

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