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. Icon in corner of QStandardItem
Forum Updated to NodeBB v4.3 + New Features

Icon in corner of QStandardItem

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 3 Posters 3.1k 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.
  • N Neko

    Thanks for your answer!
    But i don't quite catch the 2nd part.

    "and then implement a custom QStyledItemDelegate subclass that reimplements paint() calling the base implementation an then painting the second icon"

    could you explain that part a little bit more detailed please ?

    Do you mean something like this http://doc.qt.io/qt-5/qtwidgets-itemviews-stardelegate-example.html ?

    mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by mrjj
    #4

    @Neko
    Hi
    First read
    http://doc.qt.io/qt-5/qstyleditemdelegate.html

    bascially its a subclass like ( psudo code. didnt compile it )

    class SmallIconDelegate : public QStyledItemDelegate {
      Q_OBJECT
     public:
      paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const {
        QStyledItemDelegate::paint(painter, option, index); // call base
        // grab icon from data role
        // paint it where u want
      }
    }
    

    update:: yes its like star example :)

    1 Reply Last reply
    3
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #5
      class SmallIconDelegate : public QStyledItemDelegate {
          Q_OBJECT
          Q_DISABLE_COPY(SmallIconDelegate)
      public:
          explicit SmallIconDelegate(QObject* parent = Q_NULLPTR)
              : QStyledItemDelegate(parent)
          {}
          void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const Q_DECL_OVERRIDE {
              QStyledItemDelegate::paint(painter, option, index); // call base
              // grab icon from data role
              const QVariant value = index.data(Qt::DecorationRole + Qt::UserRole);
              if (value.isValid() && !value.isNull()) {
                  QPixmap cornerPixmap;
                  const QSize decorationSize(option.rect.height()/4,option.rect.height()/4);
                  switch (value.type()) {
                  case QVariant::Icon: {
                      QIcon tempIcon = qvariant_cast<QIcon>(value);
                      QIcon::Mode mode;
                      if (!(option.state & QStyle::State_Enabled))
                          mode = QIcon::Disabled;
                      else if (option.state & QStyle::State_Selected)
                          mode = QIcon::Selected;
                      else
                          mode = QIcon::Normal;
                      const QIcon::State state = option.state & QStyle::State_Open ? QIcon::On : QIcon::Off;
                      cornerPixmap = tempIcon.pixmap(decorationSize,mode,state);
                  }
                      break;
                  case QVariant::Color:
                      cornerPixmap = QPixmap(decorationSize);
                      cornerPixmap.fill(qvariant_cast<QColor>(value));
                      break;
                  case QVariant::Image:
                      cornerPixmap = QPixmap::fromImage(qvariant_cast<QImage>(value)).scaled(decorationSize);
                      break;
                  case QVariant::Pixmap:
                      cornerPixmap   = qvariant_cast<QPixmap>(value).scaled(decorationSize);
                      break;
                  default:
                      break;
                  }
                  // paint it where u want
                  if(!cornerPixmap.isNull()){
                      const QWidget *widget = option.widget;
                      QStyle *style = widget ? widget->style() : QApplication::style();
                      style->drawItemPixmap(painter,option.rect,Qt::AlignTop|Qt::AlignLeft,cornerPixmap);
                  }
              }
          }
      };
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      3
      • N Offline
        N Offline
        Neko
        wrote on last edited by
        #6

        Will check it out. Thank you very much guys :)

        1 Reply Last reply
        0
        • N Offline
          N Offline
          Neko
          wrote on last edited by Neko
          #7

          Okay, i'm testing it and it is working!
          Thank you guys!
          Now i just need to figure out how to get it on the bottom right corner. Already tried using

          style->drawItemPixmap(painter, QRect(option.rect.topLeft(), decorationSize), Qt::AlignBottom | Qt::AlignRight, cornerPixmap);

          but it's not working.
          Can't be that hard huh? Thanks again guys, you saved my day

          Edit: got it :)
          style->drawItemPixmap(painter, QRect(option.rect.topLeft(), option.rect.bottomRight()), Qt::AlignBottom | Qt::AlignRight, cornerPixmap);

          1 Reply Last reply
          1
          • N Offline
            N Offline
            Neko
            wrote on last edited by
            #8
            This post is deleted!
            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #9

              @Neko said in Icon in corner of QStandardItem:

              QRect(option.rect.topLeft(), option.rect.bottomRight())

              You can just pass option.rect directly

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              2
              • N Offline
                N Offline
                Neko
                wrote on last edited by
                #10

                Already modified this whole line since i want to display 2 different icons next to each other :)

                But thank you.

                1 Reply Last reply
                0
                • N Offline
                  N Offline
                  Neko
                  wrote on last edited by
                  #11

                  Okay, i've got another problem.
                  When i try to change the icons during runtime, or simply add new icons to my listview later on, the updated and new icons wont display any icons anymore. How can i fix this ?

                  Currently i'm doing:

                  onCreate:
                  QIcon IconA = "myPathA"
                  QIcon IconB = "myPathB"
                  QStandardItem * iconItem = new QStandardItem;
                  iconItem->setIcon(QIcon(iconPath));
                  iconItem->setData(QIcon(iconA), Qt::UserRole + 50);
                  iconItem->setData(QIcon(IconB), Qt::UserRole + 51);

                  onUpdate:
                  iconA = "anotherPathA"
                  iconB = "anotehrPathB"
                  model->setData(index, iconA, Qt::UserRole + 50);
                  model->setData(index, iconB, Qt::UserRole + 51);

                  changing my Icon names works. But the small icons seem to make some troubles.

                  1 Reply Last reply
                  0
                  • N Offline
                    N Offline
                    Neko
                    wrote on last edited by
                    #12

                    Guess i found my problem. Sorry for disturbing. Will update

                    1 Reply Last reply
                    1
                    • N Offline
                      N Offline
                      Neko
                      wrote on last edited by Neko
                      #13

                      Used wrong type by accident.
                      Used QString instead of QIcon. No wonder it didnt work
                      Working fine :)

                      1 Reply Last reply
                      1

                      • Login

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