This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Qt Script Question/ Read from File

Tags: None
(comma "," separated)
jseabold
Registered Member
Posts
7
Karma
0

Qt Script Question/ Read from File

Tue Feb 03, 2009 3:37 am
Hello all,

I am new to Qt Script, though I am pretty familiar with Javascript and C++ though it's been a while.  (Personal note: I miss Python!).

I am trying to write a script for my own use that keeps track of a recently played list, which will update to my webpage (everything else will be implemented in Python once the playlist is in a text file).  I need my script to be able to read from a file and am having trouble reading a file into an array.  Here is what I have so far.

Code: Select all
Importer.loadQtBinding("qt.core");
f = new QFile( "~/playlist" );
f.open(new QIODevice.OpenMode(QIODevice.ReadWrite));
var recent = new Array(50);
var line = new QTextStream(f);
i=0;
while (!f.atEnd())
{
recent[i]=line.readLine()
if (f.atEnd())
{
break;
}
else
{
i++
}
}


I can implement it another way and get my entire playlist into the array, but I am getting hung up with the f.atEnd() implementation.  It only reads the first line into the array then breaks the loop.  Why should f.atEnd() return true at the end of the first line?  I am using Linux and created a text file for the program to read from that has a few words on each line.

Side note, why is the scripting documentation still so poor?  I really wish I had the requisite knowledge to post some HowTo's because I feel like I'm having to learn to program all over again just getting the syntax down.  Nothing personal, I just hope the community becomes more accessible.  Everything else about Amarok is great.
User avatar
markey
KDE Developer
Posts
2286
Karma
3
OS
Regarding Python/QtScript: "Everything comes at a price" :)

