在QT4中添加自定义插槽

在QT4中添加自定义插槽,qt,Qt,在Qt5中,我使用此方法将进程发出的信号与插槽连接起来,但在Qt4中不起作用 connect(process, &QProcess::readyReadStandardOutput, [=]{ ui->textBrowser->append(process->readAllStandardOutput()); }); 根据某人的建议,我尝试以这种方式实现它,并将原来的行替换为 connect(process, SIGNAL(readyReadStandardError

在Qt5中,我使用此方法将进程发出的信号与插槽连接起来,但在Qt4中不起作用

connect(process, &QProcess::readyReadStandardOutput, [=]{
ui->textBrowser->append(process->readAllStandardOutput());
});
根据某人的建议,我尝试以这种方式实现它,并将原来的行替换为

connect(process, SIGNAL(readyReadStandardError()), receiver, SLOT(yourCustomSlot()) );
并在mainwindow.h中添加了这个

class MyReceiverClass {

slots:
    void yourCustomSlot() {
        ui->textBrowser->append(process->readAllStandardOutput());
    }
};

但它并没有以这种方式工作,因为它得到了声明错误。我不知道添加自定义插槽的正确方法是什么。有人能告诉我怎么做吗?

实际上,这不是
插槽
,而是
私人插槽
公共插槽
,取决于你想要实现的目标

你在第一个例子中使用lambdas,QT5支持,但不是QT4(因为它使用了旧的C++标准)。 如果你想在你的类中使用signals\slots机制,你需要有Q_对象宏

因此,您的示例如下(头文件):

//widget.cpp

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
}

但是,您应该真正阅读关于如何在Qt中基于UI表单创建小部件和gui的文档,以及关于插槽和信号连接的文档,因为您的问题表明您对这些基本机制在Qt中的工作方式缺乏了解。

不,我现在没有使用任何lambda,我试图以这种方式实现它,但得到的错误是“ui”未在此范围内声明“process”未在此范围内声明
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

namespace Ui {
class Widget;
}

class QPushButton;

class MyReceiverClass  : public QWidget
{
    Q_OBJECT

public:
    explicit MyReceiverClass (QWidget *parent = 0);
    ~MyReceiverClass ();

public slots:
    void yourCustomSlot() {
                ui->textBrowser->append(process->readAllStandardOutput());
            }

private:
    Ui::Widget *ui;
};

#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
}