QList of object



  • Hi,

    I'm developping a Qt application that use the GPIO of my pcDuino (GPIO are just seen as files).
    I use buttons with leds that must switch on when the button is pressed.

    It's working fine, and I want now to make my application more generic and parametrable.
    So, I'm using QSettings to allow the changing some parameters dynamically. What I want to do is choose wich gpio port matches wich buttons and leds in my settings file.
    So, to create dynamically my objects I use a QList I append for all new parameters I'm reading from my file.
    Then, I make a connection to switch on/off my led, regarding the state of the button.

    My problem is that with the parametrable version, using QList, my program never runs into my connect function.
    While my non-parametrable version was perfectly working.

    Here is my code :
    @ settings.beginGroup("LedButton");
    QStringList gpioGroupList = settings.childGroups();
    int nbGPIO = gpioGroupList.count();
    QList<LedButton *> gpioButtonLedList;
    for (int i=0; i<nbGPIO;i++)
    {
    qWarning() << "gpio n°" << i << gpioGroupList[i] << "=" << settings.value(gpioGroupList[i]+"/button").toInt() << "/" << settings.value(gpioGroupList[i]+"/led").toInt();
    GeneralPurposeIO gpioButton(settings.value(gpioGroupList[i]+"/button").toInt());
    if(!gpioButton.open(GeneralPurposeIO::Mode::Input))
    qWarning() << "button"<< i <<"not opened";

        GeneralPurposeIO gpioLed(settings.value(gpioGroupList[i]+"/led").toInt());
        if (!gpioLed.open(GeneralPurposeIO::Mode::Output))
            qWarning() << "led"<< i <<"not opened";
    
        LedButton *ledButton = new LedButton(&gpioLed, &gpioButton, qApp);
        gpioButtonLedList.append(ledButton);
    }
    
    for(int i=0; i<gpioButtonLedList.count();i++)
    {
        QObject::connect(gpioButtonLedList[i], &LedButton::toggled, [](bool checked)
        {
            qWarning() << "checked" << checked;
        });
    }
    

    @

    And here is my non-parametrable version that works :
    

    @ GeneralPurposeIO buttonIO(0);
    if(!buttonIO.open(GeneralPurposeIO::Mode::Input))
    {
    qWarning() << "gpio0 not opened";
    return -1;
    }

    GeneralPurposeIO ledIO(8);
    if (!ledIO.open(GeneralPurposeIO::Mode::Output))
    {
        qWarning() << "gpio8 not opened";
        return -1;
    }
    
    LedButton button08( &ledIO, &buttonIO);
    QObject::connect(&button08, &LedButton::toggled, [](bool checked)
    {
        qWarning() << "button08 :" << checked;
    });@
    

    Does anybody sees a mistake in the first code ?

    Thanks.


  • Moderators

    @
    LedButton *ledButton = new LedButton(&gpioLed, &gpioButton, qApp);
    @

    gpioLed and gpioButton are created on the stack inside the for loop. That means those objects are destroyed when they go out of scope (when loop cycle ends).


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.