Pick words from .txt

  • Hola! How I can import words from file called test.txt? For example I want import words "a dog", " a cat", "a car" and "a boy". Words are separated from other files with %% or with anything sign, because I want they individually. @a dog %% a cat %% a car %% a boy@
    Do you understand? If you don't, I can explain.
    Thank you!

  • You can use QTextStream, QTextStrem::readLine() and QString::split().

  • Could you write example how to use those. I'm beginner, so I don't understand yet everything in the docs.

  • May be you should try reading the doc again. Example is already given "there":http://doc.qt.nokia.com/latest/qstring.html#split

    bq. QString str = "a,,b,c";
    QStringList list1 = str.split(",");
    // list1: [ "a", "", "b", "c" ]

  • Ok. Now I can split them, but still, how I can use them alone?

  • What do you mean by use them alone?
    QStringList is a list with strings. You can access each item seperatly.

  • you may use "at()":http://doc.qt.nokia.com/stable/qlist.html#, first(), last()... etc

  • I mean that I want add each of them one at time to listWidget when I click pushButton.
    @char* word1 = first // first should be" a dog"
    char* word2 = second // second should be "a cat"
    static int Time = 1;
    if (Time == 1)
    if (Time == 2)
    Time == Time + 1;@
    Do you understand now?

  • @QString str = " a dog %% a cat %% a car %% a boy";
    QStringList list1 = str.split("%%");

    for (int i = 0; i < list1.size(); ++i)

    Note : just wrote roughly here, not tested. :)

    OR if you want to add one item per click,

    in the slot for button,
    @static int i = 0;
    if(i < list1.size())

  • Thanks! I was just stupid and I didn't understand how I must use "list1.at(i)" command. Now I can make my application! THANK YOU!

  • bq. There are no stupid questions, only stupid answers.

    Go ahead :) Happy coding :)

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.