Results 1 to 4 of 4

Thread: rewriting code QTreeWIDGET

  1. #1
    Join Date
    Oct 2016
    Posts
    61
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default rewriting code QTreeWIDGET

    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>:perator[]: "index out of range".
    Qt Code:
    1. ui->arbolConectados->findItems(barejid,Qt::MatchExactly)[0]
    To copy to clipboard, switch view to plain text mode 

    Code working:
    Qt Code:
    1. void MainWindow::cambioRoster(QString barejid, QString resource)
    2. {
    3. if(cliente.rosterManager().isRosterReceived() == true)
    4. {
    5. QXmppPresence::Type a = cliente.rosterManager().getPresence(barejid,resource).type();
    6. QIcon online;
    7. online.addFile(":/icons/user-online.png");
    8. if(ui->arbolConectados->findItems(barejid,Qt::MatchExactly).size() == 0)
    9. {
    10. item->setText(0,barejid);
    11. ui->arbolConectados->addTopLevelItem(item);
    12. }
    13. if(a == QXmppPresence::Available)
    14. {
    15. item->setText(0,resource);
    16. item->setIcon(0,online);
    17. ui->arbolConectados->findItems(barejid,Qt::MatchExactly)[0]->addChild(item);
    18. }else
    19. {
    20. int i=0;
    21. while(i<ui->arbolConectados->findItems(barejid,Qt::MatchExactly)[0]->childCount() && ui->arbolConectados->findItems(barejid,Qt::MatchExactly)[0]->child(i)->text(0) != resource) //busqueda lineal
    22. {
    23. i++;
    24. }
    25. ui->arbolConectados->findItems(barejid,Qt::MatchExactly)[0]->takeChild(i);
    26. if(ui->arbolConectados->findItems(barejid,Qt::MatchExactly)[0]->childCount() == 0)
    27. {
    28. online.addFile(":/icons/user-offline.png");
    29. }
    30. }
    31. ui->arbolConectados->findItems(barejid,Qt::MatchExactly)[0]->setIcon(0,online);
    32. }
    33. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: rewriting code QTreeWIDGET

    QTreeWidgetItem*, i.e. pointer to QTreeWidgetItem.

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

    Cheers,
    _

  3. #3
    Join Date
    Oct 2016
    Posts
    61
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: rewriting code QTreeWIDGET

    So i need to do one pointer of my pointer?
    Can you show me one example of your idea? thx.

  4. #4
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    506
    Thanks
    11
    Thanked 76 Times in 74 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: rewriting code QTreeWIDGET

    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

Similar Threads

  1. Replies: 8
    Last Post: 22nd June 2015, 21:24
  2. Replies: 0
    Last Post: 21st October 2013, 09:16
  3. Sample code to use QTreeWidget and QStackWidget
    By Jeff100 in forum Qt Programming
    Replies: 1
    Last Post: 22nd December 2010, 08:05
  4. can't select item in a QTreeWidget via code
    By zack in forum Qt Programming
    Replies: 1
    Last Post: 14th July 2009, 20:31
  5. Replies: 2
    Last Post: 17th March 2008, 12:53

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.