gpt4 book ai didi

qt - 在 QTextEdit 子类中连接信号和槽?

转载 作者:行者123 更新时间:2023-12-02 05:41:01 29 4
gpt4 key购买 nike

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

然而,该消息从未显示在“日志”中。我在这里缺少什么?

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

最佳答案

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

它可能失败的另一种情况是您的 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

关于qt - 在 QTextEdit 子类中连接信号和槽?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10961361/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com