我正在使用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