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. Centering DecorationRole icon in QTableView cell
Forum Updated to NodeBB v4.3 + New Features

Centering DecorationRole icon in QTableView cell

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 12.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.
  • A Offline
    A Offline
    ArmanSom
    wrote on last edited by
    #1

    I've subclassed QStyledItemDelegate and overrode the paint function. Then, I set the delegate appropriately. Here is the paint override code below:

    {
        QStyleOptionViewItem opt = option;
        opt.decorationAlignment = Qt::AlignHCenter;
        QStyledItemDelegate::paint(painter, opt, index);
    }
    

    Even so, the icon does not center. However, if I set:

    opt.decorationPosition = QStyleOptionViewItem::Top;
    

    ...then the icon does center horizontally. However, it is pushed up to the "top" which is not what I want. How can I center my icon horizontally and vertically?

    1 Reply Last reply
    1
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What if you use Qt::AlignCenter ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      A 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        What if you use Qt::AlignCenter ?

        A Offline
        A Offline
        ArmanSom
        wrote on last edited by
        #3

        @SGaist Unfortunately, that makes no difference.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Can you provide a minimal compilable sample that shows this ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          A 1 Reply Last reply
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            Just to understand better, do you want the text to be painted overlapping the decoration?

            "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

            A 1 Reply Last reply
            0
            • SGaistS SGaist

              Can you provide a minimal compilable sample that shows this ?

              A Offline
              A Offline
              ArmanSom
              wrote on last edited by
              #6

              @SGaist Sure, here it is:

              class Model : public QAbstractTableModel
              {
                  int rows;
                  int columns;
                  QIcon flagIcon;
              
                public:
                  explicit Model(QObject * parent = nullptr) : QAbstractTableModel(parent)
                  {
                      rows = 3;
                      columns = 3;
                      flagIcon.addFile(QCoreApplication::applicationDirPath() + "/flag.png");
                  }
              
                  int rowCount(const QModelIndex &parent = QModelIndex()) const { return rows; }
                  int columnCount(const QModelIndex &parent = QModelIndex()) const { return columns; }
                  QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const
                  {
                      if (role == Qt::DecorationRole)
                          return flagIcon;
              
                      else return QVariant();
                  }
              };
              
              class FlagDelegate : public QStyledItemDelegate
              {
              public:
                  FlagDelegate(QObject * parent) : QStyledItemDelegate(parent) { }
                  void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
                  {
                      QStyleOptionViewItem opt = option;
                      opt.decorationAlignment = Qt::AlignCenter;
                      QStyledItemDelegate::paint(painter, opt, index);
                  }
              };
              
              int main(int argc, char *argv[])
              {
                  QApplication a(argc, argv);
              
                  QTableView table;
                  table.setFixedWidth(400);
                  table.setFixedHeight(400);
              
                  Model model(&table);
                  table.setModel(&model);
              
                  FlagDelegate delegate(&table);
                  table.setItemDelegate(&delegate);
              
                  table.show();
              
                  return a.exec();
              }
              

              Again, in the overridden paint method, the icon only centers if I set opt.decorationPosition to Top or Bottom.

              1 Reply Last reply
              0
              • VRoninV VRonin

                Just to understand better, do you want the text to be painted overlapping the decoration?

                A Offline
                A Offline
                ArmanSom
                wrote on last edited by
                #7

                @VRonin Sure, if that's what it takes. That's somewhat inapplicable to my situation because I have only one column that displays icons, but it never displays text.

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  ArmanSom
                  wrote on last edited by
                  #8

                  The solution is to manually draw the pixmap of the icon with the painter object, like so:

                  void FlagDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
                  {
                      QRect rect = option.rect;
                      QIcon icon = qvariant_cast<QIcon>(index.data(Qt::DecorationRole));
                      QPixmap pix = icon.pixmap( rect.size() *= 0.70 );
                  
                      QPoint p = QPoint((rect.width() - pix.width())/2, (rect.height() - pix.height())/2);
                      painter->drawPixmap(rect.topLeft() + p, pix);
                  }
                  
                  1 Reply Last reply
                  2
                  • VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by
                    #9
                    void FlagDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
                    {
                    const QStyle* style = option.widget ? option.widget->style() : qApp->style();
                    style->drawPixmap(option.rect,Qt::AlignCenter,index.data(Qt::DecorationRole).value<QIcon>().pixmap(option.rect.size()));
                    }
                    
                    

                    "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

                    • Login

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