Search code examples
c++qtqlistwidget

QListWidget add item at the bottom of the list


I'm making a messaging app using Qt5. I use a QListWidget to display the messages, and I want the message that the user sends to be added at the bottom of the list. I have tried this:

QString message_content = "new message";
messages_list.insertItem(0, message_content);

but the message still appeared at the top of the QListWidget. Using QListWidget::insertItem with index -1 did not work either, resulting in the messages appearing at the top like the screenshot below: enter image description here

The outcome that I want is to have the new message appear below the "this is an old item" item. How can I achieve this effect?


Solution

  • You have to use the addItem() method:

    messages_list.addItem(message_content);