我用QFile打開一個檔案,然后用QSocketNotifier監聽它的描述符是否可讀,如果activatied則進入槽函式列印一句話。但實際運行時,終端不停的重復列印(不停的進入槽函式),我很奇怪,沒有往檔案寫任何東西啊?如果我去讀檔案,什么都讀不到。。。代碼如下:Server::Server(QString fileName, QObject *parent) :QObject(parent){ file = new QFile(fileName, this); if(file->open(QIODevice::ReadWrite)){ if(file->handle() >= 0){ notifier = new QSocketNotifier(file->handle(), QSocketNotifier::Write, this); connect(notifier, SIGNAL(activated(int)), this, SLOT(onActivated(int))); } }else qDebug() << "open file failed!";}voidServer::onActivated(int){ char buf[32]; int n = file->read(buf, 31); qDebug() << "n = " << n;}如上所述,總是不停的進onActivated,讀的n總是0。。。。盼望解答疑惑,謝謝!
uj5u.com熱心網友回復:
你好 我也遇到了這樣的問題 原因找到了嗎轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/186475.html
標籤:其他技術討論專區
下一篇:Java監聽器的四種實作方法
