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 create Array of objects
Forum Updated to NodeBB v4.3 + New Features

How to create Array of objects

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 6 Posters 20.3k 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.
  • E Offline
    E Offline
    Engelard
    wrote on 9 Aug 2018, 02:56 last edited by Engelard 8 Sept 2018, 03:23
    #1

    My custom class for QTableWidgetItem's inherit QTableWidgetItem(obviously), i need to create objects from it somehow. I've made custom class with intention to store there ID of every object and other stuff in future.

    I need to create in my main class array of that custom objects, and it's size undefined and elements(objects) should be added in runtime.
    If it would be array of Int's or Strings - would be easy, i'm done that simple before, but here is dynamic array of objects.

    Declaration in header file:

    mListItem *Item[];
    
    D J 2 Replies Last reply 9 Aug 2018, 03:31
    0
    • E Engelard
      9 Aug 2018, 02:56

      My custom class for QTableWidgetItem's inherit QTableWidgetItem(obviously), i need to create objects from it somehow. I've made custom class with intention to store there ID of every object and other stuff in future.

      I need to create in my main class array of that custom objects, and it's size undefined and elements(objects) should be added in runtime.
      If it would be array of Int's or Strings - would be easy, i'm done that simple before, but here is dynamic array of objects.

      Declaration in header file:

      mListItem *Item[];
      
      D Offline
      D Offline
      Devopia53
      wrote on 9 Aug 2018, 03:31 last edited by
      #2

      @Engelard

      Use container classes like QList or QVector.
      like this:
      QList<mListItem *> Item;

      1 Reply Last reply
      6
      • E Engelard
        9 Aug 2018, 02:56

        My custom class for QTableWidgetItem's inherit QTableWidgetItem(obviously), i need to create objects from it somehow. I've made custom class with intention to store there ID of every object and other stuff in future.

        I need to create in my main class array of that custom objects, and it's size undefined and elements(objects) should be added in runtime.
        If it would be array of Int's or Strings - would be easy, i'm done that simple before, but here is dynamic array of objects.

        Declaration in header file:

        mListItem *Item[];
        
        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 9 Aug 2018, 04:48 last edited by
        #3

        @Engelard To add to @Devopia53 take a look at http://doc.qt.io/qt-5/containers.html
        Normal C/C++ arrays have fix size by definition.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        5
        • E Offline
          E Offline
          Engelard
          wrote on 9 Aug 2018, 14:27 last edited by Engelard 8 Sept 2018, 14:29
          #4

          Thanks for both of you. I choose(Qt doc. recommend) QVector, but i dont get only one thing, and it does'nt mentioned in documentation. How add new elements to my array?

          I mean, i have a function, which should create new item in my QVector, it must create it in element position [1](means second slot), but the size of QVector is [1](by default as i understand). So program compiles well, i run the program, and when i press button - that function(which must create new element in array) try to create it like 10 times, so i'm getting 9 pop-up error dialogs in my screen.

          in header:

          QVector<mListItem*> itemVal;
          

          In CPP file, inside that function:

          itemVal[valID] = new mListItem();
          itemVal.at(valID)->setText(QString::number(val));
          itemVal.at(valID)->setTextAlignment(Qt::AlignRight);
          
          ui->tableWidMemory->setItem(MWindow::tabRows, 1, itemVal.at(valID));
          

          For creating new element i can't use .at() because it is for read only as were said in documentation.

          P.S. if someone dont understand, that pop-up critical window errors said that "index is out of range"

          1 Reply Last reply
          0
          • V Offline
            V Offline
            VRonin
            wrote on 9 Aug 2018, 14:32 last edited by
            #5
            mListItem* tempItem =  new mListItem;
            tempItem->setText(QString::number(val));
            tempItem->setTextAlignment(Qt::AlignRight);
            itemVal.append(tempItem);
            
            ui->tableWidMemory->setItem(MWindow::tabRows, 1, tempItem);
            

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            E 1 Reply Last reply 9 Aug 2018, 15:15
            2
            • V VRonin
              9 Aug 2018, 14:32
              mListItem* tempItem =  new mListItem;
              tempItem->setText(QString::number(val));
              tempItem->setTextAlignment(Qt::AlignRight);
              itemVal.append(tempItem);
              
              ui->tableWidMemory->setItem(MWindow::tabRows, 1, tempItem);
              
              E Offline
              E Offline
              Engelard
              wrote on 9 Aug 2018, 15:15 last edited by
              #6

              @VRonin It's not working, i need not just create object in that function, i need to create it in array..

              M J 2 Replies Last reply 9 Aug 2018, 15:16
              0
              • E Engelard
                9 Aug 2018, 15:15

                @VRonin It's not working, i need not just create object in that function, i need to create it in array..

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 9 Aug 2018, 15:16 last edited by
                #7

                @Engelard
                Hi
                itemVal.append(tempItem);
                addes it to the list.
                it has size zero to begin with.
                so append addes new mListItem* element

                1 Reply Last reply
                1
                • E Engelard
                  9 Aug 2018, 15:15

                  @VRonin It's not working, i need not just create object in that function, i need to create it in array..

                  J Offline
                  J Offline
                  JonB
                  wrote on 9 Aug 2018, 15:26 last edited by JonB 8 Sept 2018, 15:26
                  #8

                  @Engelard
                  Look at documenation http://doc.qt.io/archives/qt-5.5/qlist.html#append to see how that method appends items to a list/array.

                  1 Reply Last reply
                  1
                  • E Offline
                    E Offline
                    Engelard
                    wrote on 9 Aug 2018, 15:29 last edited by
                    #9

                    Yeah, i already figure out that append() function must be, so i experimented and it's now working and looks like that:

                    mListItem *temp = new mListItem();
                    itemVal.append(temp);
                    
                    1 Reply Last reply
                    0

                    2/9

                    9 Aug 2018, 03:31

                    topic:navigator.unread, 7
                    • Login

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