Search code examples
c++qtqlist

What does my list of QList class stores here


I am going through a Qt Project, and I am unable to understand a part of the code on QList. In the following code, I know what Q_ASSERT does. I have misunderstanding what does my List called keyItemPairs will store?

void NTCommunicationController::processStartupMessage(const QJsonObject &params)
{
  Q_ASSERT(m_systemSettings);

  QList<QPair<QString, NTEditorModelItem*>> keyItemPairs =
  {{QString(NTParameterSetMessage::SU_BSP_VERSION),    m_systemSettings->getBspVersion()},
   {QString(NTParameterSetMessage::SU_KERNEL_VERSION), m_systemSettings->getKernelVersion()},
   {QString(NTParameterSetMessage::SU_APP_VERSION),    m_systemSettings->getApplicationVersion()},
   {QString(NTParameterSetMessage::SU_FW_VERSION),     m_systemSettings->getFirmwareVersion()},
   {QString(NTParameterSetMessage::SU_PIN_CODE),       m_systemSettings->getPincodeSetting()}
  };
  applyValuesToModelItems(params, keyItemPairs, true);
}

Solution

  • It stores exactly what its name tells you. It is a list of objects where each element is a pair of values. In this particular case QPair<QString, NTEditorModelItem*>.

    Think of a QPair<> (or analogue std::pair<>) as a way of storing two associated values inside a single object.

    You could achieve the same using a struct with two fields if you're more familiar with such approach. For example:

    struct Entry {
        QString value;
        NTEditorModelItem* model;
    };
    
    QList<Entry> items = {{NTParameterSetMessage::SU_BSP_VERSION, m_systemSettings->getBspVersion()},
                          {NTParameterSetMessage::SU_KERNEL_VERSION), m_systemSettings->getKernelVersion()}
    }
    

    You get pretty much the same functionality. However, using a pair template you don't have to create a separate struct just to bind the values together.