how to pass qobject as argument from signal to slot in qt connect

后端 未结 1 1136
不知归路
不知归路 2020-12-29 13:51

My original code passed a QStringList from the signal to the slot and then returned a QList. Everything worked fine but I needed to change both the QStringList and QList in

相关标签:
1条回答
  • 2020-12-29 14:23

    Usually QObject is passed by pointer, not by reference (note that QObject cannot be copied and cannot be passed by value). QObject* is registered as a meta type by default. So creating a signal and a slot with QObject* argument is enough to get them work:

    private slots:
      void test_slot(QObject* object);
    
    signals:
      void test_signal(QObject* object);
    

    Initialization:

    connect(this, SIGNAL(test_signal(QObject*)), this, SLOT(test_slot(QObject*)));
    

    Emitting:

    QObject* object = new QObject();
    emit test_signal(object);
    

    Of course the signal and the slot could be in different classes.

    0 讨论(0)
提交回复
热议问题