Results 1 to 5 of 5

Thread: Rookie struggling with string manipulation and searching

  1. #1
    Join Date
    Sep 2010
    Location
    Ontario Canada
    Posts
    23
    Thanks
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Symbian S60 Maemo/MeeGo

    Default Rookie struggling with string manipulation and searching

    Hello All!
    I am building a dialog based app for symbian that teaches the user how to tie knots.
    I am using a listwidget in the main dialog as a menu that passes the selected knot name to the knot view dialog that iterates through the images and displays them with a QLabel. I also display a text with a description of the selected knot. I have managed to get all that working with the code below. Because some knots like the bowline and sheet bend have a couple of different variants I used a switch function to be sure I get the correct images and description, this all works great though I wonder if this is the correct approach?
    I would now like to iterate through the direction text's on a label below the displayed image.
    I can make it work by building a separate file for each direction and then iterate through them the same way I am with the images and description documents but it seems cumbersome and I am thinking there must be a better way.
    I can figure out how to use QString::indexOf(); to find key words in a document but is there a way to extract an entire sentence instead of just a word? That way I could just use one txt document for all the instructions...
    All constructive criticism is welcomed!
    If this code looks ridiculous go easy on me, I am learning C++ from a book...
    Qt Code:
    1. //recieve knot name and index of selected knot
    2. void knotViewDlg::getIndex(QString knot, int index)
    3. {
    4. //setup d with the path to the resource images
    5. QStringList result; //holds the image filenames
    6. QStringList description; //holds the txt filenames
    7. QDir d(":/knotImages/images");
    8. //populate result with all image names
    9. result = d.entryList();
    10. knot.remove(" "); //remove spaces from string
    11. //search result for key word
    12. QString str1 = knot; //store selected knot name for txts
    13. QString str;
    14. strLst = result.filter(knot, Qt::CaseInsensitive); //fill strLst with filtered images
    15.  
    16. //figure out which bowline or sheet bend was selected then setup to select proper image files
    17. if(strLst.contains("Bowline_0.gif", Qt::CaseInsensitive))
    18. {
    19. switch(index)
    20. {
    21. case 0:
    22. {
    23. knot = "Bowline_";
    24. break;
    25. }
    26. case 1:
    27. {
    28. knot = "Doublebowline_";
    29. break;
    30. }
    31. case 2:
    32. {
    33. knot = "Bowlineonabight_";
    34. break;
    35. }
    36.  
    37. default:
    38. break;
    39. };
    40. strLst = result.filter(knot); //setup strLst with the correct image files
    41. strLst.sort();
    42. }
    43.  
    44. if(strLst.contains("Sheetbend_0.gif", Qt::CaseInsensitive) || (description.contains("DoubleSheetbend_0.gif", Qt::CaseInsensitive)))
    45. {
    46. switch(index)
    47. {
    48. case 3:
    49. {
    50. knot = "Sheetbend_";
    51. break;
    52. }
    53. case 4:
    54. {
    55. knot = "Doublesheetbend_";
    56. break;
    57. }
    58. default:
    59. break;
    60. };
    61. strLst = result.filter(knot);
    62. strLst.sort();
    63. }
    64. strLst.sort();
    65. //set first image before calling timer
    66. str = strLst.at(0);
    67. ui->label->setPixmap(QPixmap(QString::fromUtf8(
    68. ":/knotImages/images/").append(str)));
    69. //set first instruction here?
    70.  
    71. //setup description page
    72. d = (":/images/docs/texts");
    73. result = d.entryList();
    74. description = (result.filter(str1, Qt::CaseInsensitive));
    75. //figure out which bowline or sheet bend was selected then setup to select proper text file
    76. if(description.contains("Bowline.txt", Qt::CaseInsensitive))
    77. {
    78. switch(index)
    79. {
    80. case 0:
    81. {
    82. description.first() = "Bowline.txt";
    83. break;
    84. }
    85. case 1:
    86. {
    87. description.first() = "DoubleBowline.txt";
    88. break;
    89. }
    90. case 2:
    91. {
    92. description.first() = "Bowlineonabight.txt";
    93. break;
    94. }
    95. default:
    96. break;
    97. };
    98. }
    99. if(description.contains("Sheetbend.txt", Qt::CaseInsensitive) || (description.contains("DoubleSheetbend.txt", Qt::CaseInsensitive)))
    100. {
    101. switch(index)
    102. {
    103. case 3:
    104. {
    105. description.first() = "SheetBend.txt";
    106. break;
    107. }
    108. case 4:
    109. {
    110. description.first() = "DoubleSheetbend.txt";
    111. break;
    112. }
    113. default:
    114. break;
    115. };
    116. }
    117.  
    118. //open appropriate file for descriptions
    119. str = (QString::fromUtf8(":/images/docs/texts/").append(description.at(0)));
    120. QFile inputFile(str);
    121. inputFile.open(QIODevice::ReadOnly);
    122. QTextStream in(&inputFile);
    123. QString line = in.readAll();
    124. inputFile.close();
    125. ui->textEdit->append(line);
    126. ui->tabWidget->setCurrentIndex(1);
    127. ui->textEdit->setFocus();
    128.  
    129. //open appropriate instructions here?
    130. }
    131. void knotViewDlg::changeImage()
    132. {
    133. QString str = strLst.at(counter);
    134. ui->label->setPixmap(QPixmap(QString::fromUtf8(
    135. ":/knotImages/images/").append(str)));
    136. counter++;
    137. if(counter >= (strLst.count()))
    138. {
    139. counter = 0;
    140. }
    To copy to clipboard, switch view to plain text mode 

    All constructive comments and help much appreciated!

    Cheers,
    Jon

  2. #2
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Rookie struggling with string manipulation and searching

    You might consider using XML. That way, you could separate portions of a single document with tags, and easily extract the contents of a single tagged portion.

    Or, you could do much the same by embedding tags in your own master document, searching it for whatever tag you choose ('##Tag_Start##', for example, closed with '##Tag_End##'). For a simple project, this may be a better approach than hauling in all of the XML machinery.

  3. #3
    Join Date
    Jan 2008
    Location
    Davao City, Philippines
    Posts
    77
    Thanks
    16
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Rookie struggling with string manipulation and searching

    Hi Jon,

    not sure I undrstand your question right ...

    Quote Originally Posted by Jon Heron View Post
    ... is there a way to extract an entire sentence instead of just a word?
    but may be this could be helpfull for you:

    Qt Code:
    1. QString QString::section ( QChar sep, int start, int end = -1, SectionFlags flags = SectionDefault ) const
    To copy to clipboard, switch view to plain text mode 

    A sentence starts with nothing but ends with '.' usually. so secton 0 will be the first, section 2 wil be the second sentence and so on ...

  4. #4
    Join Date
    Sep 2010
    Location
    Ontario Canada
    Posts
    23
    Thanks
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Symbian S60 Maemo/MeeGo

    Default Re: Rookie struggling with string manipulation and searching

    Thanks guys!

    gboelter, that is very helpful!

    So I guess I could setup 2 sections, one for the knot category and then have another for the individual directions, perhaps load them into a QStringList to iterate through with the images....
    Something like this maybe?
    Qt Code:
    1. QString instructions;
    2. instructions = line2.section(str1, 1, -1); //load instructions with the selected instructions
    3. QStringList inst; //will hold list of instructions
    4.  
    5. int tmp(instructions.count(str1, Qt::CaseInsensitive)); //count of instructions
    6. for(int i = 0; i < tmp; i++)
    7. {
    8. inst += instructions.section('|', i, -1);
    9. }
    To copy to clipboard, switch view to plain text mode 

    If I format my text file something like this;
    Qt Code:
    1. Bowline
    2. | bowline Form an overhand loop |
    3. | bowline Pass the end up through the loop from underneath |
    4. | bowline the end behind the standing part |
    5. Bowline
    To copy to clipboard, switch view to plain text mode 
    So the first section extracts all the text between the two Bowline tags, the count for the loop uses the lowercase bowline for the count and the second section extracts each instruction into the stringlist for iteration with the image....
    Does that make sense?
    I will give it a try later tonight once the kids are in bed!

    Cheers,
    Jon

  5. #5
    Join Date
    Sep 2010
    Location
    Ontario Canada
    Posts
    23
    Thanks
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Symbian S60 Maemo/MeeGo

    Default Re: Rookie struggling with string manipulation and searching

    I was close...
    This is what worked;
    Qt Code:
    1. instructions = line2.section(str1, 1, 1)
    2. int tmp(instructions.count('.') );
    3. for(int i = 0; i < tmp; i++)
    4. {
    5. inst += instructions.section('.', i, i);
    6. qDebug() << "*****" << inst.at(i) << "*******";
    7. }
    To copy to clipboard, switch view to plain text mode 
    I had to remove all the carriage returns in the text file also.
    Here is the file format;
    Qt Code:
    1. Bowline Form an overhand loop.Pass the end up through the loop from underneath.Pass the end behind the standing part.Bring the end back down through the loop.To tighten grip the end and the loop in one hand and the standing part in the other. .Be sure to form the knot properly. Bowline
    To copy to clipboard, switch view to plain text mode 
    and the output;
    Qt Code:
    1. ***** "Form an overhand loop " *******
    2. ***** "Pass the end up through the loop from underneath " *******
    3. ***** "Pass the end behind the standing part " *******
    4. ***** "Bring the end back down through the loop " *******
    5. ***** "To tighten grip the end and the loop in one hand and the standing part in the other " *******
    6. ***** " " *******
    7. ***** "Be sure to form the knot properly " *******
    To copy to clipboard, switch view to plain text mode 

    Thanks again!
    Cheers,
    Jon
    Last edited by Jon Heron; 19th October 2010 at 02:46.

Similar Threads

  1. Text Manipulation.
    By suneel1310 in forum Qt Programming
    Replies: 7
    Last Post: 26th July 2010, 10:07
  2. Struggling with interface classes
    By JPNaude in forum Qt Programming
    Replies: 1
    Last Post: 23rd June 2010, 09:24
  3. String manipulation methods?
    By PaladinKnight in forum Newbie
    Replies: 5
    Last Post: 16th March 2010, 23:44
  4. Struggling with QThread...
    By TemporalBeing in forum Qt Programming
    Replies: 2
    Last Post: 23rd March 2009, 20:46
  5. Struggling with value space
    By UnicycleBloke in forum Qt for Embedded and Mobile
    Replies: 3
    Last Post: 28th May 2008, 23:26

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.