2

我从 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)));

但是,该消息从未显示在“日志”中。我在这里想念什么?

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

4

1 回答 1

5

如果没有完整的示例,很难确切地看出您的实现有什么问题。有时,如果一个对象没有在堆上初始化,超出范围,信号或槽就会失败。

它可能失败的另一种方式是,如果您的 QApplication 尚未到达 exec() 调用。

我没有尝试过在信号和槽调用中使用 const,而且我之前在任何示例中都没有看到它,所以这可能会导致问题;但在下面的示例中似乎工作正常。

带有 QTextEdit 派生类的工作示例

示例截图

这是我放在一起的一个简单示例,它使用按钮和行编辑进行一些基本的日志记录。

这是标题:

#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
于 2012-06-09T18:07:05.270 回答