questions over QtextStream and Qfile

  • I need to translate certain strings. This was done before on windows systems using a .mdb file. For my linux platforms I have extracted the information and created a .csv as well as a .txt file.

    As usual Qt docs are not really helping me that much. My first problem is that I cannot find how I cab open the file in my application, I tried something which I found googling:

    // main function v v v //
    case 101:
         if (b == 0x02 || b == 0x86) {  // b is serial read byte, 0x02 and 0x86 mark end of transmission
; // open the file
          translation = file.readAll();   //Stuff the content of the file in QString 'translation'
          qDebug() << translation;        // prints the entire content of the file on my monitor to check if it works
          file.close();                   // close the file
          EXCEPTION = false;
          special = 0;  // things needed for the rest of the app
          qDebug() << "Text to translate :" <<toTranslate; // << works
          toTranslate = ""; // clears the Qstring
         } else {
      toTranslate += (char)b; // concatenate b to QString 'toTranslate'  << works

    Whatever I try I am not getting the content of the file displayed on my monitor. The text file 'Dictionary2.csv.txt' is present in the /home/pi folder

    The output looks as follows

    <<  "85"
    <<  "20"
    <<  "20"
    <<  "20"
    <<  "6d"
    <<  "65"
    <<  "61"
    <<  "73"
    <<  "75"
    <<  "72"
    <<  "69"
    <<  "6e"
    <<  "67"
    <<  "20"
    <<  "69"
    <<  "6e"
    <<  "73"
    <<  "74"
    <<  "72"
    <<  "75"
    <<  "6d"
    <<  "65"
    <<  "6e"
    <<  "74"
    <<  "73"
    <<  "2"
    QFile::open: File () already open    // << here the 90.000 chars should have been printed
    Text to translate : "   measuring instruments"  // << is correct

    It keeps telling me that the file is already open, so I removed the line which opens it, but there was no difference.
    So the question is: howcan fix?

    Than I have a 2nd question. The end goal is that my 'captured' QString will be translated. This string is present in the dictionary file and the translations are behind it, separated by commas. A part of the file:


    The grand masterplan is that I locate my string in the file, than read the content byte for byte counting the commas depending on which language is selected and then prints the remainder on my display until the next comma.

    What I would like to know, is how I can find my string in that dictionary text file, and than process the remainder byte for byte until the first following comma. I think I can do the last part using

    char c = (char);
    // code to process c

    But I still don't know how to locate my string and read stuff behind it

    I noticed that LibreOffice Calc would organize my csv file into nice rows n columns, MS excel did do no such thing. So idk perhaps it would be better to use an XML file instead of csv so I scan and extract data from it more efficiently???

  • if (! {
    It will solve your first problem.

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

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

    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 (! return;
                              translation = file.readLine();
                              qDebug() << translation;
                              EXCEPTION = false;
                              special = 0;
                              qDebug() << "Text to translate :" <<toTranslate;
                              toTranslate = "";
                          } else {
                              toTranslate += (char)b; // concatenate b to QString 'toTranslate'

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

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

  • Moderators


    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()


  • 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");
      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

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

  • @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?

  • @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

  • @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

  • Qt Champions 2017

    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.

  • " ...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.

Log in to reply

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