c++qtqt5qapplication

Connecting button to create and store an object in Qt C++


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?


Solution

  • 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.
                          */
                     });