0

I am currently working on a project for school, and I am supposed to program some functions. I'm currently working in a QT application and I have to count words from an Input Widget and give the number of counted words out in another widget.

void MainWindow::countWords(){
int wordCount = ui -> input ->toPlainText().split(QRegExp("(\\s|\\n|\\r)+")
                                                  , QString::SkipEmptyParts).count();
ui->output->clear();
ui->output->addItem(QString("Words: "+wordCount));

that's what I currently have(I looked it up in another question on this website) but it doesn't really work. If I type something and press the button to count the words, I get no number of words

Fuchur13
  • 9
  • 4
  • 1
    Start with the [tour] and read [ask]. Concerning your question, there's not enough to work with. One thing is to provide a [mcve], the other is to tell us what it does, because "doesn't really work" is nothing anyone can analyse. – Ulrich Eckhardt Jan 06 '22 at 12:31
  • Also, using Qt doesn't exclude usage of the C++ standard library, such as `std::istringstream`, which would easily be used to get a count of the number of words. – PaulMcKenzie Jan 06 '22 at 12:33
  • Yes sorry, i updated my question. – Fuchur13 Jan 06 '22 at 12:41
  • 1
    `ui -> input ->toPlainText().split` -- First, determine where the error is. You now have this string of indirection, and you don't know which one is at fault. What is returned from `ui -> input ->toPlainText()`? Save that in a variable and inspect it in the debugger or print it out. Once you do determine that the variable has the text entered, then the issue is the call to `QRegEx` and/or `count()`. – PaulMcKenzie Jan 06 '22 at 12:48
  • @Fuchur13 -- *If I type something and press the button to count the words, I get no number of words* -- [What is a debugger?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems). Once you write a program, if the program doesn't "work", the next job for you is to use the debugger to determine where the problem is. – PaulMcKenzie Jan 06 '22 at 12:52
  • Does it work if you change your output line to this: `ui->output->addItem(QString("Words: ") + QString::number(wordCount));` – JarMan Jan 06 '22 at 12:54
  • The line int wordCount = ui -> input ->toPlainText(); isn't working on its own because I put in a string. My problem is that I only had like 1-2 hours of qt in school and before that we only did something in c++ so I don't really understand how QRegEx works and I also can't find other solutions how I can count words in qt. The code above is one I found on another question, so It's not really important, I'm just searching for a solution to count words. – Fuchur13 Jan 06 '22 at 13:00
  • @JarMan omg yes thank you – Fuchur13 Jan 06 '22 at 13:02
  • @Fuchur13 -- The line `ui->input->toPlainText();` does not return an `int`. It returns, I would guess, a string or pointer to a character buffer. Once you had verified that there indeed is a string, **then** you take that string and use it in QRegRex(). That's how a programmer breaks down the issue. – PaulMcKenzie Jan 06 '22 at 13:03
  • @PaulMcKenzie ui->input->toPlainText(); isn't this line just converting my input into a string, or am I wrong? – Fuchur13 Jan 06 '22 at 13:04
  • You are not wrong, but you should never just call several things in one long chain like that without checking if one of the links are broken. That's my point. You just assumed that "everything worked" when you had `ui->input->toPlainText()->blah->blah`, when no such assumption could be made. – PaulMcKenzie Jan 06 '22 at 13:06
  • Yes sorry im trying to learn, thank you. And i have another question, QRegExp("(\\s|\\n|\\r)+" what is this piece of code doing? @PaulMcKenzie – Fuchur13 Jan 06 '22 at 13:41

1 Answers1

0

You were incorrectly adding an integer to a string literal. That works in some languages, but not C++. You have to convert your integer to a QString first.

ui->output->addItem(QString("Words: ") + QString::number(wordCount));
JarMan
  • 7,589
  • 1
  • 10
  • 25
  • My problem was that i didnt knew how to convert a integer to a QString , you helped me very much. Thank you – Fuchur13 Jan 06 '22 at 13:08
  • I have one more question, is there a simple explanation what "(QRegExp("(\\s|\\n|\\r)+")" this piece of code is doing? – Fuchur13 Jan 06 '22 at 13:34
  • @Fuchur13 [Regular Expressions](https://en.wikipedia.org/wiki/Regular_expression). Many editors and word processors give you a choice of using regular expressions to do a search. – PaulMcKenzie Jan 06 '22 at 13:44
  • @Fuchur13, The regex is looking for space characters and newline/return characters. That's how it splits the string into words. – JarMan Jan 06 '22 at 14:25
  • Thank you very much :)) – Fuchur13 Jan 06 '22 at 14:59
  • If this answered your question, please mark it as accepted. : ) – JarMan Jan 06 '22 at 15:24