Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to add multiple icons to TreeItems of QAbstractTreeModel
Forum Updated to NodeBB v4.3 + New Features

How to add multiple icons to TreeItems of QAbstractTreeModel

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 808 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.
  • S Offline
    S Offline
    summit
    wrote on last edited by
    #1

    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();
        }
    }
    
    JonBJ 1 Reply Last reply
    0
    • S summit

      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();
          }
      }
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • S Offline
        S Offline
        summit
        wrote on last edited by
        #3

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

        JonBJ B 2 Replies Last reply
        0
        • S summit

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

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

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

          1 Reply Last reply
          5
          • S summit

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

            B Offline
            B Offline
            Bonnie
            wrote on last edited by
            #5

            @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.
            1 Reply Last reply
            5

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved