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.0k 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
    #1

    Hi,

    I have little problem.

    I'm trying to store pointers in vector, but when I call vector, it returns hex values like 0xf2f744

    Code:
    @ QVector<QVariant*> cont;

    int ex = 2;
    
    cont.push_back(reinterpret_cast<QVariant*>(&ex));
    
    ex = 10;
    
    qDebug() << cont.last();@
    

    Thanks for help

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      What do you expect to see?
      You are printing the pointer's stored address.

      Vote the answer(s) that helped you to solve your issue(s)

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

        I expect to see 10, but I see as you said stored adress.

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

          This code is horrible. There's basically something wrong in every line.

          • QVariant is "implicitly shared":http://qt-project.org/doc/qt-5.0/qtcore/implicit-sharing.html class so there's little point in storing it by pointers instead of just storing by value. Also there's a QVariantList class which is usually what you actually want.
          • why do you create an int value just to horribly miss-use it in the next line with reinterpret_cast? Just do cont.append(2)
          • this reinterpret_cast is wrong! Don't do that. reinterpret_cast is usually a sign that "you're doing it wrong"™ and you are in this case. QVariant just acts like a union, it's not one. You're probably reinterpreting the internal d-pointer and that just begs for horrible crashes at some point
          • if you want to modify a value stored in a vector state it clearly: cont[whatever]=
          • if you want to get an int back from a QVariant there's a toInt() member on it. qDebug() << QVariant(2) will not print "2", it will print "QVariant(int, 2)"
          1 Reply Last reply
          0
          • G Offline
            G Offline
            glararan
            wrote on last edited by
            #5

            It was just example, without definition its useless

            I have created ten data types which I want to store in this list.
            So when I use constant val, it's useless, these data types will change every sec , and for my function where I don't know how many data types exists to add to this container.

            Do you understand me now? Container will just ref to data type

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

              Sorry, I don't really get it. You're using QVariant anyway. What is the additional indirection through a pointer for? You can store any data type in QVariant after registering it with qRegisterMetaType.

              @
              QVariantList data;
              data.append(2);
              data.append(QImage());
              data.append(MyOwnType());

              foreach(QVariant v, data)
              {
              switch(v.type())
              {
              case QMetaType::Int: qDebug() << v.toInt(); break;
              ...
              }
              }
              @
              What's wrong with that?
              Or better yet - if those types are related use a common interface and polymorphic dispatch and don't bother with QVariant.

              But if you really really want to store pointers here's how:
              @
              QVector<QVariant*> data;
              int a = 2;
              data.append(new QVariant(a));

              if(data.last()->type() == QMetaType::Int)
              qDebug() << data.last()->toInt();

              //remember to delete
              delete data.last();
              data.removeLast();
              @
              But that's as bad and ugly as it looks.

              1 Reply Last reply
              0
              • 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