我正在使用Qt創建一個顯示GUI並接受來自管道的輸入的小應用程序。阻止讀取管道的調用
如果管道沒有建立(或者,按照我的理解,如果沒有作家),調用fopen
塊,甚至認爲它show()
功能後,本應當是所謂的,沒有顯示的UI。
如何顯示用戶界面,然後調用fopen
及相關代碼?只要我的窗口在屏幕上,我不在乎是否fopen
塊。
我曾嘗試使用類似connect(this, SIGNAL(window_loaded), this, SLOT(setupListener()));
的東西,但行爲保持不變。
任何提示?謝謝 !
的main.cpp
#include <QApplication>
#include "metadataWindow.h"
#include <sys/time.h>
#include <sys/types.h>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
metadataWindow window;
window.showFullScreen();
window.setupListener();
return app.exec();
}
metadataWindow.cpp
metadataWindow::metadataWindow(QWidget *parent) : QWidget(parent)
{
this->setupUI(); // not shown here, but just basic QLabel stuff
}
void metadataWindow::setupListener()
{
const char *metadata_file = "/tmp/my-pipe-file";
// vvvvv This here is blocking vvvvvv
FILE *fd = fopen(metadata_file, "r");
pipe = new QTextStream(fd);
streamReader = new QSocketNotifier(fileno(fd), QSocketNotifier::Read, qApp);
QObject::connect(streamReader, SIGNAL(activated(int)), this, SLOT(onData()));
streamReader->setEnabled(true);
}
非常感謝您的非常清楚的解釋。我在非阻塞模式下使用'open()'進行了第二次路由,因此,我在第一次調用'onData()'時初始化了'QTextStream'管道(因爲我無法從'直接打開()')。這實際上工作得很好,我不認爲它會有任何缺點。 – tchap