【发布时间】:2015-06-19 07:08:47
【问题描述】:
我编写了一个 Qt 应用程序来为后端 Linux 控制台应用程序创建 UI。现在,我希望控制台输出和标准错误显示在我的应用程序的窗口中。
我已经尝试了以下代码:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QProcess *console_backend = new QProcess(this);
QString file = "/path/to/console_executable";
console_backend->start(file);
console_backend->setReadChannel(QProcess::StandardOutput);
QObject::connect(console_backend, SIGNAL(console_backend
->readyReadStandardOutput()),
this, SLOT(this->receiveConsoleBackendOutput()));
}
void MainWindow::receiveConsoleBackendOutput()
{
//..celebrate !
}
当我执行应用程序时,我得到的错误是:
Object::connect: No such signal
QProcess::console_backend>readyReadStandardOutput() in
../projekt_directory/mainwindow.cpp:239
Object::connect: (receiver name: 'MainWindow')
我对信号槽的理解有误吗?为什么是“没有这样的信号”? 整个方法是错误的吗?
【问题讨论】:
-
你会的。
connect(console_backend, SIGNAL(readyReadStandardOutput()), this, SLOT(receiveConsoleBackendOutput())); -
@Amartel 哦该死。那是真正的代码复制粘贴。它应该是 console_backend 代替 real_code。您的建议可以消除错误。
-
SIGNAL和SLOT是宏,它们将方法的名称转换为字符串,因此您需要删除所有this->等调用。只是方法的签名。阅读 Qt 的手册了解更多示例。
标签: qt signals-slots qprocess