Menu

Main Menu
Talk Get Daily Search

Member's Online

    User Name
    Password

    Need assistance from QT programmer

    Reply
    Page 13 of 18 | Prev | 3   11     12   13   14     15   | Next | Last
    d-iivil | # 121 | 2010-09-23, 11:04 | Report

    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.

    Edit | Forward | Quote | Quick Reply | Thanks

     
    nicolai | # 122 | 2010-09-23, 11:13 | Report

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

    Edit | Forward | Quote | Quick Reply | Thanks

     
    d-iivil | # 123 | 2010-09-23, 11:21 | Report

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

    Edit | Forward | Quote | Quick Reply | Thanks

     
    Berserk | # 124 | 2010-09-23, 12:06 | Report

    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.

    Edit | Forward | Quote | Quick Reply | Thanks
    The Following User Says Thank You to Berserk For This Useful Post:
    d-iivil

     
    d-iivil | # 125 | 2010-09-23, 12:19 | Report

    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

    Edit | Forward | Quote | Quick Reply | Thanks
    The Following User Says Thank You to d-iivil For This Useful Post:
    Berserk

     
    Diph | # 126 | 2010-09-23, 12:37 | Report

    Rule number one: look from API first.

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

    Edit | Forward | Quote | Quick Reply | Thanks

     
    d-iivil | # 127 | 2010-09-23, 19:25 | Report

    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.

    Edit | Forward | Quote | Quick Reply | Thanks

     
    Diph | # 128 | 2010-09-24, 05:34 | Report

    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

    Edit | Forward | Quote | Quick Reply | Thanks

     
    d-iivil | # 129 | 2010-09-24, 09:30 | Report

    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

    Edit | Forward | Quote | Quick Reply | Thanks

     
    Berserk | # 130 | 2010-09-24, 12:12 | Report

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

    Edit | Forward | Quote | Quick Reply | Thanks

     
    Page 13 of 18 | Prev | 3   11     12   13   14     15   | Next | Last
vBulletin® Version 3.8.8
Normal Logout