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. Adding item to beginning of QListWidget doesn't work for me [Solved]
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 4.3k Views
  • 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 Offline
    J Offline
    janfaroe
    wrote on last edited by
    #1

    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
    0
    • D Offline
      D Offline
      dbzhang800
      wrote on last edited by
      #2

      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
      0
      • J Offline
        J Offline
        janfaroe
        wrote on last edited by
        #3

        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
        0

        • Login

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