QTreeWidget contacts in C ++, with QT and XMPP

0

Hello, I'm trying to make a list of all my contacts in my account. To do this, I will show those that are connected and not. I rely on the XMPP chat communication protocol. The code is as follows:

    void MainWindow::rosterRecibido()
{
    logado = true; //cuando recibimos el roster ya mostramos el frame con la lista de conectados
    ui->frameLogin->hide();
    ui->frameConexion->show();
    int i,j;
    QTreeWidgetItem *item = new QTreeWidgetItem();
    QStringList contactos = cliente.rosterManager().getRosterBareJids();
    for(i=0;i<contactos.length();i++)
    {
        item->setText(0,contactos[i]);
        QStringList recursos = cliente.rosterManager().getResources(contactos[i]);
        QIcon online;
        online.addFile(":/icons/user-offline.png");
        item->setIcon(0,online);
        for(j=0;j<recursos.length();j++)
        {
            item->addChild(new QTreeWidgetItem());
            item->child(j)->setText(0,recursos[j]);
            online.addFile(":/icons/user-online.png");
            item->setIcon(0,online);
            item->child(j)->setIcon(0,online);
            listaItems.append(item);

        }


    }
    ui->arbolConectados->addTopLevelItems(listaItems);
}

What happens to me that when I add a contact the previous one is deleted even though I add it to the list. I repeat is based on the XMPP system.

    
asked by Sergio Ramos 14.10.2016 в 18:11
source

1 answer

0

You are reusing the same item. Each item in the tree must be an object of type QTreeWidgetItem independent. I understand that the statement:

QTreeWidgetItem *item = new QTreeWidgetItem();

should go inside the loop for .

By the way, the parentheses are not necessary in the syntax of new :

QTreeWidgetItem *item = new QTreeWidgetItem;
    
answered by 16.10.2016 / 16:54
source