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

Set QVector data to QTreeWidgetItem

Scheduled Pinned Locked Moved Unsolved General and Desktop
17 Posts 3 Posters 5.4k Views
  • 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.
  • gangerMG gangerM

    Hi,

    This is a quess since I don't know how the QTreeWidgetItem works.
    your variable allApplicationsItem will get out of scope since it is inside a loop
    The next thing is that google says QTreeWidgetItem needs to used with QTreeWidget.
    I think people who have used this can help you alot better if you post some debug info

    Greatings,
    gangerM

    Cobra91151C Offline
    Cobra91151C Offline
    Cobra91151
    wrote on last edited by Cobra91151
    #3

    @gangerM

    Thanks but this is just a small example. The actual what I want is to get all apps data from registry and write it in appropriate columns with vector or without.

    Here is what I want: https://forum.qt.io/topic/79753/parsing-icon-for-applications-issue/2

    Code:

    With vector:

    struct AppsData {
        QList<QIcon> icons;
        QStringList name;
        QStringList version;
        QStringList publisher;
        QStringList installLocation;
        QStringList uninstallLocation;
    };
    
    void Test::setAppData(QList<QIcon> icons, QStringList names, QStringList versions, QStringList publishers, QStringList installLocations, QStringList uninstallLocations)
    {
        AppsData appsData;
        appsData.icons = icons;
        appsData.name = names;
        appsData.version = versions;
        appsData.publisher = publishers;
        appsData.installLocation = installLocations;
        appsData.uninstallLocation = uninstallLocations;
    
        QVector<AppsData> dataVector;
        dataVector.push_back(appsData);
    }
    

    Or without:

    for (int i = 0; i < uninstallLocations.count(); i++) {
            allApplicationsItem = new QTreeWidgetItem(allApplications);
            allApplicationsItem->setText(0, names.at(i));
            allApplicationsItem->setText(1, versions.at(i));
            allApplicationsItem->setText(2, publishers.at(i));
            allApplicationsItem->setText(3, installLocations.at(i));
            allApplicationsItem->setText(4, uninstallLocations.at(i));
        }
    
    1 Reply Last reply
    0
    • gangerMG Offline
      gangerMG Offline
      gangerM
      wrote on last edited by
      #4

      @Cobra91151

      The problem in your link is that 'programIcon' is only set when there is a new icon
      This means that if an application has no icon it will use the icon that was there previously

      I'll advice you to search some things about scope, pointers, references (and with that memory management) for c++
      Maybe make a function that copies from 1 vector to another and after that use the QTreeWidgetItem. (So you can try some things)
      I hope you can do something with this...
      I can't really grasp what you are trying to do with the vector and I think somewhere around there is the problem.

      Cobra91151C 1 Reply Last reply
      0
      • gangerMG gangerM

        @Cobra91151

        The problem in your link is that 'programIcon' is only set when there is a new icon
        This means that if an application has no icon it will use the icon that was there previously

        I'll advice you to search some things about scope, pointers, references (and with that memory management) for c++
        Maybe make a function that copies from 1 vector to another and after that use the QTreeWidgetItem. (So you can try some things)
        I hope you can do something with this...
        I can't really grasp what you are trying to do with the vector and I think somewhere around there is the problem.

        Cobra91151C Offline
        Cobra91151C Offline
        Cobra91151
        wrote on last edited by Cobra91151
        #5

        @gangerM

        I think that I don't need vectors at all in this case, it just complicates the issue, all data works fine but the problem is with icons. When icon isNull() I set default icon but then it sets on all apps to default one. So I will change code back when all works and try to fix the issue with icons.

        1 Reply Last reply
        0
        • Cobra91151C Offline
          Cobra91151C Offline
          Cobra91151
          wrote on last edited by
          #6

          The main post has been updated to illustrate the current issue.

          1 Reply Last reply
          0
          • Cobra91151C Offline
            Cobra91151C Offline
            Cobra91151
            wrote on last edited by
            #7
            void Test::setAppData(QVector<AppsData> data)
            {
                qDebug() << data.data()->name;
            }
            

            This code return only 1 name value: "HHD Software Free Hex Editor Neo 6.24".

            jsulmJ 2 Replies Last reply
            0
            • Cobra91151C Cobra91151
              void Test::setAppData(QVector<AppsData> data)
              {
                  qDebug() << data.data()->name;
              }
              

              This code return only 1 name value: "HHD Software Free Hex Editor Neo 6.24".

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

              @Cobra91151 What do you expect it to return?
              Why do you use data()?
              Just do

              void Test::setAppData(QVector<AppsData> data)
              {
                  for (AppsData element : data)
                      qDebug() << element.name;
              }
              

              This will print out names of all elements in the vector. Alternative:

              
              void Test::setAppData(QVector<AppsData> data)
              {
                  for (int i = 0; i < data.size(); ++i)
                      qDebug() << data[i].name;
              }
              

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

              1 Reply Last reply
              2
              • Cobra91151C Cobra91151
                void Test::setAppData(QVector<AppsData> data)
                {
                    qDebug() << data.data()->name;
                }
                

                This code return only 1 name value: "HHD Software Free Hex Editor Neo 6.24".

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

                @Cobra91151

                void Test::setAppData(QVector<AppsData> data)
                {
                    for (int i = 0; i < data.count(); i++) {
                        allApplicationsItem = new QTreeWidgetItem(allApplications);
                        allApplicationsItem->setText(0, data[i].name); // I assume you want to set name, is this correct? You cannot just use the whole struct as you need to pass a string
                    }
                }
                

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

                Cobra91151C 1 Reply Last reply
                2
                • jsulmJ jsulm

                  @Cobra91151

                  void Test::setAppData(QVector<AppsData> data)
                  {
                      for (int i = 0; i < data.count(); i++) {
                          allApplicationsItem = new QTreeWidgetItem(allApplications);
                          allApplicationsItem->setText(0, data[i].name); // I assume you want to set name, is this correct? You cannot just use the whole struct as you need to pass a string
                      }
                  }
                  
                  Cobra91151C Offline
                  Cobra91151C Offline
                  Cobra91151
                  wrote on last edited by
                  #10

                  @jsulm

                  Thanks. But I have figured it out how to retrieve data from QVector structure. The only problem is, it sets only one row. I have checked loop and it sets only one.

                  Code:

                      AppsData appsData;
                      QVector<AppsData> dataVector;
                  
                      for (QString key : allCurrentUserKeys) {
                           if (!key.isEmpty()) {
                              if (key.contains("DisplayIcon") && registryKeyCurrentUser.value(key).toString() != "") {
                                  displayIcon = registryKeyCurrentUser.value(key).toString();
                  
                                  if (displayIcon.contains("\"")) {
                                      displayIconSplitted = displayIcon.split("\"");
                                      iconSplittedData = displayIconSplitted.value(displayIconSplitted.length() - 2);
                                      iconFile = iconSplittedData;
                                  } else if (displayIcon.contains(",")) {
                                      displayIconSplitted = displayIcon.split(",");
                                      iconSplittedData = displayIconSplitted.value(displayIconSplitted.length() - 2);
                                      iconFile = iconSplittedData;
                                  } else {
                                      iconFile = displayIcon;
                                  }
                  
                                  if (iconFile.contains(".ico")) {
                                      programIcon = QIcon(iconFile);
                                  } else {
                                      QFileInfo fileInfo(iconFile);
                                      programIcon = fileIconProvider.icon(fileInfo);
                                  }
                  
                                  appsData.icon = programIcon;
                                  //programIcon = setDefaultIcon();
                              }
                  
                              if (key.contains("DisplayName") && registryKeyCurrentUser.value(key).toString() != "") {
                                  displayName = registryKeyCurrentUser.value(key).toString();
                                  appsData.name = displayName;
                                  //emit appData(programIcon, displayName);
                              }
                          }
                      }
                  
                      dataVector.push_back(appsData);
                      emit appData(dataVector);
                  

                  For example there are 5 entries in registry but only latest is set to a QVector structure. How to get all registry entries?

                  jsulmJ 1 Reply Last reply
                  0
                  • Cobra91151C Cobra91151

                    @jsulm

                    Thanks. But I have figured it out how to retrieve data from QVector structure. The only problem is, it sets only one row. I have checked loop and it sets only one.

                    Code:

                        AppsData appsData;
                        QVector<AppsData> dataVector;
                    
                        for (QString key : allCurrentUserKeys) {
                             if (!key.isEmpty()) {
                                if (key.contains("DisplayIcon") && registryKeyCurrentUser.value(key).toString() != "") {
                                    displayIcon = registryKeyCurrentUser.value(key).toString();
                    
                                    if (displayIcon.contains("\"")) {
                                        displayIconSplitted = displayIcon.split("\"");
                                        iconSplittedData = displayIconSplitted.value(displayIconSplitted.length() - 2);
                                        iconFile = iconSplittedData;
                                    } else if (displayIcon.contains(",")) {
                                        displayIconSplitted = displayIcon.split(",");
                                        iconSplittedData = displayIconSplitted.value(displayIconSplitted.length() - 2);
                                        iconFile = iconSplittedData;
                                    } else {
                                        iconFile = displayIcon;
                                    }
                    
                                    if (iconFile.contains(".ico")) {
                                        programIcon = QIcon(iconFile);
                                    } else {
                                        QFileInfo fileInfo(iconFile);
                                        programIcon = fileIconProvider.icon(fileInfo);
                                    }
                    
                                    appsData.icon = programIcon;
                                    //programIcon = setDefaultIcon();
                                }
                    
                                if (key.contains("DisplayName") && registryKeyCurrentUser.value(key).toString() != "") {
                                    displayName = registryKeyCurrentUser.value(key).toString();
                                    appsData.name = displayName;
                                    //emit appData(programIcon, displayName);
                                }
                            }
                        }
                    
                        dataVector.push_back(appsData);
                        emit appData(dataVector);
                    

                    For example there are 5 entries in registry but only latest is set to a QVector structure. How to get all registry entries?

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

                    @Cobra91151 said in Set QVector data to QTreeWidgetItem:

                    dataVector.push_back(appsData);

                    Shouldn't

                    dataVector.push_back(appsData);
                    

                    be inside the loop?

                    
                        QVector<AppsData> dataVector;
                    
                        for (QString key : allCurrentUserKeys) {
                             AppsData appsData;
                             if (!key.isEmpty()) {
                                if (key.contains("DisplayIcon") && registryKeyCurrentUser.value(key).toString() != "") {
                                    displayIcon = registryKeyCurrentUser.value(key).toString();
                    
                                    if (displayIcon.contains("\"")) {
                                        displayIconSplitted = displayIcon.split("\"");
                                        iconSplittedData = displayIconSplitted.value(displayIconSplitted.length() - 2);
                                        iconFile = iconSplittedData;
                                    } else if (displayIcon.contains(",")) {
                                        displayIconSplitted = displayIcon.split(",");
                                        iconSplittedData = displayIconSplitted.value(displayIconSplitted.length() - 2);
                                        iconFile = iconSplittedData;
                                    } else {
                                        iconFile = displayIcon;
                                    }
                    
                                    if (iconFile.contains(".ico")) {
                                        programIcon = QIcon(iconFile);
                                    } else {
                                        QFileInfo fileInfo(iconFile);
                                        programIcon = fileIconProvider.icon(fileInfo);
                                    }
                    
                                    appsData.icon = programIcon;
                                    //programIcon = setDefaultIcon();
                                }
                    
                                if (key.contains("DisplayName") && registryKeyCurrentUser.value(key).toString() != "") {
                                    displayName = registryKeyCurrentUser.value(key).toString();
                                    appsData.name = displayName;
                                    //emit appData(programIcon, displayName);
                                }
                            }
                            dataVector.push_back(appsData);
                        }
                    
                        
                        emit appData(dataVector);
                    

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

                    Cobra91151C 1 Reply Last reply
                    1
                    • jsulmJ jsulm

                      @Cobra91151 said in Set QVector data to QTreeWidgetItem:

                      dataVector.push_back(appsData);

                      Shouldn't

                      dataVector.push_back(appsData);
                      

                      be inside the loop?

                      
                          QVector<AppsData> dataVector;
                      
                          for (QString key : allCurrentUserKeys) {
                               AppsData appsData;
                               if (!key.isEmpty()) {
                                  if (key.contains("DisplayIcon") && registryKeyCurrentUser.value(key).toString() != "") {
                                      displayIcon = registryKeyCurrentUser.value(key).toString();
                      
                                      if (displayIcon.contains("\"")) {
                                          displayIconSplitted = displayIcon.split("\"");
                                          iconSplittedData = displayIconSplitted.value(displayIconSplitted.length() - 2);
                                          iconFile = iconSplittedData;
                                      } else if (displayIcon.contains(",")) {
                                          displayIconSplitted = displayIcon.split(",");
                                          iconSplittedData = displayIconSplitted.value(displayIconSplitted.length() - 2);
                                          iconFile = iconSplittedData;
                                      } else {
                                          iconFile = displayIcon;
                                      }
                      
                                      if (iconFile.contains(".ico")) {
                                          programIcon = QIcon(iconFile);
                                      } else {
                                          QFileInfo fileInfo(iconFile);
                                          programIcon = fileIconProvider.icon(fileInfo);
                                      }
                      
                                      appsData.icon = programIcon;
                                      //programIcon = setDefaultIcon();
                                  }
                      
                                  if (key.contains("DisplayName") && registryKeyCurrentUser.value(key).toString() != "") {
                                      displayName = registryKeyCurrentUser.value(key).toString();
                                      appsData.name = displayName;
                                      //emit appData(programIcon, displayName);
                                  }
                              }
                              dataVector.push_back(appsData);
                          }
                      
                          
                          emit appData(dataVector);
                      
                      Cobra91151C Offline
                      Cobra91151C Offline
                      Cobra91151
                      wrote on last edited by
                      #12

                      @jsulm

                      It should be, I have tried it also, but it returns multiple the same values.

                      Screenshot:
                      alt text

                      jsulmJ 1 Reply Last reply
                      0
                      • Cobra91151C Cobra91151

                        @jsulm

                        It should be, I have tried it also, but it returns multiple the same values.

                        Screenshot:
                        alt text

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

                        @Cobra91151 You should move

                        dataVector.push_back(appsData);
                        

                        into

                        if (!key.isEmpty()) {
                                    if (key.contains("DisplayIcon") && registryKeyCurrentUser.value(key).toString() != "") {
                        

                        So

                        QVector<AppsData> dataVector;
                        
                            for (QString key : allCurrentUserKeys) {
                                 AppsData appsData;
                                 if (!key.isEmpty()) {
                                    if (key.contains("DisplayIcon") && registryKeyCurrentUser.value(key).toString() != "") {
                                        displayIcon = registryKeyCurrentUser.value(key).toString();
                        
                                        if (displayIcon.contains("\"")) {
                                            displayIconSplitted = displayIcon.split("\"");
                                            iconSplittedData = displayIconSplitted.value(displayIconSplitted.length() - 2);
                                            iconFile = iconSplittedData;
                                        } else if (displayIcon.contains(",")) {
                                            displayIconSplitted = displayIcon.split(",");
                                            iconSplittedData = displayIconSplitted.value(displayIconSplitted.length() - 2);
                                            iconFile = iconSplittedData;
                                        } else {
                                            iconFile = displayIcon;
                                        }
                        
                                        if (iconFile.contains(".ico")) {
                                            programIcon = QIcon(iconFile);
                                        } else {
                                            QFileInfo fileInfo(iconFile);
                                            programIcon = fileIconProvider.icon(fileInfo);
                                        }
                        
                                        appsData.icon = programIcon;
                                        //programIcon = setDefaultIcon();
                                       if (key.contains("DisplayName") && registryKeyCurrentUser.value(key).toString() != "") {
                                        displayName = registryKeyCurrentUser.value(key).toString();
                                        appsData.name = displayName;
                                        //emit appData(programIcon, displayName);
                                      }
                                      dataVector.push_back(appsData);
                                    }
                               }
                            }
                        
                            emit appData(dataVector);

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

                        Cobra91151C 1 Reply Last reply
                        1
                        • jsulmJ jsulm

                          @Cobra91151 You should move

                          dataVector.push_back(appsData);
                          

                          into

                          if (!key.isEmpty()) {
                                      if (key.contains("DisplayIcon") && registryKeyCurrentUser.value(key).toString() != "") {
                          

                          So

                          QVector<AppsData> dataVector;
                          
                              for (QString key : allCurrentUserKeys) {
                                   AppsData appsData;
                                   if (!key.isEmpty()) {
                                      if (key.contains("DisplayIcon") && registryKeyCurrentUser.value(key).toString() != "") {
                                          displayIcon = registryKeyCurrentUser.value(key).toString();
                          
                                          if (displayIcon.contains("\"")) {
                                              displayIconSplitted = displayIcon.split("\"");
                                              iconSplittedData = displayIconSplitted.value(displayIconSplitted.length() - 2);
                                              iconFile = iconSplittedData;
                                          } else if (displayIcon.contains(",")) {
                                              displayIconSplitted = displayIcon.split(",");
                                              iconSplittedData = displayIconSplitted.value(displayIconSplitted.length() - 2);
                                              iconFile = iconSplittedData;
                                          } else {
                                              iconFile = displayIcon;
                                          }
                          
                                          if (iconFile.contains(".ico")) {
                                              programIcon = QIcon(iconFile);
                                          } else {
                                              QFileInfo fileInfo(iconFile);
                                              programIcon = fileIconProvider.icon(fileInfo);
                                          }
                          
                                          appsData.icon = programIcon;
                                          //programIcon = setDefaultIcon();
                                         if (key.contains("DisplayName") && registryKeyCurrentUser.value(key).toString() != "") {
                                          displayName = registryKeyCurrentUser.value(key).toString();
                                          appsData.name = displayName;
                                          //emit appData(programIcon, displayName);
                                        }
                                        dataVector.push_back(appsData);
                                      }
                                 }
                              }
                          
                              emit appData(dataVector);
                          Cobra91151C Offline
                          Cobra91151C Offline
                          Cobra91151
                          wrote on last edited by Cobra91151
                          #14

                          @jsulm

                          Yes, but the apps data will not be in appropriate columns.

                          Screenshot:
                          alt text

                          Code:

                          for (int i = 0; i < vectorData.count(); i++) {
                                allApplicationsItem = new QTreeWidgetItem(allApplications);
                                allApplicationsItem->setIcon(0, vectorData[i].icon);
                                allApplicationsItem->setText(0, vectorData[i].name);
                                allApplicationsItem->setText(1, vectorData[i].version);
                                allApplicationsItem->setText(2, vectorData[i].publisher);
                                allApplicationsItem->setText(3, vectorData[i].installLocation);
                                allApplicationsItem->setText(4, vectorData[i].uninstallLocation);
                          }
                          
                          jsulmJ 1 Reply Last reply
                          0
                          • Cobra91151C Cobra91151

                            @jsulm

                            Yes, but the apps data will not be in appropriate columns.

                            Screenshot:
                            alt text

                            Code:

                            for (int i = 0; i < vectorData.count(); i++) {
                                  allApplicationsItem = new QTreeWidgetItem(allApplications);
                                  allApplicationsItem->setIcon(0, vectorData[i].icon);
                                  allApplicationsItem->setText(0, vectorData[i].name);
                                  allApplicationsItem->setText(1, vectorData[i].version);
                                  allApplicationsItem->setText(2, vectorData[i].publisher);
                                  allApplicationsItem->setText(3, vectorData[i].installLocation);
                                  allApplicationsItem->setText(4, vectorData[i].uninstallLocation);
                            }
                            
                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #15

                            @Cobra91151 It looks like you don't get icons for all apps - you need to fix your code, I currently don't have time for this.

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

                            Cobra91151C 1 Reply Last reply
                            0
                            • jsulmJ jsulm

                              @Cobra91151 It looks like you don't get icons for all apps - you need to fix your code, I currently don't have time for this.

                              Cobra91151C Offline
                              Cobra91151C Offline
                              Cobra91151
                              wrote on last edited by Cobra91151
                              #16

                              @jsulm

                              In this case I have all apps icons but we dataVector.push_back(appsData); setting in if (key.contains("DisplayName") or icons key. That's why this issue, one first row is empty.

                              I have changed to dataVector.push_back(appsData); on the last check:

                              if (key.contains("UninstallString") && registryKeyCurrentUser.value(key).toString() != "" && !key.contains("QuietUninstallString")) {
                                  uninstallLocation = registryKeyCurrentUser.value(key).toString();
                                  appsData.uninstallLocation = uninstallLocation;
                                  dataVector.push_back(appsData);
                              }
                              

                              And now it setting all data properly, but I will check for other registry keys.

                              Screenshot (HKEY_CURRENT_USER):
                              alt text

                              1 Reply Last reply
                              0
                              • Cobra91151C Offline
                                Cobra91151C Offline
                                Cobra91151
                                wrote on last edited by
                                #17

                                The same issue with icons still exists. It's time to change code to Win API and try to fix this issue.

                                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