PDA

View Full Version : QIODevice::bytesAvailable() always returning 0



quickNitin
14th June 2006, 06:25
hi

i am trying to read from a fifo and checking availability of data on fifo using QIODevice::bytesAvailable() which as per doc returns no of bytes availble for reading.
But to me it is always returns 0.
Aim of following code is to see whether something is there for reading in file if yes read it and append to textlabel.



#include"MyDialog.h"
#include<iostream>
#include<QTimer>
#include<QString>
#include<QFile>
using namespace std;

MyDialog::MyDialog()
{
setupUi(this);
connect(this,SIGNAL(yesAvailable()),this,SLOT(read DataHere()));
connect(checkBox,SIGNAL(clicked()),this,SLOT(openF ile()));
connect(this,SIGNAL(dataFound()),this,SLOT(receive OnRead()));
flag=false;

}

MyDialog::~MyDialog()
{

}
void MyDialog::openFile()
{ if(flag==false)
{
flag=true;
fifoFile.setFileName("/home/prince/mydev01");
if(!fifoFile.open(QIODevice::ReadOnly))
cout<<"Error in opening the fifofile"<<endl;
else
{ cout<<"File opened\n";
connect(&fifoFile,SIGNAL(readyRead()),this,SLOT(readDataHer e()));
QTimer *t=new QTimer(this);
connect(t,SIGNAL(timeout()),this,SLOT(checkingAvai lability()));
t->start(1001);
}
}
}
void MyDialog::checkingAvailability()
{ long x;
if(fifoFile.canReadLine())
cout<<" \n YA can read line";
else
cout<<" \n Can't read line:";
x=fifoFile.bytesAvailable();
cout<<"\t No of bytes available"<<x<<"\t";
if(x>0)
{

emit yesAvailable();
}
}

void MyDialog::receiveOnRead()
{

QString sx;
sx.setNum(x);
QString sy;
sy.setNum(y);
label->setText(sx+QString(" ") +sy);
cout<<"\n From dialog "<<x<<" "<<y<<endl;
}

void MyDialog::readDataHere()
{
if(fifoFile.canReadLine())
cout<<" \n YA can read line";
QByteArray temp=fifoFile.read(2);
bool conversionErrorFlag;
x=temp.toInt(&conversionErrorFlag);
temp=fifoFile.read(2);
cout<<"\t "<<x;
y=temp.toInt(&conversionErrorFlag);
emit dataFound();
}



quick nitin

wysota
14th June 2006, 09:43
Could be because you connected the readyRead signal to a slot which may cause the data to be read before the other slot fires and has a chance to call bytesAvailable(). Also check if your file isSequential().

quickNitin
14th June 2006, 10:25
thanks .i will check that. my file is a QFile object opening a fifo . i will check for that also

quickNitin
14th June 2006, 11:13
fifoFile is not sequential

wysota
14th June 2006, 11:33
fifoFile is not sequential
I think bytesAvailable() will always return 0 in that case. But this is strange... fifos should be sequential... It might be a bug in QFile -- it might not check if file is a fifo. In that case you'll have to rely on readyRead() signal.

quickNitin
14th June 2006, 11:40
in doc sth ambiguous is said about readyRead(). It says readyRead() is emitted everytime when new data is available or appeneded to file. In next para it says in a loop it may not be emitted again.
I cannot understand this distiction fully.

quickNitin
14th June 2006, 14:04
readyRead() signal is also not being generated while on other side of fifo brazenhem's line drawing algo is continuously puttin the points.
I checked it by adding statement


connect(&fileFifo,SIGNAL(readyRead()),qApp,SLOT(aboutQt())) ;


is it something abnormal or i am missing sth about behaviour of QIODevice with fifos.