PDA

View Full Version : rewriting code QTreeWIDGET



SirJonas
16th October 2016, 11:28
Some way of save in one sentence this line? i am trying to save this code in one sentece to avoid to repeat all time. I tried creating one QTreeWidgetitem but show me this error: ASSERT failure in QList<T>::operator[]: "index out of range".

ui->arbolConectados->findItems(barejid,Qt::MatchExactly)[0]

Code working:

void MainWindow::cambioRoster(QString barejid, QString resource)
{
if(cliente.rosterManager().isRosterReceived() == true)
{
QXmppPresence::Type a = cliente.rosterManager().getPresence(barejid,resour ce).type();
QIcon online;
online.addFile(":/icons/user-online.png");
if(ui->arbolConectados->findItems(barejid,Qt::MatchExactly).size() == 0)
{
QTreeWidgetItem *item = new QTreeWidgetItem();
item->setText(0,barejid);
ui->arbolConectados->addTopLevelItem(item);
}
if(a == QXmppPresence::Available)
{
QTreeWidgetItem *item = new QTreeWidgetItem();
item->setText(0,resource);
item->setIcon(0,online);
ui->arbolConectados->findItems(barejid,Qt::MatchExactly)[0]->addChild(item);
}else
{
int i=0;
while(i<ui->arbolConectados->findItems(barejid,Qt::MatchExactly)[0]->childCount() && ui->arbolConectados->findItems(barejid,Qt::MatchExactly)[0]->child(i)->text(0) != resource) //busqueda lineal
{
i++;
}
ui->arbolConectados->findItems(barejid,Qt::MatchExactly)[0]->takeChild(i);
if(ui->arbolConectados->findItems(barejid,Qt::MatchExactly)[0]->childCount() == 0)
{
online.addFile(":/icons/user-offline.png");
}
}
ui->arbolConectados->findItems(barejid,Qt::MatchExactly)[0]->setIcon(0,online);
}
}

anda_skoa
16th October 2016, 17:31
QTreeWidgetItem*, i.e. pointer to QTreeWidgetItem.

If you get an assert at the index access, then the list returned by findItems is empty.

Cheers,
_

SirJonas
16th October 2016, 18:53
So i need to do one pointer of my pointer?
Can you show me one example of your idea? thx.

Ginsengelf
17th October 2016, 07:27
No, you need to check if ui->arbolConectados->findItems(barejid,Qt::MatchExactly).empty() returns true or false, and access [0] only if empty() returned false.

Ginsengelf