Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. How to create multiple QListWidgetItem instances dynamically in Qt?
Qt 6.11 is out! See what's new in the release blog

How to create multiple QListWidgetItem instances dynamically in Qt?

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
15 Posts 4 Posters 3.5k 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.
  • K Offline
    K Offline
    kishore_hemmady
    wrote on last edited by
    #1

    I'm able to create 1 instance below
    videoNewItem = new QListWidgetItem("videoNewItem", ui->video_listWidget);
    How to create multiple such objects?

    jsulmJ 1 Reply Last reply
    0
    • K kishore_hemmady

      I'm able to create 1 instance below
      videoNewItem = new QListWidgetItem("videoNewItem", ui->video_listWidget);
      How to create multiple such objects?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @kishore_hemmady I don't understand the question. You create other objects in the same way you create the one in your example. What does not work?

      videoNewItem1 = new QListWidgetItem("videoNewItem", ui->video_listWidget);
      videoNewItem2 = new QListWidgetItem("videoNewItem", ui->video_listWidget);
      videoNewItem3 = new QListWidgetItem("videoNewItem", ui->video_listWidget);
      ...
      

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

      1 Reply Last reply
      3
      • K Offline
        K Offline
        kishore_hemmady
        wrote on last edited by
        #3

        hi @jsulm
        i need to declare an array of object for the QListWidgetItem class;
        something like the below

        QListWidgetItem *nodeItem;

        nodeItem=new QListWidgetItem[NUMBERS]("videoNewItem", ui->video_listWidget);

        how can i create the array of object here with the parameterized constructor call???

        jsulmJ 1 Reply Last reply
        0
        • K kishore_hemmady

          hi @jsulm
          i need to declare an array of object for the QListWidgetItem class;
          something like the below

          QListWidgetItem *nodeItem;

          nodeItem=new QListWidgetItem[NUMBERS]("videoNewItem", ui->video_listWidget);

          how can i create the array of object here with the parameterized constructor call???

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by jsulm
          #4

          @kishore_hemmady Why do you want to have an array? You can use a QVector or QList:

          QList<QListWidgetItem*> items;
          for (int i = 0; i < 10; ++i)
              items.append(new QListWidgetItem(QString("videoNewItem_") + i, ui->video_listWidget));
          

          If you really want to have an array:

          QListWidgetItem* items[10];
          for (int i = 0; i < 10; ++i)
              items[i] = new QListWidgetItem(QString("videoNewItem_") + i, ui->video_listWidget);
          

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

          K 1 Reply Last reply
          3
          • J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            additionaly to what @jsulm said,

            a QlistWidgetItem is rather useless without QListWidget to put them into, and QListWidget has 3 functions to access any QListWidgetItem it contains:

            • QListWidgetItem * item(int row) const
            • QListWidgetItem * itemAt(const QPoint & p) const
            • QListWidgetItem * itemAt(int x, int y) const

            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            2
            • jsulmJ jsulm

              @kishore_hemmady Why do you want to have an array? You can use a QVector or QList:

              QList<QListWidgetItem*> items;
              for (int i = 0; i < 10; ++i)
                  items.append(new QListWidgetItem(QString("videoNewItem_") + i, ui->video_listWidget));
              

              If you really want to have an array:

              QListWidgetItem* items[10];
              for (int i = 0; i < 10; ++i)
                  items[i] = new QListWidgetItem(QString("videoNewItem_") + i, ui->video_listWidget);
              
              K Offline
              K Offline
              kishore_hemmady
              wrote on last edited by
              #6

              thank you @jsulm
              How can i access the Nth items in the list.

              jsulmJ 1 Reply Last reply
              0
              • K kishore_hemmady

                thank you @jsulm
                How can i access the Nth items in the list.

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @kishore_hemmady Why don't you read documentation? http://doc.qt.io/qt-5/qlist.html
                items[i]...

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

                K 1 Reply Last reply
                1
                • jsulmJ jsulm

                  @kishore_hemmady Why don't you read documentation? http://doc.qt.io/qt-5/qlist.html
                  items[i]...

                  K Offline
                  K Offline
                  kishore_hemmady
                  wrote on last edited by
                  #8

                  @jsulm
                  i am doing the same , its not showing error but crashing.
                  thats why asked is there any other method to do this.

                  K 1 Reply Last reply
                  0
                  • K kishore_hemmady

                    @jsulm
                    i am doing the same , its not showing error but crashing.
                    thats why asked is there any other method to do this.

                    K Offline
                    K Offline
                    kishore_hemmady
                    wrote on last edited by
                    #9

                    @jsulm

                    HI

                    this is my code for getting playlist for video files for different video zones.

                    QList<QListWidgetItem*> videoNewItem
                    for(INT32 i=0;i<i32NumberOfVideoZones;i++){
                                videoNewItem.append(new QListWidgetItem(QString("videoNewItem_") + i, ui->video_listWidget));
                            }
                    

                    void MainWindow::on_add_file_clicked()
                    {

                                 int row = 1;
                              videoNewItem[i32tContainerindex] = new QListWidgetItem(QString("videoNewItem_") + i32tContainerindex,    ui->video_listWidget);
                                    videoNewItem[i32tContainerindex]->setFlags (videoNewItem[i32tContainerindex]->flags() | Qt::ItemIsUserCheckable);
                                   videoNewItem[i32tContainerindex]->setCheckState (Qt::Unchecked);
                    
                                    videoNewItem[i32tContainerindex]->setText ((ui->video_file_name_lineEdit->text()));
                                    ui->video_listWidget->insertItem (row, videoNewItem[i32tContainerindex]);
                                    ui->video_listWidget->addItem (videoNewItem[i32tContainerindex]);
                                    ui->video_listWidget->show();
                                    ui->video_file_name_lineEdit->setText ("");
                                    ui->video_listWidget->addItem (videoNewItem[i32tContainerindex]);
                                    ui->video_listWidget->repaint();
                                    GUI_videostruct[i32tContainerindex].videoFileName=ui->video_listWidget->item(0)->text();
                    

                    }

                    but when i change the video zone from index 0 to 1, i ll retain the value(files) of videoNewItem[0] in videoNewItem[1] listWidget playlist
                    i want to get the different files for different zones.

                    jsulmJ 1 Reply Last reply
                    0
                    • K kishore_hemmady

                      @jsulm

                      HI

                      this is my code for getting playlist for video files for different video zones.

                      QList<QListWidgetItem*> videoNewItem
                      for(INT32 i=0;i<i32NumberOfVideoZones;i++){
                                  videoNewItem.append(new QListWidgetItem(QString("videoNewItem_") + i, ui->video_listWidget));
                              }
                      

                      void MainWindow::on_add_file_clicked()
                      {

                                   int row = 1;
                                videoNewItem[i32tContainerindex] = new QListWidgetItem(QString("videoNewItem_") + i32tContainerindex,    ui->video_listWidget);
                                      videoNewItem[i32tContainerindex]->setFlags (videoNewItem[i32tContainerindex]->flags() | Qt::ItemIsUserCheckable);
                                     videoNewItem[i32tContainerindex]->setCheckState (Qt::Unchecked);
                      
                                      videoNewItem[i32tContainerindex]->setText ((ui->video_file_name_lineEdit->text()));
                                      ui->video_listWidget->insertItem (row, videoNewItem[i32tContainerindex]);
                                      ui->video_listWidget->addItem (videoNewItem[i32tContainerindex]);
                                      ui->video_listWidget->show();
                                      ui->video_file_name_lineEdit->setText ("");
                                      ui->video_listWidget->addItem (videoNewItem[i32tContainerindex]);
                                      ui->video_listWidget->repaint();
                                      GUI_videostruct[i32tContainerindex].videoFileName=ui->video_listWidget->item(0)->text();
                      

                      }

                      but when i change the video zone from index 0 to 1, i ll retain the value(files) of videoNewItem[0] in videoNewItem[1] listWidget playlist
                      i want to get the different files for different zones.

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @kishore_hemmady Are you talking about changing i32tContainerindex from 0 to 1? Because I don't understand your description. Where and how do you change it? Are you sure it is actually changed?

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

                      1 Reply Last reply
                      0
                      • K Offline
                        K Offline
                        kishore_hemmady
                        wrote on last edited by
                        #11

                        yes I am changing i32tContainerindex from 0 to 1.
                        I am developing a page which contains listWidget, whenever i am switching from videoNewItem[0]'s listWidget to videoNewItem[1]
                        listWidget i am not able to show only videoNewItem[1] listWidget rather i am getting the content of both the listWidget

                        jsulmJ 1 Reply Last reply
                        0
                        • K kishore_hemmady

                          yes I am changing i32tContainerindex from 0 to 1.
                          I am developing a page which contains listWidget, whenever i am switching from videoNewItem[0]'s listWidget to videoNewItem[1]
                          listWidget i am not able to show only videoNewItem[1] listWidget rather i am getting the content of both the listWidget

                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @kishore_hemmady Sorry, I don't know your code, so I don't know what you're doing wrong. Did you try to debug your app to see what happens?

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

                          K 1 Reply Last reply
                          0
                          • jsulmJ jsulm

                            @kishore_hemmady Sorry, I don't know your code, so I don't know what you're doing wrong. Did you try to debug your app to see what happens?

                            K Offline
                            K Offline
                            kishore_hemmady
                            wrote on last edited by
                            #13
                            This post is deleted!
                            jsulmJ 1 Reply Last reply
                            0
                            • K kishore_hemmady

                              This post is deleted!

                              jsulmJ Offline
                              jsulmJ Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              @kishore_hemmady I already understood before, but how this picture can help me to find the issue? The issue is in your code...

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

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

                                Hi,

                                That looks like over engineering. Furthermore, you are calling insertItem and right after that addItem twice with exactly the same item. Then you also update your GUI_videostruct data using an index on one side and a hardcoded value on the other side.

                                You should rather explain what exactly you are trying to achieve. Also since you are using a QListWidget, there's no real need to keep a list of the item besides since you already have them available through QListWidget.

                                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
                                1

                                • Login

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