【发布时间】:2016-11-15 11:08:03
【问题描述】:
我正在使用 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);
}
【问题讨论】: