Results 1 to 9 of 9

Thread: Using an array and QString contains

  1. #1
    Join Date
    Mar 2011
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Using an array and QString contains

    Hello everyone. I'm writing a program that will search for particular words and count if they appear or not and then output to a csv file. I suspect that my problem lies in line 71 when it checks to see if otext contains dataArray[iii][0] and writes "yes" if it does, because the debugresults displays "no" for dataArray[iii][1].

    My code:

    Easier to read code/comments::



    Qt Code:
    1. class cdata
    2. {//creation of the cdata class. This is created to protect dataArray from external influences, but can still be accessed through select functions.
    3. public:
    4. void popcountries();
    5. QString debugresults();
    6. void parseInput(QString);
    7.  
    8. private:
    9. QString dataArray[192][192];
    10.  
    11. };
    12.  
    13.  
    14. void MainWindow::on_parse_B_clicked()
    15. {//button clicked and program starts
    16.  
    17. QString tmp; //creates the QString for results to be shown
    18. tmp = start(); //start()'s results are the results to be shown
    19. ui.debugResultsTxt->setPlainText(tmp); //results shown
    20. }
    21.  
    22. QString start()
    23. {
    24. //calls cdata class from above
    25. cdata d;
    26.  
    27. d.popcountries(); // populates first column of the array from the given file
    28.  
    29. QString input;
    30. MainWindow mw; //calls mainwindow class
    31. input = mw.oText(); //returns the text to be searched through as the user's input
    32.  
    33. d.parseInput(input); //uses the user's input to see if it contains dataArray[iii][0] (the first column of the array)
    34.  
    35. QString tmp; //creates a qstring to return the final/debug results
    36. tmp = d.debugresults(); //debug results. This is used to format the final results as they would appear in the csv file.
    37.  
    38. return tmp; //returns the formatted final/debug results to on_parse_B_clicked()
    39.  
    40. }
    41.  
    42. void cdata::popcountries()
    43. { //parses the file line by line and populates the first column of dataArray. If the end of the file is reached, it populates the rest with "-1" as a placeholder
    44. QFile file;
    45. QString filename = "countriesList.txt";
    46. file.setFileName(filename);
    47. file.open(QIODevice::ReadOnly | QIODevice::Text);
    48. QTextStream txtstream(&file);
    49. for (int iii=0; iii<192; iii++)
    50. {
    51.  
    52.  
    53. QString tmp = txtstream.readLine();
    54. dataArray[iii][0] = tmp;
    55.  
    56. int xxx = iii+1;
    57. if (txtstream.atEnd() == TRUE)
    58. {
    59. for (xxx ; xxx<192; xxx++)
    60. {
    61. QString tmp2 = "-1";
    62. dataArray[xxx][0] = tmp2;
    63. }
    64. break;
    65. }
    66.  
    67. }
    68. file.close();
    69.  
    70. }
    71.  
    72.  
    73. QString MainWindow::oText()
    74. { //grabs the user's input and returns it to start()
    75.  
    76. QString otext;
    77. otext = ui.tmpTextinput->toPlainText();
    78. return otext;
    79. }
    80.  
    81. void cdata::parseInput(QString otext)
    82. {//checks to see if the word in dataArray[iii][0] (the first column as populated above) is in otext (the user's input) and writes "yes" in the next column if it is, and "no" if it is not.
    83.  
    84. for (int iii = 0; iii<192; iii++)
    85. {
    86.  
    87. int xxx = iii+1;
    88.  
    89. if (otext.contains(dataArray[iii][0], Qt::CaseInsensitive)) //Check to see if otext has the word from the first column of dataArray --- I believe that this is the problem
    90. {
    91. dataArray[iii][1] = "yes"; //if it does, it writes yes in the next column
    92. }
    93. else
    94. {
    95. dataArray[iii][1] = "no"; //if it doesn't, it writes no in the next column
    96. }
    97. if (dataArray[xxx][0] == "-1") // checks the next row in the first column to see if it is the placeholder "-1". If it is, it breaks the loop.
    98. break;
    99. }
    100.  
    101.  
    102.  
    103. }
    104.  
    105.  
    106. QString cdata::debugresults()
    107. { //formats the array into a string that will eventually become the csv file. For now, it formats them and returns the formatted string to start(), which returns that to on_parse_B_clicked() and displays it.
    108. QString tmp1 = "Test results:,\n";
    109. for (int iii=0; iii<192; iii++)
    110. {
    111.  
    112. QString tmp2 = dataArray[iii][0];
    113. QString tmp3 = dataArray[iii][1];
    114. tmp2.append(" , " + tmp3 + " , ");
    115. tmp1.append(tmp2 + " \n");
    116.  
    117. int xxx = iii+1;
    118.  
    119. if (dataArray[xxx][0] == "-1")
    120. break;
    121.  
    122. }
    123. return tmp1;
    124. }
    To copy to clipboard, switch view to plain text mode 

    Original code in post::
    Qt Code:
    1. class cdata
    2. {
    3. public:
    4. void popcountries();
    5. QString debugresults();
    6. void parseInput(QString);
    7.  
    8. private:
    9. QString dataArray[192][192];
    10.  
    11. };
    12.  
    13. void cdata::popcountries()
    14. {
    15. QFile file;
    16. QString filename = "countriesList.txt";
    17. file.setFileName(filename);
    18. file.open(QIODevice::ReadOnly | QIODevice::Text);
    19. QTextStream txtstream(&file);
    20. for (int iii=0; iii<192; iii++)
    21. {
    22.  
    23.  
    24. QString tmp = txtstream.readLine();
    25. dataArray[iii][0] = tmp;
    26.  
    27. int xxx = iii+1;
    28. if (txtstream.atEnd() == TRUE)
    29. {
    30. for (xxx ; xxx<192; xxx++)
    31. {
    32. QString tmp2 = "-1";
    33. dataArray[xxx][0] = tmp2;
    34. }
    35. break;
    36. }
    37.  
    38. }
    39. file.close();
    40.  
    41. }
    42.  
    43. QString cdata::debugresults()
    44. {
    45. QString tmp1 = "Test results:,\n";
    46. for (int iii=0; iii<192; iii++)
    47. {
    48.  
    49. QString tmp2 = dataArray[iii][0];
    50. QString tmp3 = dataArray[iii][1];
    51. tmp2.append(" , " + tmp3 + " , ");
    52. tmp1.append(tmp2 + " \n");
    53.  
    54. int xxx = iii+1;
    55.  
    56. if (dataArray[xxx][0] == "-1")
    57. break;
    58.  
    59. }
    60. return tmp1;
    61. }
    62.  
    63. void cdata::parseInput(QString otext)
    64. {//checks to see if the country dataArray[iii][0] is listed in otext
    65.  
    66. for (int iii = 0; iii<192; iii++)
    67. {
    68.  
    69. int xxx = iii+1;
    70.  
    71. if (otext.contains(dataArray[iii][0], Qt::CaseInsensitive)) //I believe that this is the problem
    72. {
    73. dataArray[iii][1] = "yes";
    74. }
    75. else
    76. {
    77. dataArray[iii][1] = "no";
    78. }
    79. if (dataArray[xxx][0] == "-1")
    80. break;
    81. }
    82.  
    83.  
    84.  
    85. }
    86.  
    87. QString MainWindow::oText()
    88. {
    89.  
    90. QString otext;
    91. otext = ui.tmpTextinput->toPlainText();
    92. return otext;
    93. }
    94.  
    95. QString start()
    96. {
    97. //calls cdata class
    98. cdata d;
    99.  
    100. d.popcountries(); //in cdata.cpp and populates first column of the array
    101.  
    102. QString input;
    103. MainWindow mw; //calls mainwindow class
    104. input = mw.oText(); //in cdata.cpp and returns the users input to search for
    105.  
    106. d.parseInput(input); //parses the input;compares against the first column of dataAray[iii][0]; writes "yes" or "no" to column two; and in parseInput.cpp
    107.  
    108. QString tmp;
    109. tmp = d.debugresults(); //debug results. Used for displaying the final results.
    110.  
    111. return tmp;
    112.  
    113. }
    114.  
    115. void MainWindow::on_parse_B_clicked()
    116. {//button clicked and program starts
    117.  
    118. QString tmp;
    119. tmp = start();
    120. ui.debugResultsTxt->setPlainText(tmp);
    121. }
    To copy to clipboard, switch view to plain text mode 

    What could I be doing wrong? I wrote a similar program that uses only a 1d array and it works fine.
    Last edited by TomJoad; 11th April 2011 at 17:44.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Using an array and QString contains

    What could I be doing wrong?
    All kinds of things:
    You define a two dimentional array where the second dimension is 192:
    Qt Code:
    1. QString dataArray[192][192];
    To copy to clipboard, switch view to plain text mode 
    But nowhere in your code you use more than '0' or '1' dimensions.
    So probably
    Qt Code:
    1. QString dataArray[192][2];
    To copy to clipboard, switch view to plain text mode 
    Is better.

    But since you are using C++, and Qt why not use containers?
    Qt Code:
    1. QVector<QVector<QString>> dataArray;
    To copy to clipboard, switch view to plain text mode 
    You win many advantages over you C style array.

    Moreover, when you fill your array you use ONLY the '0' dimension, so why do you use a multi dimensional array at all?
    In addition, allthough you are filling only the '0' dimension, in debugresults() you read and append from both?!

    Then the following code:
    Qt Code:
    1. for (int iii=0; iii<192; iii++)
    2. {
    3.  
    4.  
    5. QString tmp = txtstream.readLine();
    6. dataArray[iii][0] = tmp;
    7.  
    8. int xxx = iii+1;
    9. if (txtstream.atEnd() == TRUE)
    10. {
    11. for (xxx ; xxx<192; xxx++)
    12. {
    13. QString tmp2 = "-1";
    14. dataArray[xxx][0] = tmp2;
    15. }
    16. break;
    17. }
    18.  
    19. }
    To copy to clipboard, switch view to plain text mode 
    can be translated to:
    Qt Code:
    1. QVector<QString> oneDimension;
    2. while(!txtstream.atEnd())
    3. {
    4. oneDimension.push_back(txtstream.readLine());
    5. }
    6.  
    7. oneDimension.push_back(QString::number(-1));
    8. dataArray.push_back(oneDimension);
    To copy to clipboard, switch view to plain text mode 

    You problem lies in a messy and unstructured code, which is hard to follow and understand, which probably is the cause to any logical error you are making.
    Clean up and structure you code first, add comments, so that YOU too can know what the code is actually trying to do.
    Then we can have a second look.

    Please don't take my critic personally, all I said is with full respect.
    I just called 'em the way I see 'em, and the advice is real and true.
    Last edited by high_flyer; 11th April 2011 at 16:45. Reason: code corrections
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Mar 2011
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using an array and QString contains

    This is what I envision:

    The program will open multiple webpages and write "yes" or "no" to the dataArray if the webpage contains the word in dataArray[iii][0]. The reason for [192][192] is because there will be 192 words it checks against, and the proceeding columns will contain "yes" or "no" for each website it checks against. So 0,0 will be a word and 0,1; 0,2; etc. will be the result "yes" or "no." The final output will be to a csv file that will go:

    word, yes, no, yes, yes, etc.,
    word 2, no, no, yes, no, etc.,
    etc.

    Right now, I have a txt file it grabs the list of words from to populate the first column (working), and check against an input (what the user inputs) and write the results (not working). The program will then show it in the results window, just for testing purposes.

    I appreciate the suggestion of using a vector instead, and it appears to be the better solution instead of using an array, but it didn't address the underlying problem of why the program isn't correctly recognizing that the user's input is a word in the first column of the array and writes "yes" in the next row.

    I've gone through and done a better job at commenting on the code. I believe lack of comments from my first post may have created some confusion.

    Qt Code:
    1. class cdata
    2. {//creation of the cdata class. This is created to protect dataArray from external influences, but can still be accessed through select functions.
    3. public:
    4. void popcountries();
    5. QString debugresults();
    6. void parseInput(QString);
    7.  
    8. private:
    9. QString dataArray[192][192];
    10.  
    11. };
    12.  
    13.  
    14. void MainWindow::on_parse_B_clicked()
    15. {//button clicked and program starts
    16.  
    17. QString tmp; //creates the QString for results to be shown
    18. tmp = start(); //start()'s results are the results to be shown
    19. ui.debugResultsTxt->setPlainText(tmp); //results shown
    20. }
    21.  
    22. QString start()
    23. {
    24. //calls cdata class from above
    25. cdata d;
    26.  
    27. d.popcountries(); // populates first column of the array from the given file
    28.  
    29. QString input;
    30. MainWindow mw; //calls mainwindow class
    31. input = mw.oText(); //returns the text to be searched through as the user's input
    32.  
    33. d.parseInput(input); //uses the user's input to see if it contains dataArray[iii][0] (the first column of the array)
    34.  
    35. QString tmp; //creates a qstring to return the final/debug results
    36. tmp = d.debugresults(); //debug results. This is used to format the final results as they would appear in the csv file.
    37.  
    38. return tmp; //returns the formatted final/debug results to on_parse_B_clicked()
    39.  
    40. }
    41.  
    42. void cdata::popcountries()
    43. { //parses the file line by line and populates the first column of dataArray. If the end of the file is reached, it populates the rest with "-1" as a placeholder
    44. QFile file;
    45. QString filename = "countriesList.txt";
    46. file.setFileName(filename);
    47. file.open(QIODevice::ReadOnly | QIODevice::Text);
    48. QTextStream txtstream(&file);
    49. for (int iii=0; iii<192; iii++)
    50. {
    51.  
    52.  
    53. QString tmp = txtstream.readLine();
    54. dataArray[iii][0] = tmp;
    55.  
    56. int xxx = iii+1;
    57. if (txtstream.atEnd() == TRUE)
    58. {
    59. for (xxx ; xxx<192; xxx++)
    60. {
    61. QString tmp2 = "-1";
    62. dataArray[xxx][0] = tmp2;
    63. }
    64. break;
    65. }
    66.  
    67. }
    68. file.close();
    69.  
    70. }
    71.  
    72.  
    73. QString MainWindow::oText()
    74. { //grabs the user's input and returns it to start()
    75.  
    76. QString otext;
    77. otext = ui.tmpTextinput->toPlainText();
    78. return otext;
    79. }
    80.  
    81. void cdata::parseInput(QString otext)
    82. {//checks to see if the word in dataArray[iii][0] (the first column as populated above) is in otext (the user's input) and writes "yes" in the next column if it is, and "no" if it is not.
    83.  
    84. for (int iii = 0; iii<192; iii++)
    85. {
    86.  
    87. int xxx = iii+1;
    88.  
    89. if (otext.contains(dataArray[iii][0], Qt::CaseInsensitive)) //Check to see if otext has the word from the first column of dataArray --- I believe that this is the problem
    90. {
    91. dataArray[iii][1] = "yes"; //if it does, it writes yes in the next column
    92. }
    93. else
    94. {
    95. dataArray[iii][1] = "no"; //if it doesn't, it writes no in the next column
    96. }
    97. if (dataArray[xxx][0] == "-1") // checks the next row in the first column to see if it is the placeholder "-1". If it is, it breaks the loop.
    98. break;
    99. }
    100.  
    101.  
    102.  
    103. }
    104.  
    105.  
    106. QString cdata::debugresults()
    107. { //formats the array into a string that will eventually become the csv file. For now, it formats them and returns the formatted string to start(), which returns that to on_parse_B_clicked() and displays it.
    108. QString tmp1 = "Test results:,\n";
    109. for (int iii=0; iii<192; iii++)
    110. {
    111.  
    112. QString tmp2 = dataArray[iii][0];
    113. QString tmp3 = dataArray[iii][1];
    114. tmp2.append(" , " + tmp3 + " , ");
    115. tmp1.append(tmp2 + " \n");
    116.  
    117. int xxx = iii+1;
    118.  
    119. if (dataArray[xxx][0] == "-1")
    120. break;
    121.  
    122. }
    123. return tmp1;
    124. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Using an array and QString contains

    I suggest you avoid calling your variables 'xxx' and 'tmp2', it makes your code completely unreadable.

    Basically the easiest way to count words in the text is to use what QTextStream offers:
    Qt Code:
    1. QHash<QString,int> occurences;
    2. QFile f("input");
    3. if(!f.open(QFile::ReadOnly|QFile::Text)) return false;
    4. QTextStream stream(&f);
    5. QString word;
    6. QRegExp isWord("[A-Za-z_]+"); // allow letters and underscore only
    7. while(!stream.atEnd()){
    8. stream >> word; // get next word
    9. if(!isWord.exactMatch(word)) continue; // doesn't match, skip it
    10. int cnt = occurences.value(word, 0);
    11. occurences.insert(word, cnt+1);
    12. }
    To copy to clipboard, switch view to plain text mode 
    Now "occurences" contains count of all words in the text and you can choose the ones you need. You can also pre-insert all the words you're interested in into the hash and count only those that are already in the hash. The representation is superflous but it's simplest what you can do. An advanced approach would use a prefix tree instead of the hash to make the representation much more compact.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Using an array and QString contains

    if you put a break point in line 89, what are the values you see for otext and dataArray[iii][0]?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. #6
    Join Date
    Mar 2011
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using an array and QString contains

    Quote Originally Posted by high_flyer View Post
    if you put a break point in line 89, what are the values you see for otext and dataArray[iii][0]?
    I was having a hard time figuring out what you were referring to. Do you mean this function?

    Qt Code:
    1. void cdata::parseInput(QString otext)
    2. {//checks to see if the country dataArray[iii][0] is listed in otext
    3.  
    4. for (int iii = 0; iii<192; iii++)
    5. {
    6.  
    7. int xxx = iii+1;
    8.  
    9. if (otext.contains(dataArray[iii][0], Qt::CaseInsensitive)) //I believe that this is the problem
    10. {
    11. dataArray[iii][1] = "yes";
    12. }
    13. else
    14. {
    15. dataArray[iii][1] = "no";
    16. }
    17. if (dataArray[xxx][0] == "-1")
    18. break;
    19. }
    To copy to clipboard, switch view to plain text mode 

    For this one, I expect it to check to see if dataArray[iii][0] is in otext. If it is, I expect it to write "yes" to dataArray[iii][1], if not, it should write no. Once it is done with that if else statement, it moves to the next if statement which checks if dataArray[xxx = iii+1][0] is "-1". If it is, the loop breaks and the program stops checking. If not, it cycles through the loop again to check the next value of dataArray[iii][0].

    The first column of dataArray as populated by popcountries():
    USA
    Ireland
    Italy
    France

    debug output regardless if "USA" is otext or not:
    Test results:,
    USA , no ,
    Ireland , no ,
    Italy , no ,
    France , no ,

    What it should be if USA is otext:
    Test results:,
    USA , yes ,
    Ireland , no ,
    Italy , no ,
    France , no ,

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Using an array and QString contains

    Is there any particular reason why you are doing it the hard way? There are so many beautiful data structures in the world that suit your purpose better than a flat array - like a "set", for example.
    Qt Code:
    1. QSet<QString> allCountries;
    2. QList<QIODevice*> streamsToCheck;
    3. struct Bucket {
    4. QIODevice *device;
    5. QSet<QString> found;
    6. QSet<QString> notFound;
    7. };
    8. QList<Bucket> buckets;
    9. foreach(QIODevice *dev, streamsToCheck) {
    10. Bucket b;
    11. b.notFound = allCountries;
    12. b.device = dev;
    13. QTextStream stream(dev);
    14. QString word;
    15. while(!b.notFound.isEmpty() && !stream.atEnd()) {
    16. stream >> word;
    17. word = word.toLower();
    18. if(b.notFound.contains(word))
    19. b.notFount.remove(word);
    20. }
    21. b.found = allCountries - b.notFound;
    22. buckets << b;
    23. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Mar 2011
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using an array and QString contains

    Ok everyone, I have narrowed down the problem. It is actually a problem with oText() not returning like it should. Perhaps I am not accessing it right (I don't think so), or something else is going wrong. If I hard code input (input = "france" it works.

    Qt Code:
    1. QString start()
    2. {
    3. //calls cdata class from above
    4. cdata d;
    5.  
    6. d.popcountries(); // populates first column of the array from the given file
    7.  
    8. QString input;
    9. MainWindow mw; //calls mainwindow class
    10. input = mw.oText(); //returns the text to be searched through as the user's input
    11.  
    12. d.parseInput(input); //uses the user's input to see if it contains dataArray[iii][0] (the first column of the array)
    13.  
    14. QString tmp; //creates a qstring to return the final/debug results
    15. tmp = d.debugresults(); //debug results. This is used to format the final results as they would appear in the csv file.
    16.  
    17. return tmp; //returns the formatted final/debug results to on_parse_B_clicked()
    18.  
    19. }
    To copy to clipboard, switch view to plain text mode 

    oText:
    Qt Code:
    1. QString MainWindow::oText()
    2. { //grabs the user's input and returns it to start()
    3.  
    4. QString otext;
    5. otext = ui.tmpTextinput->toPlainText();
    6. return otext;
    7. }
    To copy to clipboard, switch view to plain text mode 

    Complete header:

    Qt Code:
    1. #include "ui_mainwindow.h"
    2.  
    3. #include <QMainWindow>
    4. #include <QFile>
    5. #include <QTextStream>
    6. #include <QString>
    7.  
    8. namespace Ui {
    9. class MainWindow;
    10. }
    11.  
    12. class MainWindow : public QMainWindow
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. explicit MainWindow(QWidget *parent = 0);
    18.  
    19. QString oText();
    20.  
    21.  
    22. private slots:
    23. void on_parse_B_clicked();
    24.  
    25. private:
    26. Ui::MainWindow ui;
    27. };
    28.  
    29.  
    30. class cdata
    31. {
    32. public:
    33. void popcountries();
    34. QString debugresults();
    35. void parseInput(QString);
    36.  
    37. private:
    38. QString dataArray[192][192];
    39.  
    40. };
    To copy to clipboard, switch view to plain text mode 

    Are you not allowed to return a QString like this? It doesn't seem likely....

    ========================

    FIXED (in a roundabout way):

    Still didn't figure out why oText() wasn't returning the QString correctly, but fixed it by adding the correct lines of code to the push button and then passing it to start().

    Qt Code:
    1. QString start(QString input) //<---changed
    2. {
    3. //calls cdata class
    4. cdata d;
    5.  
    6. d.popcountries(); //in cdata.cpp
    7.  
    8. d.parseInput(input); //parses the input and in parseInput.cpp
    9.  
    10. QString tmp;
    11. tmp = d.debugresults(); //debug results
    12.  
    13. return tmp;
    14.  
    15. }
    16.  
    17.  
    18. void MainWindow::on_parse_B_clicked()
    19. {
    20.  
    21. QString tmp;
    22. QString input = ui.tmpTextinput->toPlainText(); //<-- added to get the contents of the txt input
    23. tmp = start(input); //<-- passes the input to start
    24. ui.debugResultsTxt->setPlainText(tmp);
    25. }
    To copy to clipboard, switch view to plain text mode 

    Anyone happen to know the reason why oText() wasn't returning the QString correctly?
    Last edited by TomJoad; 11th April 2011 at 21:14.

  9. #9
    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: Using an array and QString contains

    Where does one start.
    Quote Originally Posted by TomJoad View Post
    Qt Code:
    1. QString start()
    2. {
    3. //calls cdata class from above
    4. cdata d;
    5.  
    6. d.popcountries(); // populates first column of the array from the given file
    7.  
    8. QString input;
    9. MainWindow mw; //calls mainwindow class
    10. input = mw.oText(); //returns the text to be searched through as the user's input
    11.  
    12. d.parseInput(input); //uses the user's input to see if it contains dataArray[iii][0] (the first column of the array)
    13.  
    14. QString tmp; //creates a qstring to return the final/debug results
    15. tmp = d.debugresults(); //debug results. This is used to format the final results as they would appear in the csv file.
    16.  
    17. return tmp; //returns the formatted final/debug results to on_parse_B_clicked()
    18.  
    19. }
    To copy to clipboard, switch view to plain text mode 
    OK. At line 9 you create a new instance of the MainWindow class. You never show this instance, so any UI element in that class will have its default value, e.g. an empty string for line and text edits. At line 10 you retrieve the default value of a text edit via oText() into input and process it. oText() is functioning correctly.

Similar Threads

  1. Char array[6] to QString and display it
    By arunvv in forum Newbie
    Replies: 11
    Last Post: 12th March 2014, 20:48
  2. Converting QString to char array
    By srohit24 in forum Qt Programming
    Replies: 3
    Last Post: 22nd July 2009, 18:19
  3. Assign a wchar_t array to QString
    By jacky in forum Qt Programming
    Replies: 5
    Last Post: 18th April 2009, 12:28
  4. Char Array to QString
    By 3nc31 in forum Qt Programming
    Replies: 2
    Last Post: 25th November 2007, 22:18
  5. how to copy QString content into array
    By eric in forum Qt Programming
    Replies: 12
    Last Post: 14th November 2007, 23:13

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.