连接QTextEdit子类中的信号和插槽?

时间:2012-06-09 13:16:38

标签: qt signals qtextedit slot

我从QTextEdit派生了一个类,并将其用作“日志”。我为它配备了一个接收日志消息的插槽。

class CLogbook : public QTextEdit
{
   Q_OBJECT;
    public:
    void log(QString msg) {append(msg)};

    public slots:
    void recvLogSignal(const QString message)
    {    
         append("hallo");
         std::cout << "signal received.\n";    
         log(message);
    }

};

然后另一个班级会发出这样的信号:

// in the header
signals:
    void logMessage(const QString);

// in the implementation
    emit logMessage("qt is cute");
    std::cout << "if you can read this the logMessage was emitted\n";

并且我也将信号连接到插槽

connect(tableeditor, SIGNAL(logMessage(const QString)), logbook, SLOT(recvLogSignal(const QString)));

但是,消息永远不会显示在“日志”中。我在这里缺少什么?

已解决:发出信号后调用connect方法: - (

1 个答案:

答案 0 :(得分:4)

如果没有完整的示例,很难确切地看到您的实施有什么问题。如果对象超出范围,如果未在堆上初始化,则有时信号或插槽将失败。

另一种可能失败的方法是你的QApplication没有到达exec()调用。

我没有尝试在信号和插槽调用中使用const,我之前没有在任何例子中看到它,因此可能导致问题;但它似乎在下面的例子中工作正常。

使用QTextEdit的派生类

的工作示例

Screenshot of example

这是一个简单的例子,我把它放在一起,通过按钮和行编辑进行一些基本的记录。

以下是标题:

#ifndef WIDGET_H
#define WIDGET_H

#include <QtGui/QWidget>
//#include <QTextEdit>
#include "clogbook.h"
#include <QLineEdit>

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = 0);
    ~Widget() {}
public slots:
    void on_pushButton();
    void on_lineEditReturn();
private:
    CLogBook * text_edit_log;
    QLineEdit * line_edit;
};

#endif // WIDGET_H

以下是来源:

#include "widget.h"

#include <QBoxLayout>
#include <QPushButton>
#include <QTimer>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    QBoxLayout * box_layout = new QBoxLayout(QBoxLayout::TopToBottom);

    text_edit_log = new CLogBook;
    line_edit = new QLineEdit("Type Here and press Enter.");
    QPushButton * push_button = new QPushButton("Click to Add To Log");
    text_edit_log->setText("My Log Book");
    box_layout->addWidget(text_edit_log);
    box_layout->addWidget(line_edit);
    box_layout->addWidget(push_button);

    this->setLayout(box_layout);

    QObject::connect(push_button, SIGNAL(clicked()), this, SLOT(on_pushButton()));
    QObject::connect(line_edit, SIGNAL(returnPressed()), this, SLOT(on_lineEditReturn()));
}

void Widget::on_pushButton()
{
//    text_edit_log->append("Push Button Logging Test");
    text_edit_log->recvLogSignal("Push button test.");
}

void Widget::on_lineEditReturn()
{
//    text_edit_log->append(line_edit->text());
    text_edit_log->recvLogSignal(QString("LineEdit: ") + line_edit->text() );
    line_edit->clear();
}

这是主要的:

#include <QtGui/QApplication>
#include "widget.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}

这是CLogBook类:

#ifndef CLOGBOOK_H
#define CLOGBOOK_H

#include <QTextEdit>
#include <iostream>

class CLogBook : public QTextEdit
{
    Q_OBJECT
public:
    explicit CLogBook(QWidget *parent = 0) : QTextEdit(parent) { }
    void log (QString msg) { append(msg); }

public slots:
    void recvLogSignal(const QString message)
    {
        append("hallo");
        std::cout << "signal received.\n" << std::endl;
        log(message);
    }

};

#endif // CLOGBOOK_H