Results 1 to 7 of 7

Thread: QIODevice::bytesAvailable() always returning 0

  1. #1
    Join Date
    Apr 2006
    Posts
    122
    Thanks
    18
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

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

    Qt Code:
    1. #include"MyDialog.h"
    2. #include<iostream>
    3. #include<QTimer>
    4. #include<QString>
    5. #include<QFile>
    6. using namespace std;
    7.  
    8. MyDialog::MyDialog()
    9. {
    10. setupUi(this);
    11. connect(this,SIGNAL(yesAvailable()),this,SLOT(readDataHere()));
    12. connect(checkBox,SIGNAL(clicked()),this,SLOT(openFile()));
    13. connect(this,SIGNAL(dataFound()),this,SLOT(receiveOnRead()));
    14. flag=false;
    15.  
    16. }
    17.  
    18. MyDialog::~MyDialog()
    19. {
    20.  
    21. }
    22. void MyDialog::openFile()
    23. { if(flag==false)
    24. {
    25. flag=true;
    26. fifoFile.setFileName("/home/prince/mydev01");
    27. if(!fifoFile.open(QIODevice::ReadOnly))
    28. cout<<"Error in opening the fifofile"<<endl;
    29. else
    30. { cout<<"File opened\n";
    31. connect(&fifoFile,SIGNAL(readyRead()),this,SLOT(readDataHere()));
    32. QTimer *t=new QTimer(this);
    33. connect(t,SIGNAL(timeout()),this,SLOT(checkingAvailability()));
    34. t->start(1001);
    35. }
    36. }
    37. }
    38. void MyDialog::checkingAvailability()
    39. { long x;
    40. if(fifoFile.canReadLine())
    41. cout<<" \n YA can read line";
    42. else
    43. cout<<" \n Can't read line:";
    44. x=fifoFile.bytesAvailable();
    45. cout<<"\t No of bytes available"<<x<<"\t";
    46. if(x>0)
    47. {
    48.  
    49. emit yesAvailable();
    50. }
    51. }
    52.  
    53. void MyDialog::receiveOnRead()
    54. {
    55.  
    56. QString sx;
    57. sx.setNum(x);
    58. QString sy;
    59. sy.setNum(y);
    60. label->setText(sx+QString(" ") +sy);
    61. cout<<"\n From dialog "<<x<<" "<<y<<endl;
    62. }
    63.  
    64. void MyDialog::readDataHere()
    65. {
    66. if(fifoFile.canReadLine())
    67. cout<<" \n YA can read line";
    68. QByteArray temp=fifoFile.read(2);
    69. bool conversionErrorFlag;
    70. x=temp.toInt(&conversionErrorFlag);
    71. temp=fifoFile.read(2);
    72. cout<<"\t "<<x;
    73. y=temp.toInt(&conversionErrorFlag);
    74. emit dataFound();
    75. }
    To copy to clipboard, switch view to plain text mode 

    quick nitin
    Last edited by quickNitin; 14th June 2006 at 08:46. Reason: code changed

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

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

  3. #3
    Join Date
    Apr 2006
    Posts
    122
    Thanks
    18
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default 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

  4. #4
    Join Date
    Apr 2006
    Posts
    122
    Thanks
    18
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QIODevice::bytesAvailable() always returning 0

    fifoFile is not sequential

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

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

  6. #6
    Join Date
    Apr 2006
    Posts
    122
    Thanks
    18
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

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

  7. #7
    Join Date
    Apr 2006
    Posts
    122
    Thanks
    18
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default 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
    Qt Code:
    1. connect(&fileFifo,SIGNAL(readyRead()),qApp,SLOT(aboutQt()));
    To copy to clipboard, switch view to plain text mode 

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

Similar Threads

  1. Replies: 12
    Last Post: 14th June 2006, 10:24
  2. [DevQt] New versions, feature requests and bug reports
    By fullmetalcoder in forum Qt-based Software
    Replies: 103
    Last Post: 15th May 2006, 10:54

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.