为什么我的插槽没有被调用?
我有这个类:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
所有包含信号或槽的类都必须在其声明顶部提及 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
其他一些可能性:
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).
我花了大约一天的时间在自己的代码中解决了另一种可能性:
One additional possibility which I just took about a day to solve in my own code: