Results 1 to 5 of 5

Thread: Finding an specific item in QTreeWidget by its text

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Finding an specific item in QTreeWidget by its text

    This:
    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3.  
    4.  
    5. int main(int argc, char *argv[])c++ singelton
    6. {
    7. QApplication app(argc, argv);
    8.  
    9. tree.setColumnCount(3);
    10.  
    11. QFile data("test.txt");
    12. if (data.open(QIODevice::ReadOnly)) {
    13. QTextStream s(&data);
    14. while (!s.atEnd()) {
    15. QString leituraall = s.readLine();
    16. QString vr_data = leituraall.section('\t',0,0);
    17. QString vr_semana = leituraall.section('\t',1,1);
    18. QString vr_hora = leituraall.section('\t',2,2);
    19. QString vr_smalldescription = leituraall.section('\t',3,3);
    20.  
    21. // Look for the date in column zero
    22. QList<QTreeWidgetItem*> items = tree.findItems(vr_data, Qt::MatchExactly, 0);
    23. if (items.count() == 0) { // not found, create new entry
    24. group = new QTreeWidgetItem(&tree);
    25. group->setText(0, vr_data);
    26. }
    27. else
    28. group = items.at(0);
    29.  
    30. // create the child entry
    31. QTreeWidgetItem *child = new QTreeWidgetItem(group);
    32. child->setText(0, vr_semana);
    33. child->setText(1, vr_hora);
    34. child->setText(2, vr_smalldescription);
    35. }
    36. }
    37.  
    38. tree.show();
    39.  
    40. return app.exec();
    41. }
    To copy to clipboard, switch view to plain text mode 
    I have dispensed with the separate QStringList of dates already seen, the code that creates the child is not repeated, no goto. Or even this:
    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3.  
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication app(argc, argv);
    8.  
    9.  
    10. tree.setColumnCount(3);
    11.  
    12. QFile data("test.txt");
    13. if (data.open(QIODevice::ReadOnly)) {
    14. QTextStream s(&data);
    15. while (!s.atEnd()) {
    16. QStringList fields = s.readLine().split('\t');
    17.  
    18. // Look for the date in column zero
    19. QList<QTreeWidgetItem*> items = tree.findItems(fields.at(0), Qt::MatchExactly, 0);
    20. if (items.count() == 0) { // not found, create new entry
    21. group = new QTreeWidgetItem(&tree, fields.mid(0,1));
    22. }
    23. else
    24. group = items.at(0);
    25.  
    26. // create the child entry
    27. QTreeWidgetItem *child = new QTreeWidgetItem(group, fields.mid(1));
    28. }
    29. }
    30.  
    31. tree.show();
    32.  
    33. return app.exec();
    34. }
    To copy to clipboard, switch view to plain text mode 
    which dispenses with intermediate variables.

    If the second column in the data file contains any values that are identical to values in the first column this will fail.

    "goto" is not inherently evil. However, using "goto" in code has a long track record of producing unmaintainable spaghetti code because it allows the execution path to jump around arbitrarily (bowl of spaghetti). Using "goto" when there is a perfectly good, well defined alternative (while loop, break, continue, return) is not likely to win many friends.

  2. #2
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Finding an specific item in QTreeWidget by its text

    Quote Originally Posted by ChrisW67 View Post
    This:

    [...]

    which dispenses with intermediate variables.

    If the second column in the data file contains any values that are identical to values in the first column this will fail.

    "goto" is not inherently evil. However, using "goto" in code has a long track record of producing unmaintainable spaghetti code because it allows the execution path to jump around arbitrarily (bowl of spaghetti). Using "goto" when there is a perfectly good, well defined alternative (while loop, break, continue, return) is not likely to win many friends.
    ChrisW67,

    thanks a lot for taking your time to write those two codes. I will test them soon after I do some changes in the code.

    Also thanks for the explanation about the use of goto. In case you have time, could you please help me with another "goto situation" in this post? --> http://www.qtcentre.org/threads/4452...etter?p=202750

    Thanks!


    Momergil

Similar Threads

  1. Problem with Finding the Text
    By charlesprime in forum Qt Programming
    Replies: 4
    Last Post: 24th March 2011, 10:10
  2. Replies: 2
    Last Post: 4th December 2010, 08:09
  3. QTreeWidget get a text from child item
    By vcp in forum Qt Programming
    Replies: 1
    Last Post: 16th January 2009, 17:36
  4. Finding text on Text edit
    By jyoti kumar in forum Qt Programming
    Replies: 2
    Last Post: 18th May 2006, 13:20
  5. Replies: 1
    Last Post: 17th March 2006, 08:19

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.