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. questions over QtextStream and Qfile
Qt 6.11 is out! See what's new in the release blog

questions over QtextStream and Qfile

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 5 Posters 3.9k Views 2 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.
  • Vinod KuntojiV Offline
    Vinod KuntojiV Offline
    Vinod Kuntoji
    wrote on last edited by
    #2

    if (!file.open(QIODevice::ReadOnly) {
    return;
    }
    It will solve your first problem.

    QStringList pieces = mainString.split( "," );
    This will help to split the string using delimiter " , ".

    C++, Qt, Qt Quick Developer,
    PthinkS, Bangalore

    B 1 Reply Last reply
    6
    • Vinod KuntojiV Vinod Kuntoji

      if (!file.open(QIODevice::ReadOnly) {
      return;
      }
      It will solve your first problem.

      QStringList pieces = mainString.split( "," );
      This will help to split the string using delimiter " , ".

      B Offline
      B Offline
      bask185
      wrote on last edited by
      #3

      @Vinod-Kuntoji nope it does not, so I probably forget something.
      In the header file I have:

      private:
      QFile file;
      

      In the constructor of mainwindow:

      file.setFileName("Dictionary2.csv.txt"); // I removed the dir because the file is in the same folder as the application's executable
      

      and in the function:

      case 101:
                            if (b == 0x02 || b == 0x86) {
                                if (!file.open(QIODevice::ReadOnly)) return;
      
                                translation = file.readLine();
                                qDebug() << translation;
                                file.close();
      
      
                                EXCEPTION = false;
                                special = 0;
                                qDebug() << "Text to translate :" <<toTranslate;
                                toTranslate = "";
                            } else {
                                toTranslate += (char)b; // concatenate b to QString 'toTranslate'
                            }
                            break;
      

      But nothing, it keeps telling me the device is already open when I use the readlLine command.

      1 Reply Last reply
      0
      • B Offline
        B Offline
        bask185
        wrote on last edited by
        #4

        I fixed problem one by making file a locale variable inside case 101

        1 Reply Last reply
        2
        • EddyE Offline
          EddyE Offline
          Eddy
          wrote on last edited by
          #5

          Hi,

          about problem 2 :

          You were using a database before, which seems the best solution for this. You could use sqlite as a platform independent database.

          How many languages would you like to support in your app? only 3?
          I you stick to the .txt file plan you could read line by line and put it in a QString. Use split to get a QStringlist.

          I would use separate containers for each language pair.
          If you populate a QHash or QMap with the QStrings, then you can find the one you want with at()

          Eddy

          Qt Certified Specialist
          www.edalsolutions.be

          1 Reply Last reply
          3
          • B Offline
            B Offline
            bask185
            wrote on last edited by
            #6

            Hi eddy,

            Last week I have a long way with the translations. And it is almost done. I first stuff all the content of the .csv in a QString. It is basically a table with 14 columns/languages and 140 rows/sentences to translate.

            I first split on newline characters so I have a String array with all the lines. After that I start a for-loop in which I split row[i] on commas so every line gets split into 14 elements (different languages. Than I compare the first element of that array with the String I must translate. (english is first column) and lastly I add the language variable to the index depending which language is set.

            QFile file("Dictionary.csv");
            
              file.open(QIODevice::ReadOnly);
              translation = file.readAll();
              QStringList pieces = translation.split("\n");
                                      
               for (int i=0;i<pieces.size();i++) {
                 QStringList pieces2 = pieces[i].split(",");
                 if (pieces2[0].contains(toTranslate) == true) qDebug() << pieces2[language]; 
                 if(pieces2[0] == toTranslate) qDebug() << "king O the hill"; // doesnt work yet
                                        
                 }
                 file.close();
            

            I still have to wright the part where the translation gets printed on my display but that is not a big deal.

            At the moment I still have a small little problem with the string comparisons. If I must translate the string " test" the output is:

            <<  "85"
            <<  "20"
            <<  "20"
            <<  "20"
            <<  "74"
            <<  "65"
            <<  "73"
            <<  "74"
            <<  "2"
            "\"   TEST\""
            "\"   TEST MACHINE SEQUENCE\""
            "\"   TEST MOTOR WHEEL TURN\""
            "\"   TEST NIPPLE-MOTORS\""
            Text to translate : "   TEST"
            
            

            you can see what the problem is, that what I must translate is present in 4/140 lines. That is why I added the 2nd comparison which is not working. I already suspected I couldnt compare string like that ;) I think that I will simply break out of the for-loop as soon as I have a translation. I think that oughta solve it.

            The only big thing which remains is to translate the Chinese, Thai and polish things. But I won't be the one doing that :D

            1 Reply Last reply
            0
            • J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by J.Hilk
              #7

              @bask185 This might be a stupid question on my side, but why don't you use Qt's buid in crossplatform translation tool for this task?


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              B 1 Reply Last reply
              0
              • J.HilkJ J.Hilk

                @bask185 This might be a stupid question on my side, but why don't you use Qt's buid in crossplatform translation tool for this task?

                B Offline
                B Offline
                bask185
                wrote on last edited by
                #8

                @J.Hilk The question is not stupid, but some of my answers might be ;)

                1: I did not know this existed << most common reason I don't use a specific Qt function

                after reading your link:

                2: seems rather complicated, but this must be the ever poor explaining doc.qt pages

                3: From the example on that page, I am not seeing how it actually works...

                4: I am not seeing how I can combine this with our own database

                5: this... 'thing' makes it's own translations??? was unclear.

                6: I already have my translation working with ~10 lines of code, so I think I won't be needing it

                J.HilkJ 1 Reply Last reply
                0
                • B bask185

                  @J.Hilk The question is not stupid, but some of my answers might be ;)

                  1: I did not know this existed << most common reason I don't use a specific Qt function

                  after reading your link:

                  2: seems rather complicated, but this must be the ever poor explaining doc.qt pages

                  3: From the example on that page, I am not seeing how it actually works...

                  4: I am not seeing how I can combine this with our own database

                  5: this... 'thing' makes it's own translations??? was unclear.

                  6: I already have my translation working with ~10 lines of code, so I think I won't be needing it

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #9

                  @bask185
                  Well, both the build in method and your method are quite similar.

                  • in your project file you can mark the files where the translations shall be stored. E.G.:
                  TRANSLATIONS = De_de.ts \
                                 En_en.ts \
                  
                  • Mark every string that shall be translated with the tr() makro. E.g.:
                   QString str = tr("Translate me");
                  
                  • the cmd-line program lupdate will parse all your cpp-files and extract all strings markt for translation

                  • the QtLinquist can be used to link your translation to the appropriate string

                  the QTranslator Class is used to load the matching translations


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  1 Reply Last reply
                  1
                  • mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by mrjj
                    #10

                    Hi
                    Its not that complicated even it does seems a bit so from the Docs.
                    Overall, you flag all texts in the program wit tr("the text"), you run a tool that
                    produces a ts file. ( extracts all texts flagged with tr)
                    This ts file you can open in the Qt Linguist editor.
                    You will manually do the actual translation.
                    Linguist tool will produce a fast binary file .qm that can be loaded by the translator class to load another language.
                    The whole language system provide tool to help the translation. ( contexts ( where is text used) ) and hints for the translator and duplicates.
                    (same text, other context) and what is translated and what still needs to be.

                    However, for your use case having the text externally, it might not help that much or even be overkill if you need nothing extra help managing the
                    flow of translating the app.

                    1 Reply Last reply
                    2
                    • B Offline
                      B Offline
                      bask185
                      wrote on last edited by
                      #11

                      " ...or even be overkill ..." For me that means about 95% of all Qt functions ;)

                      I find myself to have problems with all the super Qt 'features' more often than that I can make good use of them.

                      I also fixed the string comparison problem thingi, it turned out that the content of the file was encapsulated in quotation marks, I removed those and all problems vanished. I will now go on with the next 'challenge'; Chinese :D So Imma gonna put this one on the solved stack. Tnx folks.

                      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