Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QFile and buffered device
QtWS25 Last Chance

QFile and buffered device

Scheduled Pinned Locked Moved General and Desktop
13 Posts 5 Posters 8.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Naouali
    wrote on last edited by
    #1

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

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      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

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • N Offline
        N Offline
        Naouali
        wrote on last edited by
        #3

        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
        {
        public:
        QcustomFile(const QString &name);
        public slots:
        void printData();

        signals:
        void readyRead();
        };

        #endif // QCUSTOMFILE_H
        @

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

        QcustomFile::QcustomFile(const QString &name)
        {
        this->setFileName(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");
        if(custom->open(QIODevice::ReadOnly)){

            QTimer *timer = new QTimer();
            QObject::connect(timer, SIGNAL(timeout()), custom, SLOT(printData()));
            timer->start(1000);
        
        }@
        
        1 Reply Last reply
        0
        • K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          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.

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #5

            You must add Q_OBJECT macro to you class header.

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0
            • N Offline
              N Offline
              Naouali
              wrote on last edited by
              #6

              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'

              1 Reply Last reply
              0
              • D Offline
                D Offline
                DenisKormalev
                wrote on last edited by
                #7

                Try to rerun qmake and make.

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  Jacol
                  wrote on last edited by
                  #8

                  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) :
                  QFile(parent)
                  {
                  }@
                  Of course you must add Q_OBJECT macro.

                  1 Reply Last reply
                  0
                  • N Offline
                    N Offline
                    Naouali
                    wrote on last edited by
                    #9

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

                    1 Reply Last reply
                    0
                    • N Offline
                      N Offline
                      Naouali
                      wrote on last edited by
                      #10

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

                      1 Reply Last reply
                      0
                      • J Offline
                        J Offline
                        Jacol
                        wrote on last edited by
                        #11

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

                        1 Reply Last reply
                        0
                        • N Offline
                          N Offline
                          Naouali
                          wrote on last edited by
                          #12

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

                          1 Reply Last reply
                          0
                          • G Offline
                            G Offline
                            goetz
                            wrote on last edited by
                            #13

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

                            http://www.catb.org/~esr/faqs/smart-questions.html

                            1 Reply Last reply
                            0

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved