0

I am creating a Qt Widget based app and I want to be able to add objects to std::list<>(that store objects of some class) at runtime by using button(QPushButton). Is there any way to pass this list by reference so I can store created objects in it?

AFAIK there is no way to use connect(Object1, signal, Object2, slot), because slot can't have any parameters.

If I can't do this the way I explained how can I achieve it?

  • 1
    *"slot can't have any parameters"* -- Where did you hear that? That's not true at all. – JarMan Apr 14 '21 at 17:49
  • I use slots with parameters all the time. With that said it may be some work connecting a signal to that does not have a parameter to a slot with parameters. Depending on the situation you may use several different methods to make it work. – drescherjm Apr 14 '21 at 17:52
  • You can `connect` to a [`lambda`](https://doc.qt.io/qt-5/signalsandslots.html#signals-and-slots-with-default-arguments) with captures. – G.M. Apr 14 '21 at 17:52

1 Answers1

1

Without more context it's difficult to know exactly what you're trying to achieve but... given a variable objects defined as follows...

std::list<QObject *> objects;

You can connect a button's clicked signal to a lambda that captures objects by reference with...

QObject::connect(&button, &QPushButton::clicked,
                 [&objects](bool checked)
                 {

                     /*
                      * Update objects here.
                      */
                 });
G.M.
  • 12,232
  • 2
  • 15
  • 18