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. No Icons in TreeView
Forum Updated to NodeBB v4.3 + New Features

No Icons in TreeView

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 6.5k 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.
  • P Offline
    P Offline
    pracedru
    wrote on last edited by
    #1

    Hi everybody

    I'm trying to make a my own model to be used in a treeview and i'd like to have icons on my items.
    in my model i've implemented the data function as

    @
    QVariant PipeDesignItemModel::data(const QModelIndex &index, int role) const
    {

    if (!index.isValid())
        return QVariant();
    
    if (role != Qt::DisplayRole)
        return QVariant();
    
    QObject * Item ;
    
    Item = static_cast<QObject*>(index.internalPointer());
    
    QVariant retObj;
    QString iconfile = "c:/GreenPipe.ico";
    QIcon icon = QIcon(iconfile);
    qDebug() << icon.isNull() ;
    if (role == Qt::DecorationRole)
        retObj.setValue( QIcon(iconfile));
    retObj.setValue(Item->objectName());
    
    
    return retObj;
    

    }
    @

    However the role is never Qt::DecorationRole so the icon is never shown. What do i have to do to change the role?

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      This role never reaches your if:

      @
      QVariant PipeDesignItemModel::data(const QModelIndex &index, int role) const
      {

      if (!index.isValid())
          return QVariant();
      
      if (role != Qt::DisplayRole) // here you return if item role is decoration :-)
          return QVariant();
      
      // ....
      
      if (role == Qt::DecorationRole)
          retObj.setValue( QIcon(iconfile));
      retObj.setValue(Item->objectName());
      
      
      return retObj;
      

      }
      @

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • P Offline
        P Offline
        pracedru
        wrote on last edited by
        #3

        Whoops.

        You don't want to know how much time i've used at this.
        thanx.

        1 Reply Last reply
        0
        • P Offline
          P Offline
          pracedru
          wrote on last edited by
          #4

          I fixed it and the icon now shows. But not the text.

          @
          QVariant PipeDesignItemModel::data(const QModelIndex &index, int role) const
          {

          if (!index.isValid())
              return QVariant();
          if (index.column()==1 && role != Qt::DisplayRole)
              return QVariant();
          if (index.column()==0 && role != Qt::DecorationRole)
              return QVariant();
          QObject * Item ;
          
          Item = static_cast<QObject*>(index.internalPointer());
          
          QVariant retObj;    
          
          retObj.setValue(Item->objectName());
          
          if (role == Qt::DecorationRole)
              retObj.setValue(greenPipe);
          
          return retObj;
          

          }
          @

          Can i show an icon and text at the same time?

          1 Reply Last reply
          0
          • G Offline
            G Offline
            giesbert
            wrote on last edited by
            #5

            you are showing them in different columns.
            what is your view?
            typically, the roles are used in the same column:

            @
            QVariant PipeDesignItemModel::data(const QModelIndex &index, int role) const
            {
            if (!index.isValid())
            return QVariant();

            QObject* Item = static_cast<QObject*>(index.internalPointer());
            
            QVariant retObj;   
            switch(role)
            {
            case Qt::DisplayRole:
                retObj.setValue(Item->objectName());
                break;
            case Qt::DecorationRole:
                retObj.setValue(greenPipe);
                break;
            }
            
            return retObj;
            

            }@

            this would show icon and text in each column. if you want to seperate by column, do it aditionally...

            Nokia Certified Qt Specialist.
            Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

            1 Reply Last reply
            0
            • P Offline
              P Offline
              pracedru
              wrote on last edited by
              #6

              Oh, i thought QVariant could hold more than one valuetype and that all the types needed would have to be returned. But i understand now that data is called for every role.

              Thanks.

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on last edited by
                #7

                The icon is not in a separate column. So you have the text and the icon in column 0 (and probably more texts in more columns).

                http://www.catb.org/~esr/faqs/smart-questions.html

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  MisteKiste
                  wrote on last edited by
                  #8

                  Can somebody tell me how to resize the icon? I don't get it...

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