Set QVector data to QTreeWidgetItem



  • Hi! I want to write QVector data to QTreeWidgetItem (QTreeWidget) in 5 columns.

    Example:
    1 - Name (+ icon)
    2 - Version
    3 - Pubisher
    4 - installLocation
    5 - uninstallLocation

    struct AppsData {
        QIcon icon;
        QString name;
        QString version;
        QString publisher;
        QString installLocation;
        QString uninstallLocation;
    };
    
    void Test::setAppData(QVector<AppsData> data)
    {
        /*
        QVectorIterator<AppsData> i(data);
    
        while (i.hasNext()) {
            allApplicationsItem = new QTreeWidgetItem(allApplications);
            allApplicationsItem->setText(0, i.next());
        }
        */
    
        for (int i = 0; i < data.count(); i++) {
            allApplicationsItem = new QTreeWidgetItem(allApplications);
            allApplicationsItem->setText(0, data.value(i));
        }
    }
    

    I get error:

    error: C2664: 'void QTreeWidgetItem::setText(int,const QString &)': cannot convert argument 2 from 'AppsData' to 'const QString &'
    

    How to get QVector structure data and set it in QTreeWidgetItem? Thanks in advance.



  • 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



  • @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));
        }
    


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



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



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



  • 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".


  • Moderators

    @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;
    }
    

  • Moderators

    @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
        }
    }
    


  • @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?


  • Moderators

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


  • @jsulm

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

    Screenshot:
    alt text


  • Moderators

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


  • @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);
    }
    

  • Moderators

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



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



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


Log in to reply
 

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