Reply
Thread Tools
d-iivil's Avatar
Posts: 2,154 | Thanked: 2,186 times | Joined on Dec 2009 @ Hellsinki, Finland
#121
Originally Posted by nicolai View Post
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.
__________________
If you're rich and you think I deserve a cold beer, you may donate one or two :-P

80's style stadium rock is back - FIRENOTE
Hi-Octane heavy metal - FORCE MAJEURE
 
nicolai's Avatar
Posts: 1,637 | Thanked: 4,424 times | Joined on Apr 2009 @ Germany
#122
Originally Posted by D-Iivil View Post
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's Avatar
Posts: 2,154 | Thanked: 2,186 times | Joined on Dec 2009 @ Hellsinki, Finland
#123
Originally Posted by nicolai View Post
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");
__________________
If you're rich and you think I deserve a cold beer, you may donate one or two :-P

80's style stadium rock is back - FIRENOTE
Hi-Octane heavy metal - FORCE MAJEURE
 
Berserk's Avatar
Posts: 199 | Thanked: 156 times | Joined on May 2010 @ Holland
#124
Originally Posted by D-Iivil View Post
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.
__________________
Wallpaeper - Desktop background manager (in Extras!)
Config Reader - Export all Gconf to an HTML file + compare feature (Extras-testing)
Even though these programs are available for free, I would appreciate any donations
 

The Following User Says Thank You to Berserk For This Useful Post:
d-iivil's Avatar
Posts: 2,154 | Thanked: 2,186 times | Joined on Dec 2009 @ Hellsinki, Finland
#125
Originally Posted by Berserk View Post
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
__________________
If you're rich and you think I deserve a cold beer, you may donate one or two :-P

80's style stadium rock is back - FIRENOTE
Hi-Octane heavy metal - FORCE MAJEURE
 

The Following User Says Thank You to d-iivil For This Useful Post:
Posts: 180 | Thanked: 76 times | Joined on May 2010
#126
Rule number one: look from API first.

Usually there are examples how to use the class, for example QFile.
 
d-iivil's Avatar
Posts: 2,154 | Thanked: 2,186 times | Joined on Dec 2009 @ Hellsinki, Finland
#127
Originally Posted by Diph View Post
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.
__________________
If you're rich and you think I deserve a cold beer, you may donate one or two :-P

80's style stadium rock is back - FIRENOTE
Hi-Octane heavy metal - FORCE MAJEURE
 
Posts: 180 | Thanked: 76 times | Joined on May 2010
#128
Originally Posted by D-Iivil View Post
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....
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's Avatar
Posts: 2,154 | Thanked: 2,186 times | Joined on Dec 2009 @ Hellsinki, Finland
#129
Originally Posted by Diph View Post
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

Anyways, it's now working and getting closer to first release
__________________
If you're rich and you think I deserve a cold beer, you may donate one or two :-P

80's style stadium rock is back - FIRENOTE
Hi-Octane heavy metal - FORCE MAJEURE
 
Berserk's Avatar
Posts: 199 | Thanked: 156 times | Joined on May 2010 @ Holland
#130
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()
__________________
Wallpaeper - Desktop background manager (in Extras!)
Config Reader - Export all Gconf to an HTML file + compare feature (Extras-testing)
Even though these programs are available for free, I would appreciate any donations
 
Reply


 
Forum Jump


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