maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   Development (https://talk.maemo.org/forumdisplay.php?f=13)
-   -   Need assistance from QT programmer (https://talk.maemo.org/showthread.php?t=58569)

d-iivil 2010-09-23 11:04

Re: Need assistance from QT programmer
 
Quote:

Originally Posted by nicolai (Post 824226)
Use QIODevice::ReadOnly instead.

maybe
#include <QTextStream>

I have included the QTextStream in header among others:
#include <QtCore>
#include <QMainWindow>
#include <QProgressDialog>
#include <QColorDialog>
#include <QHash>
#include <QFile>
#include <QTextStream>

But still have the errors.

nicolai 2010-09-23 11:13

Re: Need assistance from QT programmer
 
Quote:

Originally Posted by D-Iivil (Post 824235)
I have included the QTextStream in header among others:
#include <QtCore>
#include <QMainWindow>
#include <QProgressDialog>
#include <QColorDialog>
#include <QHash>
#include <QFile>
#include <QTextStream>

But still have the errors.

Ah, funny bug:

if (!file.open (IO_ReadOnly))
// didn't work
QTextStream stream ( &file );

....
while( !stream.eof() ) {

stream is only declared in the "if-block" scope. The same like this.

if (!file.open (IO_ReadOnly))
QTextStream stream ( &file );

....
while( !stream.eof() ) {

or

if (!file.open (IO_ReadOnly))
{
QTextStream stream ( &file );
}


....
while( !stream.eof() ) {

would produce an error :-)

d-iivil 2010-09-23 11:21

Re: Need assistance from QT programmer
 
Quote:

Originally Posted by nicolai (Post 824243)
Ah, funny bug:

if (!file.open (IO_ReadOnly))
// didn't work
QTextStream stream ( &file );

....
while( !stream.eof() ) {

stream is only declared in the "if-block" scope. The same like this.

if (!file.open (IO_ReadOnly))
QTextStream stream ( &file );

....
while( !stream.eof() ) {

or

if (!file.open (IO_ReadOnly))
{
QTextStream stream ( &file );
}


....
while( !stream.eof() ) {

would produce an error :-)

function 'stream' does not have a member 'eof' :-P

So I tried this:
while( !stream.atEnd())

And got no errors. However the function does not work, I get nothing into QString called "oldfont1" when trying to do it like this:
Code:

    QFile file("/etc/hildon/theme/colors.config");
    QTextStream stream ( &file );
    QString line;
    QHash<QString, QString> variables;
    while( !stream.atEnd()) {
        line = stream.readLine();
        QStringList splittedLine = line.split("=");
        variables.insert(splittedLine.at(0), splittedLine.at(1));
    }
    file.close();
    QString oldfont1 = variables.value("DefaultTextColor");


Berserk 2010-09-23 12:06

Re: Need assistance from QT programmer
 
Quote:

Originally Posted by D-Iivil (Post 824249)
However the function does not work, I get nothing into QString called "oldfont1" when trying to do it like this:
Code:

    QFile file("/etc/hildon/theme/colors.config");
    QTextStream stream ( &file );
    QString line;
    QHash<QString, QString> variables;
    while( !stream.atEnd()) {
        line = stream.readLine();
        QStringList splittedLine = line.split("=");
        variables.insert(splittedLine.at(0), splittedLine.at(1));
    }
    file.close();
    QString oldfont1 = variables.value("DefaultTextColor");


Although I didn't see it in the above code, does it contain something like the following?
Code:

file.open(QFile::ReadOnly);
I did see some "if not opened in readonly" things in the last posts, but not an explicit .open() function.

Or does a QFile open the file directly after declaring it?

I thought that.. if it's not opened, nothing is read, and the QString stays empty.

d-iivil 2010-09-23 12:19

Re: Need assistance from QT programmer
 
Quote:

Originally Posted by Berserk (Post 824294)
Although I didn't see it in the above code, does it contain something like the following?
Code:

file.open(QFile::ReadOnly);
I did see some "if not opened in readonly" things in the last posts, but not an explicit .open() function.

Or does a QFile open the file directly after declaring it?

I thought that.. if it's not opened, nothing is read, and the QString stays empty.

Hallelujah! It's working now :)
I thought it opens the file automaticly, but obviously it didn't :)

Diph 2010-09-23 12:37

Re: Need assistance from QT programmer
 
Rule number one: look from API first. :)

Usually there are examples how to use the class, for example QFile.

d-iivil 2010-09-23 19:25

Re: Need assistance from QT programmer
 
Quote:

Originally Posted by Diph (Post 824321)
Rule number one: look from API first. :)

Usually there are examples how to use the class, for example QFile.

I actually do use the "F1" -key very often inside Qt Creator and really try to find the answers first from docs before asking here :)

Problem (for me) is that most of the docs are written in the way that the writer assumes that reader already knows how things are working and therefore they are lacking the basic guides like:
1. create QFile "object" or what ever it's called
2. you must open the file with separate command so type file.open
3. do something with the file you just opened
etc etc....

But I'm slowly learning and thanks to you guys for answering my questions over and over gain.

Diph 2010-09-24 05:34

Re: Need assistance from QT programmer
 
Quote:

Originally Posted by D-Iivil (Post 824669)
Problem (for me) is that most of the docs are written in the way that the writer assumes that reader already knows how things are working and therefore they are lacking the basic guides like:
1. create QFile "object" or what ever it's called
2. you must open the file with separate command so type file.open
3. do something with the file you just opened
etc etc....

Quote:

Originally Posted by QFile Class Reference
The following example reads a text file line by line:

PHP Code:

QFile file("in.txt");
if (!
file.open(QIODevice::ReadOnly QIODevice::Text))
     return;

while (!
file.atEnd()) {
    
QByteArray line file.readLine();
    
process_line(line);



Sorry for this. :P

d-iivil 2010-09-24 09:30

Re: Need assistance from QT programmer
 
Quote:

Originally Posted by Diph (Post 824943)
Sorry for this. :P

Yep, I noticed the example but they did not explain what happens line by line which is necessary for an idiot like me :D

Anyways, it's now working and getting closer to first release :)

Berserk 2010-09-24 12:12

Re: Need assistance from QT programmer
 
What do you not understand exactly?

In the example they don't use QTextStream::readLine(), they use QFile::readLine(), which is inherited from QIODevice.

The difference is that QTextStream::readLine() returns a QString and QFile::readLine() returns a qint64 (QByteArray in the example), but I don't know anything about qint64 or QByteArrays, so I'll stick with QTextStrem::readLine() :p


All times are GMT. The time now is 06:18.

vBulletin® Version 3.8.8