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. Image alignment in QTableview
Qt 6.11 is out! See what's new in the release blog

Image alignment in QTableview

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 1.3k 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.
  • Match0umM Offline
    Match0umM Offline
    Match0um
    wrote on last edited by
    #1

    Hi guys !

    Do someone knows how to center a picture in a QTableview cell ?
    Here is my code :

    QStandardItem *itemProfilClient = new QStandardItem();
    itemProfilClient->setData(QVariant(insertClient._profil.scaled(200,200, Qt::KeepAspectRatio)), Qt::DecorationRole);
    itemProfilClient->setTextAlignment(Qt::AlignCenter);
    _model->setItem(2, 3, itemProfilClient);
    

    The issue is that the picture (which is insertClient._profil (QPixmap)) is not centered in the cell, but aligned left.

    Can somebody help me please ? 😊

    eyllanescE 1 Reply Last reply
    0
    • Match0umM Match0um

      Hi guys !

      Do someone knows how to center a picture in a QTableview cell ?
      Here is my code :

      QStandardItem *itemProfilClient = new QStandardItem();
      itemProfilClient->setData(QVariant(insertClient._profil.scaled(200,200, Qt::KeepAspectRatio)), Qt::DecorationRole);
      itemProfilClient->setTextAlignment(Qt::AlignCenter);
      _model->setItem(2, 3, itemProfilClient);
      

      The issue is that the picture (which is insertClient._profil (QPixmap)) is not centered in the cell, but aligned left.

      Can somebody help me please ? 😊

      eyllanescE Offline
      eyllanescE Offline
      eyllanesc
      wrote on last edited by eyllanesc
      #2

      @Match0um You cannot change the alignment of the image using the model, in this case the solution is to use a QProxyStyle:

      class ProxyStyle: public QProxyStyle{
      public:
          using QProxyStyle::QProxyStyle;
          QRect subElementRect(SubElement subElement, const QStyleOption *option, const QWidget *widget) const override{
              QRect rect = QProxyStyle::subElementRect(subElement, option, widget);
              if(subElement == SE_ItemViewItemDecoration){
                  rect.moveCenter(option->rect.center());
              }
              return rect;
          }
      };
      
      view->setStyle(new ProxyStyle(view->style()));
      

      If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

      Match0umM 1 Reply Last reply
      1
      • eyllanescE eyllanesc

        @Match0um You cannot change the alignment of the image using the model, in this case the solution is to use a QProxyStyle:

        class ProxyStyle: public QProxyStyle{
        public:
            using QProxyStyle::QProxyStyle;
            QRect subElementRect(SubElement subElement, const QStyleOption *option, const QWidget *widget) const override{
                QRect rect = QProxyStyle::subElementRect(subElement, option, widget);
                if(subElement == SE_ItemViewItemDecoration){
                    rect.moveCenter(option->rect.center());
                }
                return rect;
            }
        };
        
        view->setStyle(new ProxyStyle(view->style()));
        
        Match0umM Offline
        Match0umM Offline
        Match0um
        wrote on last edited by
        #3

        @eyllanesc Thank you so much it works !

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

          There is no real need to reimplement a proxy style, it can easily be done with a delegate:

          class AlignPicCenterDelegate : public QStyledItemDelegate{
          public:
              using QStyledItemDelegate::QStyledItemDelegate;
          protected:
              void initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const override {
                  QStyledItemDelegate::initStyleOption(option,index);
                  option->decorationAlignment = Qt::AlignCenter;
              }
          };
          

          Then on your view you can call https://doc.qt.io/qt-5/qabstractitemview.html#setItemDelegate

          "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