C++ 我怎样才能从另一个班级发出信号?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15176458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:06:41  来源:igfitidea点击:

How can I emit a signal from another class?

c++qtqt4signals-slots

提问by lagoru

I have a problem with my Qt application. I'm trying to emit a signal from within another class (it is a nested class of the one in which the signal is placed).

我的 Qt 应用程序有问题。我正在尝试从另一个类中发出信号(它是放置信号的类的嵌套类)。

I already connected the signal with a slot, which should be fine. But when I try to emit this signal from within this nested class I get the compiler error:

我已经用插槽连接了信号,应该没问题。但是当我尝试从这个嵌套类中发出这个信号时,我得到了编译器错误:

cannot call member function without object

没有对象就不能调用成员函数

What is wrong? I looked for that in Qt documentation but couldn't find reasonable solution or even explanation.

怎么了?我在 Qt 文档中寻找,但找不到合理的解决方案甚至解释。

The simplified class definition looks as follows.

简化的类定义如下所示。

class LogWriter : public QDialog
{   
   Q_OBJECT

public:
   class Log : public QObject
   {
      Q_OBJECT

   public:
      bool print;

      Log(bool _print, QString _color, QObject *obj = NULL)
         : QObject(obj)
      {
         print = _print;
         color = _color;
      }
   };

   LogWriter(QWidget * parent = 0);
   ~LogWriter();

public slots:
   void setMinVal();
   void setMediumVal();
   void setHighVal();
   void cleanWindow();
   void appendText(QString &text);

signals:
   void signalLogAppend(QString);
};

I connect the signal of an instance LOWof the LogWriter in the client code to some slot using the following function call:

LOW使用以下函数调用将客户端代码中 LogWriter实例的信号连接到某个插槽:

connect(&LOW, SIGNAL(signalLogAppend(QString)),
        this, SLOT(appendText(QString&)),
        Qt::DirectConnection);

回答by leemes

To understand the issue you have to understand how signals are emitted:

要了解这个问题,您必须了解信号是如何发出的:

They are simply a non-static member function call and thus require an instance to be called on (the "sender"). Typically, this instance is this(if you emit the signal from within another non-static member function of the same class), so the call syntax becomes a normal function call without any (literal) instance. The emitkeyword is optional and is simply a macro which expands to nothing. The following four versions are all the same when written in a member function of the same class which contains the signal:

它们只是一个非静态成员函数调用,因此需要调用一个实例(“发送者”)。通常,此实例是this(如果您从同一类的另一个非静态成员函数中发出信号),因此调用语法变为没有任何(文字)实例的普通函数调用。该emit关键字是可选的,仅仅是其扩展到任何一个宏。以下四个版本写在包含信号的同一个类的成员函数中时,都是一样的:

emit this->signalLogAppend("foo");
emit signalLogAppend("foo");
this->signalLogAppend("foo");
signalLogAppend("foo");

If you emit the signal of the outer class from within the inner class, the thispointer refers to an instance of the inner class and thus there is some instance missing for the outer class. It's the same like if you call any other function of the outer class from within the inner class: the compiler doesn't know on which object instance (of the outer class) to call it on. So you have to write something like:

如果您从内部类内部发出外部类的信号,则this指针指向内部类的一个实例,因此外部类缺少一些实例。这与从内部类中调用外部类的任何其他函数是一样的:编译器不知道在哪个(外部类的)对象实例上调用它。所以你必须写一些类似的东西:

emit someLogWriter->signalLogAppend("foo");

Here, someLogWriteris the instance of LogWriterfor which you want to emit the signal.

这里,someLogWriterLogWriter您要为其发出信号的实例。