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. QVector filled with pointers return hex value
Qt 6.11 is out! See what's new in the release blog

QVector filled with pointers return hex value

Scheduled Pinned Locked Moved General and Desktop
16 Posts 4 Posters 6.1k 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.
  • G Offline
    G Offline
    glararan
    wrote on last edited by
    #7

    Hm, but a isn't pointer in QVector

    because if I change "a" after append for ex.: to 10 it will show up 2. That's what I need, when I add it to QVector and then modify data type, it will show in QVector

    like
    @int a = 2;

    int* b = &a;

    a = 10;

    qDebug() << *b; // show 10@

    But in QVector

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

      Well ok, but C++ is a type-safe language so you can't have a pointer of type int* outside of the vector and of type QVariant* inside it, have them point to the same thing and still expect it to be writable by both of them safely.

      Essentially (omitting the vector) what you want is this:
      @
      int a;
      double* x = reinterpret_cast<double*>(&a);
      string* y = reinterpret_cast<string*>(&a);
      *x = 1.3;
      *y = "foo";
      @
      It makes no sense.

      Packing it into a QVariant is also not a solution, because QVariant doesn't hold a reference to a value, it makes a copy of that value. To make it work like you want, you need to ditch the original type and stick to the QVariant. This way it's the same type inside and outside of the vector, but you can still put values of different types in it
      @
      QVariant a(2);
      QVector<QVariant*> data;
      data.append(&a);
      a = 10;
      qDebug() << data.last()->toInt(); //prints 10
      a = QStringLiteral("foo");
      qDebug() << data.last()->toString(); //prints "foo"
      @

      1 Reply Last reply
      0
      • G Offline
        G Offline
        glararan
        wrote on last edited by
        #9

        Last code sounds good to me. Thanks

        1 Reply Last reply
        0
        • G Offline
          G Offline
          glararan
          wrote on last edited by
          #10

          Just last problem, I cannot use QVariant as default data type, when for example I get from GLWidget mouse position its in QVector2D not in QVariant, so if you know some good convertation from defualt data types to QVariant.

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

            Hi,

            You have an example for that in "QVariant's documentation":http://qt-project.org/doc/qt-4.8/qvariant.html#a-note-on-gui-types It also applies to other types.

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

            1 Reply Last reply
            0
            • G Offline
              G Offline
              glararan
              wrote on last edited by
              #12

              I'm currently trying:

              @ QVector<QVariant*> cont;

              int a = 2;
              
              cont.append(&QVariant::fromValue<int*>(&a));
              
              a = 10;
              
              qDebug() << cont.last()->value<int*>(); // return 0x6cfc64@
              

              And again, it's example, not final using! I'm just testing it.

              There must be something I missing all time.

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

                Again, you are printing the address not the content

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

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  glararan
                  wrote on last edited by
                  #14

                  When I use:
                  @qDebug() << cont.last()->toInt();@

                  Then it print 0, or it's again incorrect?

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

                    You're so lucky it prints 0 and doesn't crash.
                    You're making the same mistake as before, except worse ;)

                    First of all this
                    @
                    cont.append(&QVariant::fromValue<int*>(&a));
                    @

                    A pointer to QVariant that holds a pointer to int? Why not just store pointer to int then?

                    also this
                    @
                    QVariant::fromValue<int*>(&a)
                    @
                    creates a temporary QVariant variable, that is destroyed as soon as you hit ; in that line. You're taking an address of that temporary and then you try to de-reference it with cont.last()-> . This pointer is already invalid at that point. It is invalid as soon as append() returns.

                    What you keep getting wrong is what I said earlier - QVariant does not store a reference to another object. It holds a value itself. You can't modify another object by it's copy held in QVariant. It is also a bad idea to store pointer value(an address) in a QVariant. I mean "you can":http://blog.bigpixel.ro/2010/04/storing-pointer-in-qvariant but it's just not what it's for.

                    I keep having a feeling that you want to do something horrible with all that. What are you doing exactly? There definitely is a simpler solution to your problems.

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      glararan
                      wrote on last edited by
                      #16

                      OK, so what I'm doing?

                      Making function which will add to container data type with value

                      All from container will be printed into one string.

                      @QString finalstring;

                      for(int i = 0; i < cont.count(); i++)
                      {
                      finalstring += QString("%1 %2").arg(cont1[i]).arg(cont[i]);
                      }

                      emit ...(finalstring);@

                      cont1 mean vector with string values
                      cont mean vector with data types

                      simple function will add to containers

                      @void add(QString text, QVariant data)
                      {
                      cont1.append(text);
                      cont.append(..........);
                      }@

                      and I will just hit add function. I don't know how many times I will use it, that's why it cannot be constant.

                      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