Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt WebKit
  4. Problems in Implementing Save Feature in Javascript app using Qt

Problems in Implementing Save Feature in Javascript app using Qt

Scheduled Pinned Locked Moved Qt WebKit
21 Posts 4 Posters 10.6k Views
  • 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.
  • S Offline
    S Offline
    shukla
    wrote on 26 Sept 2010, 09:16 last edited by
    #12

    Also, I too worked with a very simple javascript code, passing on the variables and it worked fine. But when it comes to this app, I am facing above problems.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      shukla
      wrote on 9 Oct 2010, 13:53 last edited by
      #13

      Hi,
      I was trying to sort out the problems. Actually, the string which I need to pass to JavaScript function is multi-line string.

      Is there any method in Qt Programming to pass multi-line string ?

      1 Reply Last reply
      0
      • B Offline
        B Offline
        benjamin.poulain
        wrote on 9 Oct 2010, 15:46 last edited by
        #14

        I am not sure to understand your problem. Can't you just have '\n' in your QString?

        1 Reply Last reply
        0
        • T Offline
          T Offline
          tobias.hunger
          wrote on 9 Oct 2010, 16:31 last edited by
          #15

          Didn't we have this discussion before in "this thread":http://developer.qt.nokia.com/forums/viewthread/523 ? Unfortunately that thread seems to no longer be available...

          1 Reply Last reply
          0
          • S Offline
            S Offline
            shukla
            wrote on 9 Oct 2010, 17:22 last edited by
            #16

            Yes, we had a discussion but at that time , you wrote a long post giving me instructions right from scratch. So I started working on it again. I was committing blunders at that time.

            Now, I have made various changes in Qt code. I have tested this on a demo javascript application as well and its working fine . That is using this code, I was able to pass my QString (which I read from file) to JavaScript function and display it as well. Though this particular QString was short of length (actually I used mid() function to get a part of it). Short length QString worked fine.

            But, for my actual JavaScript app, I need to pass whole multi-line string, which I am not able to. I can save it , so my save feature is done . But to open it, I need to pass this multi-line string. I need a solution for this .

            1 Reply Last reply
            0
            • T Offline
              T Offline
              tobias.hunger
              wrote on 10 Oct 2010, 07:59 last edited by
              #17

              QFile::readAll()?

              1 Reply Last reply
              0
              • S Offline
                S Offline
                shukla
                wrote on 10 Oct 2010, 13:46 last edited by
                #18

                Hi

                I can read the contents of saved file.

                @QDataStream in(&file);
                in.setVersion(QDataStream::Qt_4_5);
                //contacts.empty(); // empty existing contact
                in >> output;
                qDebug() << output;@

                I get all the contents of file in QString variable output. I can see that in terminal and contents are correct. But I am not able to pass this multi-line QString variable to JavaScript function.
                How can QFile::readAll() help me in this . I am really sorry, but how should I implement it in my code ?

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  tobias.hunger
                  wrote on 10 Oct 2010, 14:41 last edited by
                  #19

                  I do not know javascript, but assuming output is "Line 1\nLine 2\n" you are asking the interpreter to handle this text:

                  @
                  open('Line 1
                  Line 2
                  ');
                  @

                  I would be surprised if a Javascript interpreter would handle that.

                  You will need to escape the line breaks. I do not know enough javascript to tell you how though:-) Basically you need to make sure that what the javascript interpreter gets does contain the line breaks (and any other special characters!) in the format javascript expects them to be escaped in its strings. Maybe replacing '\n' (note that this is actually one character in C/C++) with "\n" (note that these are two characters in C/C++).

                  1 Reply Last reply
                  0
                  • ? This user is from outside of this forum
                    ? This user is from outside of this forum
                    Guest
                    wrote on 11 Oct 2010, 02:47 last edited by
                    #20

                    Tobias is right, in the "example":http://developer.qt.nokia.com/wiki/QString_variable_to_Javascript you can replace this line

                    @
                    QString data("Qt is the Best!");
                    @

                    with this
                    @
                    QString data("Qt is the Best!\nQt Rocks!!");
                    @

                    and you will get line break in your alert

                    1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      Rydik
                      wrote on 24 Oct 2011, 05:55 last edited by
                      #21

                      I see this is a very old post but would have liked this solution shown for me when I found this post. JavaScript does has problems taking string with line breaks in it. In my case (read kml file stored on local hard drive, pass the string to JavaScript, and use the Google Earth extensions library to parse and display the kml file) the line breaks did not need to be conserved in a file load. To solve this I used:
                      @
                      QTextStream in(&file);
                      QString wholeFile;
                      QString line = in.readLine();
                      wholeFile+=line;
                      while (!line.isNull()) {
                      line = in.readLine();
                      wholeFile+=line;
                      }
                      @
                      An alternative method:
                      @
                      QByteArray line = file.readAll();
                      @
                      does not play well with JavaScript.

                      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