Qt World Summit: Register Today!

QFile and buffered device

  • Hi
    is the QFile considered as a buffered device or not ? i want to implement the canreadLine() function .
    Thanks in advance .

  • Depends how you are opening the file.
    You can open it as unbuffered. However, there is a restriction of unbuffered opening under windows.
    So, it should be buffered by default. QFile has also a flush method.
    "See and search buffer":http://doc.qt.nokia.com/4.7/qfile.html#QFile

  • Ok , thanks for that . i have another issue , i created a custom class that inherited QFile and then create a new slot but when i tried to use it i get this error "Object::connect: No such slot QFile::printData()
    here's the code for class
    @#ifndef QCUSTOMFILE_H
    #define QCUSTOMFILE_H
    #include <QFile>

    class QcustomFile: public QFile
    QcustomFile(const QString &name);
    public slots:
    void printData();

    void readyRead();

    #endif // QCUSTOMFILE_H

    #include "qcustomfile.h"
    #include <QDebug>

    QcustomFile::QcustomFile(const QString &name)
    void QcustomFile::printData()
    char buf[1024];
    qint64 lineLength = this->readLine(buf, sizeof(buf));
    if (lineLength != -1) {
    qDebug() << buf;
    // the line is available in buf

    and this is where i m using the new class
    @ QcustomFile* custom=new QcustomFile("/home/aladin/gps.txt");

        QTimer *timer = new QTimer();
        QObject::connect(timer, SIGNAL(timeout()), custom, SLOT(printData()));

  • I am not a real expert in signal-slot connections.
    My first guess would be that you have to register your custom class.
    "Have a look to qRegisterMetaType":http://doc.qt.nokia.com/4.7/qmetatype.html#qRegisterMetaType
    May be this is solving your issue, because the error sais QFile::printData not found. However, I could find the logic for the register requirement yet.

  • You must add Q_OBJECT macro to you class header.

  • this is the error i get when i added the Q_OBJECT
    /home/aladin/Projects/Qt/gps/qcustomfile.cpp:-1: error: undefined reference to `vtable for QcustomFile'

  • Try to rerun qmake and make.

  • Are you clean your project?
    in h file constructor:

    @explicit QcustomFile(const QString &name, QObject *parent = 0);@

    in cpp :
    @QcustomFile::QcustomFile(const QString &name, QObject *parent) :
    Of course you must add Q_OBJECT macro.

  • @ Jacol: still get the same
    @Denis Kormalev : after running qmake and make i didn't get an executable .

  • Thanks for all you guys , i remove all the unnecessary files and it works :)

  • Try remove all files from debug/release directory and all makefiles and built again. If this does not help that I have no idea.

  • that's what i've done and it works fine ,thank you all .

  • Ah, yes, and add the QObject parent pointer parameters to the constructor, as Jacol suggested. It's always good to mimic the base class' constructors if you want to use your own class as a drop in replacement.

    And if you get some weird or unexpected compiler errors, it is always a good idea to completely clean the project, delete the makefiles and re-run qmake and do a full rebuild. The same holds for erratic crashes (eg. after you have added a member to a class).

Log in to reply