Search code examples
qt4qlist

Moving an object from one QList to another


I have two arrays that holds active and inactive QWebPage respectively, that is, I want to limit the maximum running QWebPages, so I did this,

I use two QList object,

QList<QWebPage*> pages;  // holds remaining pages
QList<QWebPage*> active; // holds active ones

When I need to send a request, I do:

void XXX::addRequest (QUrl url)
{
    if (pages.size() > 0)
    {
        QWebPage *page = pages.front();
        pages.pop_front();
        active.push_back (page);

        page->mainFrame().load (url);
    }
}

On request finish,

void XXX::finished (bool ok)
{
    QWebPage *page = qobject_cast<QWebPage*> (sender());

    // blabla

    // now I need to move this "page" to the "pages" list
}

Since an object can belong to multiple list (it's just a pointer), I should be removing it from the active QList and append it to the pages list,

But QList only support deletion by index numbers, I can't do that directly now.

Any ideas? Hopefully not iterating through the list.


Solution

  • QList does support removing items by item value, not only by item index. There are member functions removeOne() and removeAll(). In your case, removeOne() is the right choice.