Important: Please read the Qt Code of Conduct -

How to create multiple QListWidgetItem instances dynamically in Qt?

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

  • Lifetime Qt Champion

    @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);

  • 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???

  • Lifetime Qt Champion

    @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);

  • Moderators

    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

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

  • Lifetime Qt Champion

    @kishore_hemmady Why don't you read documentation?

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

  • @jsulm


    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_file_name_lineEdit->setText ("");
                    ui->video_listWidget->addItem (videoNewItem[i32tContainerindex]);


    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.

  • Lifetime Qt Champion

    @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?

  • 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

  • Lifetime Qt Champion

    @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?

  • This post is deleted!

  • Lifetime Qt Champion

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

  • Lifetime Qt Champion


    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.

Log in to reply