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. How to use QList<*> * object with [] operator ?
Forum Updated to NodeBB v4.3 + New Features

How to use QList<*> * object with [] operator ?

Scheduled Pinned Locked Moved General and Desktop
10 Posts 5 Posters 5.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.
  • T3STYT Offline
    T3STYT Offline
    T3STY
    wrote on last edited by
    #1

    operator. I have been able to use this feature when I had an object like QList<pToObject *> myList. But instead, when I have a pointer to such a QList I can't seem to get it done.

    Consider the next code:
    @#include <QApplication>
    #include <QList>

    struct test{
    int a;
    int b;
    };

    int main(int argc, char *argv[]){
    QApplication app(argc, argv);

    // QList<pToObject * > * pToMyList;
    QList<test *> * values;
    for (int i=0; i<5; i++){
    test * ps = new test;
    ps->a = i;
    ps->b = i+1;
    values->append(ps);
    }

    qDebug("%d", values[2]->a);
    return app.exec();
    }@
    When I run the code above I run into:
    @main.cpp 21: error: base operand of '->' has non-pointer type 'QList<test*>'@

    operator when dealing with a pointer. Dereferencing the object ( *values[ 2 ]->a ) throws an error as well.

    Could someone please show me the way?

    p.s. I know I could use QList.at() but this doesn't allow me to write on the object. QList.takeAt() is not usable since it detaches the item from the QList, while instead I need to make changes but still leave the object in the QList.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      Arnaut
      wrote on last edited by
      #2

      You should use

      @(*values)[2]->a@

      or

      @values->operator->a@

      but first notice that in your code the values pointer is never initialized.

      Hope it helps,
      H.

      1 Reply Last reply
      0
      • JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on last edited by
        #3

        Hi,

        [quote]when I have a pointer to such a QList I can’t seem to get it done.[/quote]That's because [] performs arithmetic on pointers.

        @
        // Initialize a char pointer:
        char* string = ...

        char c;
        // The following two lines are equivalent:
        c = string[5];
        c = *(string+5);
        @

        So, in your code,
        @values[2]@
        is like calling
        @*(values+2)@

        What is your goal for using a pointer?

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

        1 Reply Last reply
        0
        • T3STYT Offline
          T3STYT Offline
          T3STY
          wrote on last edited by
          #4

          To Arnaut: my mistake, I forgot the initialization part which I have corrected while writing this post (but I didn't update the post... :D). Anyway, thank you very much, that works just fine!

          To: JKSH: my goal is to store many struct pointers in a class. so basically I need a QList pointer in my class header before I can use it:
          @ struct Obj{ ... };
          class myclass{
          ...
          private:
          QList<Obj * > * pMyList;
          }@
          During execution I load objects in pMyList using append(). But I often need to modify the values inside the saved structs, so I can't use at() and takeAt() for the reasons explained before.
          Do you think there is a better alternative than Arnaut's one?

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

            Hi,

            What JKSH meant is why is pMyList a pointer ? There's no need for that, just use

            @QList<Obj *> myList;@

            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
            • T3STYT Offline
              T3STYT Offline
              T3STY
              wrote on last edited by
              #6

              I'm trying to prevent stack overflow problems since I know the QList is going to be pretty heavy... although, I read now in the docs that QList stores items as pointers to data so it wouldn't be such a big issue...

              1 Reply Last reply
              0
              • JKSHJ Offline
                JKSHJ Offline
                JKSH
                Moderators
                wrote on last edited by
                #7

                [quote author="T3STY" date="1392118794"]I'm trying to prevent stack overflow problems since I know the QList is going to be pretty heavy... although, I read now in the docs that QList stores items as pointers to data so it wouldn't be such a big issue...[/quote]All of Qt's containers (QList, QString, QMap, etc.) take up a very tiny amount of stack space. All of their data are stored on the heap.

                Like SGaist said, there is no need to use pointers to QLists.

                Some people use pointers to -share a list between two objects- avoid copying data when letting two objects read the same list. This is not required (and should be avoided), because copying a QList is very cheap too. The data is "implicitly shared":http://qt-project.org/doc/qt-5/implicit-sharing.html . Anyway, const references should be used for this purpose.

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

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mmoll
                  wrote on last edited by
                  #8

                  [quote author="JKSH" date="1392119103"]Some people use pointers to share a list between two objects. This is not required (and should be avoided), because copying a QList is very cheap too. The data is "implicitly shared":http://qt-project.org/doc/qt-5/implicit-sharing.html[/quote]

                  One should be aware though that copy-on-write will make these independent objects as soon as you modify one of the lists, at which point the two lists will be distinct and different. This is a fundamental difference to using pointers to a shared list.

                  1 Reply Last reply
                  0
                  • T3STYT Offline
                    T3STYT Offline
                    T3STY
                    wrote on last edited by
                    #9

                    Thank you for the advice, JKSH. You're right, I don't really need pointers there. Also, thank you mmoll for pointing out the behaviour when copying :-)

                    1 Reply Last reply
                    0
                    • JKSHJ Offline
                      JKSHJ Offline
                      JKSH
                      Moderators
                      wrote on last edited by
                      #10

                      Thanks mmol. I shouldn't have said "share" -- I meant "avoid copying data"

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

                      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