Qt:如何通知对象涉及它的信号槽连接已经建立

Qt: How to notify an object that a signal slot connection involving it has been established

我想让obj_a在建立信号槽连接后立即发出signal_func()。我的问题是 obj_a 如何在建立时检测到这种连接? QObject::connect 会在建立连接时发出信号吗?

//main function
int main( int argc, char ** argv )
{
  QCoreApplication app(argc, argv);
  A obj_a; //A is a child class of QObject
  B obj_b; //B is a child class of QObject
  // initialize obj_a, obj_b...
  QObject::connect( &obj_a, &A::signal_func, &obj_b, &B::slot_func ); //how can obj_a detect the establishment of this connection?
  app.exec();
}

根据这个 link from the Qt documentation, this function return a QMetaObject::Connection, which has a method that indicates whether the connection is established successfully or not. You may find bool() 对你的情况有用。当布尔值是 true 时,您可以用 obj_a 做任何事情。 我希望这可以帮助你解决你的问题。