为什么我的插槽没有被调用?

发布于 2024-09-05 14:13:53 字数 479 浏览 8 评论 0原文

我有这个类:


class CustomEdit : public QTextEdit
{
    Q_GADGET

public:
    CustomEdit(QWidget* parent);

public slots:
    void onTextChanged ();
};


CustomEdit::CustomEdit(QWidget* parent)
    : QTextEdit(parent)
{
    connect( this, SIGNAL(textChanged()), this, SLOT(onTextChanged()));
}


void CustomEdit::onTextChanged ()
{
    // ... do stuff
}


当我在编辑控件中键入文本时,永远不会调用 onTextChanged 方法。
我缺少什么?

I have this class:


class CustomEdit : public QTextEdit
{
    Q_GADGET

public:
    CustomEdit(QWidget* parent);

public slots:
    void onTextChanged ();
};


CustomEdit::CustomEdit(QWidget* parent)
    : QTextEdit(parent)
{
    connect( this, SIGNAL(textChanged()), this, SLOT(onTextChanged()));
}


void CustomEdit::onTextChanged ()
{
    // ... do stuff
}


The onTextChanged method is never called when I type text into the edit control.
What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

可爱咩 2024-09-12 14:13:53

所有包含信号或槽的类都必须在其声明顶部提及 Q_OBJECT。它们还必须(直接或间接)从 QObject 派生。

尝试使用 Q_OBJECT

All classes that contain signals or slots must mention Q_OBJECT at the top of their declaration. They must also derive (directly or indirectly) from QObject.

Try using Q_OBJECT

苏佲洛 2024-09-12 14:13:53

其他一些可能性:

1) 您发出信号的对象被阻塞(请参阅 QObject::blockSignals())

2) 接收器没有线程关联。如果创建接收器的线程对象消失并且接收器没有移动到另一个线程,则它不会处理事件(插槽是一种特殊情况)。

A couple of other possibilities:

1) The object you are emitting the signal from is blocked (see QObject::blockSignals())

2) The receiver has no thread affinity. If the thread object that the receiver was created in goes away and the receiver isn't moved to another thread, it won't process events (slots being a special case).

幻梦 2024-09-12 14:13:53

我花了大约一天的时间在自己的代码中解决了另一种可能性:

  • 信号是在超类及其子类中定义的。 connect() 调用对子类指针进行操作,但信号是从超类代码发出的。解决方案是从子类中删除信号声明,无论如何,该信号声明都是错误的。

One additional possibility which I just took about a day to solve in my own code:

  • The signal is defined in a superclass AND its subclass. The connect() call was operating on a subclass pointer, but the signal was emitted from the superclass code. The solution was to remove the signal declaration from the subclass, which was there by mistake anyway.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文