QIODevice::bytesAvailable() always returning 0
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.
Code:
#include"MyDialog.h"
#include<iostream>
#include<QTimer>
#include<QString>
#include<QFile>
using namespace std;
MyDialog::MyDialog()
{
setupUi(this);
connect(this,SIGNAL(yesAvailable()),this,SLOT(readDataHere()));
connect(checkBox,SIGNAL(clicked()),this,SLOT(openFile()));
connect(this,SIGNAL(dataFound()),this,SLOT(receiveOnRead()));
flag=false;
}
MyDialog::~MyDialog()
{
}
void MyDialog::openFile()
{ if(flag==false)
{
flag=true;
fifoFile.setFileName("/home/prince/mydev01");
cout<<"Error in opening the fifofile"<<endl;
else
{ cout<<"File opened\n";
connect(&fifoFile,SIGNAL(readyRead()),this,SLOT(readDataHere()));
connect(t,SIGNAL(timeout()),this,SLOT(checkingAvailability()));
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()
{
sx.setNum(x);
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";
bool conversionErrorFlag;
x=temp.toInt(&conversionErrorFlag);
temp=fifoFile.read(2);
cout<<"\t "<<x;
y=temp.toInt(&conversionErrorFlag);
emit dataFound();
}
quick nitin
Re: QIODevice::bytesAvailable() always returning 0
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().
Re: QIODevice::bytesAvailable() always returning 0
thanks .i will check that. my file is a QFile object opening a fifo . i will check for that also
Re: QIODevice::bytesAvailable() always returning 0
fifoFile is not sequential
Re: QIODevice::bytesAvailable() always returning 0
Quote:
Originally Posted by quickNitin
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.
Re: QIODevice::bytesAvailable() always returning 0
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.
Re: QIODevice::bytesAvailable() always returning 0
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
Code:
connect(&fileFifo,SIGNAL(readyRead()),qApp,SLOT(aboutQt()));
is it something abnormal or i am missing sth about behaviour of QIODevice with fifos.