QFile and buffered device
-
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); }@
-
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. -
Try to rerun qmake and make.
-
-
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).