简体   繁体   English

我如何从另一个班级发出信号?

[英]How can I emit a signal from another class?

I have a problem with my Qt application. 我的Qt应用程序有问题。 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). 我正试图从另一个类中发出一个信号(它是放置信号的嵌套类)。

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 LOW of the LogWriter in the client code to some slot using the following function call: 我使用以下函数调用将客户端代码中LogWriter的实例LOW的信号连接到某个插槽:

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

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. 通常,此实例是this (如果您从同一个类的另一个非静态成员函数中发出信号),那么调用语法将成为没有任何(文字)实例的正常函数调用。 The emit keyword is optional and is simply a macro which expands to nothing. emit关键字是可选的,只是一个扩展为空的宏。 The following four versions are all the same when written in a member function of the same class which contains the signal: 当写入包含信号的同一类的成员函数时,以下四个版本都是相同的:

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 this pointer refers to an instance of the inner class and thus there is some instance missing for the outer class. 如果从内部类中发出外部类的信号,则this指针引用内部类的实例,因此外部类缺少某些实例。 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: 所以你必须写下这样的东西:

emit someLogWriter->signalLogAppend("foo");

Here, someLogWriter is the instance of LogWriter for which you want to emit the signal. 这里, someLogWriter是要为其发出信号的LogWriter实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM