Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Pick words from .txt
Qt 6.11 is out! See what's new in the release blog

Pick words from .txt

Scheduled Pinned Locked Moved General and Desktop
11 Posts 4 Posters 4.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Miikukka
    wrote on last edited by
    #1

    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!

    There are no stupid questions, only stupid answers.

    1 Reply Last reply
    0
    • P Offline
      P Offline
      p-himik
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Miikukka
        wrote on last edited by
        #3

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

        There are no stupid questions, only stupid answers.

        1 Reply Last reply
        0
        • R Offline
          R Offline
          Rahul Das
          wrote on last edited by
          #4

          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" ]


          Declaration of (Platform) independence.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Miikukka
            wrote on last edited by
            #5

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

            There are no stupid questions, only stupid answers.

            1 Reply Last reply
            0
            • G Offline
              G Offline
              giesbert
              wrote on last edited by
              #6

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

              Nokia Certified Qt Specialist.
              Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

              1 Reply Last reply
              0
              • R Offline
                R Offline
                Rahul Das
                wrote on last edited by
                #7

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


                Declaration of (Platform) independence.

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  Miikukka
                  wrote on last edited by
                  #8

                  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)
                  {
                  ui->listWidget->addItem(word1)
                  }
                  if (Time == 2)
                  {
                  ui->listWidget->addItem(word2)
                  }
                  Time == Time + 1;@
                  Do you understand now?

                  There are no stupid questions, only stupid answers.

                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    Rahul Das
                    wrote on last edited by
                    #9

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

                    for (int i = 0; i < list1.size(); ++i)
                    ui->listWidget->addItem(list1.at(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())
                    ui->listWidget->addItem(list1.at(i));
                    i++;@


                    Declaration of (Platform) independence.

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      Miikukka
                      wrote on last edited by
                      #10

                      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!

                      There are no stupid questions, only stupid answers.

                      1 Reply Last reply
                      0
                      • R Offline
                        R Offline
                        Rahul Das
                        wrote on last edited by
                        #11

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

                        Go ahead :) Happy coding :)


                        Declaration of (Platform) independence.

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved