Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to add multiple icons to TreeItems of QAbstractTreeModel



  • This is my current code i am only able to insert only one icon to the treeItem.

    How can i add both the icons to the treeItem

    QVariant TreeModel::data(const QModelIndex &index, int role) const
    {
    
        if (!index.isValid())
            return QVariant();
    
        TreeItem *item = getItem(index);
        if (item)
        {
            switch (role)
            {
                case Qt::DisplayRole: case Qt::EditRole:
                    return QString::fromStdString(item->data().GetName());
                    break;
                case Qt::DecorationRole:
                {                           
                    Container *cont = item->GetContainer();
                    if (cont->GetGeometry()->isValid())
                    {                           
                        QString qstrIconName =  cont->GetGeometry()->GetType().c_str();
                        QString qstrIconPath = QCoreApplication::applicationDirPath();
                        QPixmap pixmap;
                        QIcon icon;                 
                        qstrIconPath = qstrIconPath + "/Icons/" + qstrIconName + ".png";
                        pixmap.load(qstrIconPath);
                        icon.addPixmap(pixmap, QIcon::Normal, QIcon::On);               
                        return icon;
                    }
                     // Function returns from here since we have returned the icon
                     // How can i add next icon to the same tree item
    
                    int numberOfFunctions = cont->getNumberOfFunctions();
                    if (numberOfFunctions > 0)
                    {
                        QString qstrIconName = "FUNCTION";
                        QString qstrIconPath = QCoreApplication::applicationDirPath();
                        QPixmap pixmap;
                        QIcon icon;
                        qstrIconPath = qstrIconPath + "/Icons/" + qstrIconName + ".png";
                        pixmap.load(qstrIconPath);
                        icon.addPixmap(pixmap, QIcon::Normal, QIcon::On);
                        return icon;
                    }
                    break;              
                }
            }       
            return QVariant();
        }
    }
    


  • @summit
    I assume that DecorationRole only allows for one icon? I don't know how generically you would expect multiple icons to be accepted/displayed?



  • @JonB Yes lot of time i would expect multiple icons to be displayed , what are the other ways i can achieve this ?



  • @summit
    I will be silent after this, to allow another who knows better than I to help you, but I would assume you must look at QStyledItemDelegate as your way of displaying non-standard things like multiple icons, instead of going via Decoration role.



  • @summit

    1. Add one column for every icon.
    2. Create a big size icon that contains several icons in a row.
    3. Write custom item delegate, draw in your own way.

Log in to reply