Results 1 to 5 of 5

Thread: Finding an specific item in QTreeWidget by its text

  1. #1
    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 Finding an specific item in QTreeWidget by its text

    Hello!

    I'm having a work with QTreeWidget and I found a problem here in locating an item on it by one of its texts. The idea is the following:

    I created a QTreeWidget and I decided to read from a txt file some columns and create items and sub-items in the QTreeWidget with the data collected in the txt. The idea is that in each line of the txt there are some parameters base on date values (02/12/2011, e.g.). The algorithm is supposed to do a research in the QTreeWidget each time a new line is read in the txt and see if there is already an item related to that date. If there is not, a new item is created with that new date and the rest of the line data is used to create a child on that item. By the other hand, if an item with that date already exists, than instead of creating a new item with that date and a child with the rest, the algorithm should create only the child under the already existing item.

    But I'm having difficulties in creating a search mechanism to see if there is already an item related to that date. Everything else I already have. I tested the function QList<QTreeWidgetItem *> QTreeWidget::findItems ( const QString & text, Qt::MatchFlags flags, int column = 0 ) const (p.s.: where is the button for Qt functions and classes?), but its not working. By the moment, here is what I have:

    Qt Code:
    1. void AlarmManagment::readExistingAlarms()
    2. {
    3. v_rewr->openRead2("./Config/Alarms.txt");
    4.  
    5. name: leituraall = v_rewr->Read();
    6.  
    7. vr_data = leituraall.section('\t',0,0);
    8. vr_semana = leituraall.section('\t',1,1);
    9. vr_hora = leituraall.section('\t',2,2);
    10. vr_smalldescription = leituraall.section('\t',3,3);
    11.  
    12. if (!vector_String1.contains(vr_data))
    13. {
    14. vector_String1.push_back(vr_data);
    15. addRoot(vr_data, vr_semana, vr_hora, vr_smalldescription);
    16. qDebug() << "Item normal sendo colocado: " << vr_data;
    17. }
    18. else
    19. {
    20. a = ui->Listofalarms->findItems(vr_data,Qt::MatchExactly,1);
    21. qDebug() << "item ja exite: " << vr_data;
    22. addChild(a.front(),vr_semana,vr_hora,vr_smalldescription);
    23. }
    24.  
    25. if (!v_rewr->attheEnd()) goto name;
    26. }
    27.  
    28. void AlarmManagment::addRoot(QString data, QString semana, QString hora, QString smadesc)
    29. {
    30. QTreeWidgetItem *itm = new QTreeWidgetItem(ui->Listofalarms);
    31. itm->setText(0,data);
    32.  
    33. itm2->setText(0,semana);
    34. itm2->setText(1,hora);
    35. itm2->setText(2,smadesc);
    36.  
    37. itm->addChild(itm2);
    38. }
    39.  
    40. void AlarmManagment::addChild(QTreeWidgetItem *parent, QString sem, QString hour, QString smaldescription)
    41. {
    42. itm2->setText(0,sem);
    43. itm2->setText(1,hour);
    44. itm2->setText(2,smaldescription);
    45.  
    46. parent->addChild(itm2);
    47. }
    To copy to clipboard, switch view to plain text mode 

    while:
    Qt Code:
    1. QList<QTreeWidgetItem *> a;
    To copy to clipboard, switch view to plain text mode 

    I'm having problems in dealing with this QList of QTreeWidgetItem in terms of finding the item that has the date in question.

  2. #2
    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

    You put vr_data into column zero (line 31) but then go looking for it in column one (line 20)

    The whole routine seems much more complicated than it needs to be.

    BTW: Goto! Really!

  3. #3
    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
    You put vr_data into column zero (line 31) but then go looking for it in column one (line 20)

    The whole routine seems much more complicated than it needs to be.

    BTW: Goto! Really!
    Hello ChrisW67!

    First, thanks for the answer; it worked.

    Second, well, this is the unique routine that I could created. Do you have any idea of how to make the code better with the same functionality?

    And third, about the Go to, that was a necessity from some time ago in previous tests because the while() command wasn't working for some unidentified reason. Now that you spoke it, I noticed that I can already change to while.

    But in any case, what would be the problem with go to? :]


    Thanks!


    Momergil

  4. #4
    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.

  5. #5
    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.