Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Adding item to beginning of QListWidget doesn't work for me [Solved]

    General and Desktop
    2
    3
    3759
    Loading More Posts
    • 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.
    • J
      janfaroe last edited by

      Hi all. Hopefully I'm doing something completely wrong here - maybe someone could be friendly enough pointing out where.

      I have a QListWidget. I want to prepend items in the list widget. Docs tells me that QListWidget::insertItem is my friend. I insert an item thusly, ie. at list index 0:

      @QListWidgetItem* widgetItem = new QListWidgetItem(ui->listWidget);
      widgetItem->setText(el->getMediaItem()->getTitle());
      ui->listWidget->insertItem(0, widgetItem);
      qDebug() << "Inserting item at index 0 (hopefully): " << widgetItem->text();
      qDebug() << "Item at index 0: " << ui->listWidget->item(0)->text();
      qDebug() << "Index for inserted widget: " << ui->listWidget->row(widgetItem);@

      The debug statements print out the following:

      Inserting item at index 0 (presumably): "Retarded Tests"
      Item at index 0: "Color Blindness test - Real !"
      Index for inserted widget: 44

      As you can see, inserting at index 0 does not give me the desired result. It happily inserts at the bottom of the list. I tried inserting at index -1 instead, same result. On a side note, I am setting an item widget for each list entry, but this should have any effect on the way an item is inserted?

      Any clues?

      Edit: Qt 5.1, tested in Mac OS and Windows

      1 Reply Last reply Reply Quote 0
      • D
        dbzhang800 last edited by

        Please remove ui->listWidget from your following statement.

        @
        QListWidgetItem* widgetItem = new QListWidgetItem(ui->listWidget);
        @

        Read the documentation of QListWidgetItem's contructor and QListWidget::addItem, you can find the answer.

        1 Reply Last reply Reply Quote 0
        • J
          janfaroe last edited by

          Aha! It works! You're right, the doc for QListWidgetItem's constructor says that the item is added if it has a parent.

          May thanks! :-)

          1 Reply Last reply Reply Quote 0
          • First post
            Last post