Qlistwdiget insertitem sequence Problem

Source: Internet
Author: User

When using qlistwdiget inser for an item, the following code has a small problem.

listWidget->insertItem(row, new QListWidgetItem(listWidget))

This Code makes the insert Item always at the end of the listwidget. this is a very simple and frequently-used API. It is too simple to be simple, but unexpected problems have occurred. see the document "inserts the item at the position in the list given by row. "No nutrition. read the source code of qlistwidget and find the producer. The insert code snippet is as follows:

void QListModel::insert(int row, QListWidgetItem *item){    if (!item)        return;    item->view = qobject_cast<QListWidget*>(QObject::parent());    if (item->view && item->view->isSortingEnabled()) {        // sorted insertion        QList<QListWidgetItem*>::iterator it;        it = sortedInsertionIterator(items.begin(), items.end(),                                     item->view->sortOrder(), item);        row = qMax(it - items.begin(), 0);    } else {        if (row < 0)            row = 0;        else if (row > items.count())            row = items.count();    }    beginInsertRows(QModelIndex(), row, row);    items.insert(row, item);    item->d->theid = row;    endInsertRows();}
The original problem is very simple. During the insert operation, the system determines whether the item specifies the parent. if the parent is specified, the row is set to the maximum value. if no parent exists, the specified row is used. in my code, parent is specified during the new qlistwidgetitem operation, which causes this problem. modify the code

listWidget->insertItem(row, new QListWidgetItem())
Solve the problem!


Qlistwdiget insertitem sequence Problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.