Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to setData with QJsonObject to QListWidgetItem ?
Qt 6.12 LTS is out! See what's new in the release blog

How to setData with QJsonObject to QListWidgetItem ?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 994 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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by SPlatten
    #1

    I have a QListWidget, I am populating the list with data from a QJsonArray where each element of the array is a QJsonObect. I am trying to use the QListWidgetItem setData method to add the JSON data:

    QListWidgetItem* pobjItem = item(intRow);
    
     if ( pobjItem == nullptr ) {
        pobjItem = new QListWidgetItem(this);
    }
    if ( pobjItem != nullptr ) {
        pobjItem->setData(Qt::UserRole, objRef);
        QVariant varTemp = pobjItem->data(Qt::UserRole);
    
        if ( varTemp.isValid() == true ) {
            QJsonDocument objDoc(QJsonDocument::fromJson(varTemp.toByteArray()));
            QJsonValue objVerified(objDoc.object());
    qdbg() << "HACK!";
        }
        pobjItem->setText(citrFound->toString());
    }
    

    Using Qt Creator I can see that objRef is populated and contains a valid JSON object, the code that follows the setData call is just to verify that the data is stored correct, however although varTemp.isValid returns true the byte array is empty and objVerified does not contain the data that was set.

    How can I fix this to store a JSON object with each item?

    Kind Regards,
    Sy

    jsulmJ JonBJ 2 Replies Last reply
    0
    • jsulmJ jsulm

      @SPlatten said in How to setData with QJsonObject to QListWidgetItem ?:

      varTemp.toByteArray()

      What does this return?

      SPlattenS Offline
      SPlattenS Offline
      SPlatten
      wrote on last edited by
      #4

      @jsulm , just solved this:

                  QListWidgetItem* pobjItem = item(intRow);
      
                  if ( pobjItem == nullptr ) {
                      pobjItem = new QListWidgetItem(this);
                  }
                  if ( pobjItem != nullptr ) {
                      QVariant varTemp(objRef), varVerify;
                      pobjItem->setData(Qt::UserRole, varTemp);
                      varVerify = pobjItem->data(Qt::UserRole);
                      if ( varVerify.isValid() == true ) {
                          QJsonValue objVerified(varVerify.toJsonValue());
      qdbg() << "HACK!";
                      }
                      pobjItem->setText(citrFound->toString());
                  }
      
      

      Kind Regards,
      Sy

      1 Reply Last reply
      0
      • SPlattenS SPlatten

        I have a QListWidget, I am populating the list with data from a QJsonArray where each element of the array is a QJsonObect. I am trying to use the QListWidgetItem setData method to add the JSON data:

        QListWidgetItem* pobjItem = item(intRow);
        
         if ( pobjItem == nullptr ) {
            pobjItem = new QListWidgetItem(this);
        }
        if ( pobjItem != nullptr ) {
            pobjItem->setData(Qt::UserRole, objRef);
            QVariant varTemp = pobjItem->data(Qt::UserRole);
        
            if ( varTemp.isValid() == true ) {
                QJsonDocument objDoc(QJsonDocument::fromJson(varTemp.toByteArray()));
                QJsonValue objVerified(objDoc.object());
        qdbg() << "HACK!";
            }
            pobjItem->setText(citrFound->toString());
        }
        

        Using Qt Creator I can see that objRef is populated and contains a valid JSON object, the code that follows the setData call is just to verify that the data is stored correct, however although varTemp.isValid returns true the byte array is empty and objVerified does not contain the data that was set.

        How can I fix this to store a JSON object with each item?

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @SPlatten said in How to setData with QJsonObject to QListWidgetItem ?:

        varTemp.toByteArray()

        What does this return?

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        SPlattenS 1 Reply Last reply
        1
        • SPlattenS SPlatten

          I have a QListWidget, I am populating the list with data from a QJsonArray where each element of the array is a QJsonObect. I am trying to use the QListWidgetItem setData method to add the JSON data:

          QListWidgetItem* pobjItem = item(intRow);
          
           if ( pobjItem == nullptr ) {
              pobjItem = new QListWidgetItem(this);
          }
          if ( pobjItem != nullptr ) {
              pobjItem->setData(Qt::UserRole, objRef);
              QVariant varTemp = pobjItem->data(Qt::UserRole);
          
              if ( varTemp.isValid() == true ) {
                  QJsonDocument objDoc(QJsonDocument::fromJson(varTemp.toByteArray()));
                  QJsonValue objVerified(objDoc.object());
          qdbg() << "HACK!";
              }
              pobjItem->setText(citrFound->toString());
          }
          

          Using Qt Creator I can see that objRef is populated and contains a valid JSON object, the code that follows the setData call is just to verify that the data is stored correct, however although varTemp.isValid returns true the byte array is empty and objVerified does not contain the data that was set.

          How can I fix this to store a JSON object with each item?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #3

          @SPlatten
          setData() returns a bool for success/failure, which you should always check in code.

          1 Reply Last reply
          1
          • jsulmJ jsulm

            @SPlatten said in How to setData with QJsonObject to QListWidgetItem ?:

            varTemp.toByteArray()

            What does this return?

            SPlattenS Offline
            SPlattenS Offline
            SPlatten
            wrote on last edited by
            #4

            @jsulm , just solved this:

                        QListWidgetItem* pobjItem = item(intRow);
            
                        if ( pobjItem == nullptr ) {
                            pobjItem = new QListWidgetItem(this);
                        }
                        if ( pobjItem != nullptr ) {
                            QVariant varTemp(objRef), varVerify;
                            pobjItem->setData(Qt::UserRole, varTemp);
                            varVerify = pobjItem->data(Qt::UserRole);
                            if ( varVerify.isValid() == true ) {
                                QJsonValue objVerified(varVerify.toJsonValue());
            qdbg() << "HACK!";
                            }
                            pobjItem->setText(citrFound->toString());
                        }
            
            

            Kind Regards,
            Sy

            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
            • Unsolved