在QT4中添加自定义广告位

时间:2017-02-16 06:54:30

标签: qt

在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());
    }
};

但它没有那样的方式得到了声明错误。我不知道添加自定义插槽的正确方法是什么。有人可以解释一下如何做到这一点吗?

1 个答案:

答案 0 :(得分:1)

实际上,根据您想要达到的目标,它不是slots而是private slotspublic slots

你在第一个例子中使用lambdas,Qt5支持,但不支持Qt4(因为它使用较旧的C ++标准)。

如果你想在你的班级中使用signals \ slots机制,你需要有Q_OBJECT宏。

所以你的例子看起来像(头文件):

#include <QObject>
class MyReceiverClass : public QObject {
Q_OBJECT

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

有关广告位的详情:Qt4: Signals & Slots

但看起来您引用了ui实施中不存在的processMyReceiverClass元素。您应该创建一个包含textBrowser元素的ui表单,并将其初始化为您的小部件的gui。像这样:

// widget.h

#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

// 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中这些基本机制如何工作的理解。