You see, using QtScript gives us the great advantage of having _no_ additional dependencies (it's built into Qt). This is a great advantage especially for our cross-platform approach. Windows users won't have to worry about installing Python, Ruby, Bash (?), etc.

So basically it's a trade-off: A bit more work for the developers, but a big win for the users.


--
Mark Kretschmann - Amarok Developer
jseabold
Registered Member
Posts
7
Karma
0
So we should make development on this open source project a bit more work to accommodate those using Windows???  Oh well, I don't mind at all really, and I understand the thought process.  Like I said, I just wish it were documented a little better to help get used to the syntax.  The Qt references are great, but it's not at all immediately obvious to me (yet), how to make it work with Amarok, though I'm getting there.  I would settle for an interpreter to work with, so I can get a better idea of what's going on...

Anyone have any thoughts on my code?  There has to be a way to read a text file into an array line by line (perhaps I should be reading the whole thing and then splicing?)
User avatar
markey
KDE Developer
Posts
2286
Karma
3
OS
jseabold wrote:So we should make development on this open source project a bit more work to accommodate those using Windows???


Sorry, you will have no success with this approach. We're open to constructive criticism, which your above quote is clearly not. In Germany we have his saying (roughly translated): "First think, then write". Consider it good advice.

PS: Excess of question marks is disliked in many forums, including this one.


--
Mark Kretschmann - Amarok Developer
User avatar
eean
KDE Developer
Posts
1016
Karma
0
OS
readLine() returns a QByteArray, not a string.

QByteArray.prototype.toString = function()
{
  ts = new QTextStream( this, QIODevice.ReadOnly );
  return ts.readAll();
}
is handy. Then you can just pass .toString() to any QByteArray.

We're giving a presentation on Amarok QtScripting at FOSDEM this weekend, hopefully this will encourage us to put together a few more tutorials. Cause your right our docs are a bit lacking.

Edit: hmph, well the above solved one problem. You're actually having something where atEnd returns true quickly? I'm not really sure whats up with that. I can't really replicate.

Last edited by Anonymous on Tue Feb 03, 2009 10:39 pm, edited 1 time in total.


Amarok Developer
User avatar
eean
KDE Developer
Posts
1016
Karma
0
OS

you mixed your lines with your files

Tue Feb 03, 2009 10:46 pm
The following works

Code: Select all
Importer.loadQtBinding("qt.core");


QByteArray.prototype.toString = function()
{
   ts = new QTextStream( this, QIODevice.ReadOnly );
   return ts.readAll();
}

var f = new QFile( "/tmp/blah" );
f.open(QIODevice.ReadWrite);
var recent = new Array();
var line = new QTextStream(f);
while (!line.atEnd())
{
recent.push(line.readLine().toString());
}


Amarok Developer
User avatar
eean
KDE Developer
Posts
1016
Karma
0
OS
jseabold wrote:So we should make development on this open source project a bit more work to accommodate those using Windows???  Oh well, I don't mind at all really, and I understand the thought process.  Like I said, I just wish it were documented a little better to help get used to the syntax.  The Qt references are great, but it's not at all immediately obvious to me (yet), how to make it work with Amarok, though I'm getting there.  I would settle for an interpreter to work with, so I can get a better idea of what's going on...

Yea there is one, Amarok Script Console. It should be installed by default. Its a script.


Amarok Developer
jseabold
Registered Member
Posts
7
Karma
0
Ahh, that definitely clears up some problems I was having.  Thank you, Ian, very very much for your reply.  When I have a bit more time I will have a closer look and follow up here.

We're giving a presentation on Amarok QtScripting at FOSDEM this weekend, hopefully this will encourage us to put together a few more tutorials. Cause your right our docs are a bit lacking.


That's great news, and thank you for addressing the one constructive criticism that I actually did have!

You're actually having something where atEnd returns true quickly? I'm not really sure whats up with that. I can't really replicate.


If I run the code as is in the Amarok Script Console, but append an i onto the very end so it outputs where i was in the loop when the break happened it returns a 0 (or an undefined if I try to add recent[1]).  I have a text file already in place with a few words on each line, and it read the first then breaks.  I will try your suggestions though and hopefully won't have to bother with this again.

Yea there is one, Amarok Script Console. It should be installed by default. Its a script.


Yeah I am using it, and it is a HUGE help.  I would love a more interactive interpreter (one that holds variables in memory), but this is just a minor quibble and really just laziness on my part.  I fear that I would be totally lost without the Script Console.

We're open to constructive criticism, which your above quote is clearly not.


Mark, you are right.  That quote of mine was clearly not constructive criticism.  It wasn't a criticism at all.  It was a restatement of exactly what you told me in the post beforehand.  Directly after that sentence, I explain that I completely understand the goal and reasons for choosing this route, but would like to see more documentation.  I also stated this in my first post and made it clear that this wasn't personal or an attack.  I also never stated that I'd rather see Python used rather than Qtscript while we're on the subject.  I simply stated that using Qtscript makes me miss Python.  I did NOT say (or even think) "gosh I wish the developers weren't so boneheaded."  You seem to think I came here to criticize choices made by the developers when that is clearly not the case.  I appreciate everything that you all do very much and wouldn't criticize any decisions.  Actually, I never said anything about the developers at all, but both your posts seem to be very defensive like I had outright criticized them.  I would add to your German advice, that you read carefully before you think and then write.  I do apologize for the multiple question marks though.
User avatar
eean
KDE Developer
Posts
1016
Karma
0
OS
jseabold wrote:Yeah I am using it, and it is a HUGE help.  I would love a more interactive interpreter (one that holds variables in memory), but this is just a minor quibble and really just laziness on my part.  I fear that I would be totally lost without the Script Console.

It does keep variables in memory. The problem is that a new context is created, I never figured out how to work around that.

But in JavaScript its very easy to make things global. :)

So just do
Code: Select all
hello = "global hello world"


instead of

Code: Select all
var hello = "local hello world


And do
Code: Select all
globalBlah = function() {
...
}


instead of
Code: Select all
function localBlah() {
...
}


Amarok Developer


Bookmarks



Who is online

Registered users: bancha, Bing [Bot], daret, Evergrowing, Google [Bot], lockheed, sandyvee, Sogou [Bot]