Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QList of object
Qt 6.11 is out! See what's new in the release blog

QList of object

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 849 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TimChev1
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      @
      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).

      (Z(:^

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved