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. QTableView and Image
Forum Updated to NodeBB v4.3 + New Features

QTableView and Image

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 4 Posters 4.5k 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.
  • M Offline
    M Offline
    MokJ
    wrote on last edited by
    #1

    I'm drawing images in QTableView by QItemDelegate class.
    now the problem here is , the part of the image , that could fit in the block is only being displayed.

    Another thing is , is it possible to create something like, whenever a block from QTableView is selected ,the image on that particular block should get changed?

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

      can you show us your current code?

      "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
      0
      • M Offline
        M Offline
        MokJ
        wrote on last edited by MokJ
        #3

        Here's the widget class

        MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
        {
                QTableView *view=new QTableView(this);
                view->setShowGrid(false);
                view->setFocus();
                QStandardItemModel *model=new QStandardItemModel(3,2);
                myDelegate *delegate=new myDelegate;
        
                
                view->columnWidth(500);// no matter what I put in here, it remains of the same size
                view->rowHeight(500); //same as mentioned above, I tried to take the height and width
                  //of the image for column width and column height but nothing happened.
        
                view->setModel(model);
                view->setItemDelegate(delegate);
                QStringList SL={"Column 1","Column 2"};
                model->setHorizontalHeaderLabels(SL);
                QStandardItem *item=new QStandardItem;
                model->setItem(0,0,item);
        }
               
        

        And the QItemDelegate class, paint method

        void myDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const{
            const QStandardItemModel *model=static_cast<const QStandardItemModel*>(index.model());
        
             painter->drawImage(option.rect.x(),option.rect.y(),m_image);
        
        }
        
        JonBJ 1 Reply Last reply
        0
        • M MokJ

          Here's the widget class

          MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
          {
                  QTableView *view=new QTableView(this);
                  view->setShowGrid(false);
                  view->setFocus();
                  QStandardItemModel *model=new QStandardItemModel(3,2);
                  myDelegate *delegate=new myDelegate;
          
                  
                  view->columnWidth(500);// no matter what I put in here, it remains of the same size
                  view->rowHeight(500); //same as mentioned above, I tried to take the height and width
                    //of the image for column width and column height but nothing happened.
          
                  view->setModel(model);
                  view->setItemDelegate(delegate);
                  QStringList SL={"Column 1","Column 2"};
                  model->setHorizontalHeaderLabels(SL);
                  QStandardItem *item=new QStandardItem;
                  model->setItem(0,0,item);
          }
                 
          

          And the QItemDelegate class, paint method

          void myDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const{
              const QStandardItemModel *model=static_cast<const QStandardItemModel*>(index.model());
          
               painter->drawImage(option.rect.x(),option.rect.y(),m_image);
          
          }
          
          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by JonB
          #4

          @MokJ said in QTableView and Image:

              view->columnWidth(500);// no matter what I put in here, it remains of the same size
              view->rowHeight(500); //same as mentioned above, I tried to take the height and width
                //of the image for column width and column height but nothing happened.
          

          I'm not saying this is the right way to do it, but these lines do nothing. Look carefully at the documentation: columnWidth/rowHeight are getters, you want the setters here! In general, Qt uses methods named set... for every property you can set.

          M 1 Reply Last reply
          2
          • JonBJ JonB

            @MokJ said in QTableView and Image:

                view->columnWidth(500);// no matter what I put in here, it remains of the same size
                view->rowHeight(500); //same as mentioned above, I tried to take the height and width
                  //of the image for column width and column height but nothing happened.
            

            I'm not saying this is the right way to do it, but these lines do nothing. Look carefully at the documentation: columnWidth/rowHeight are getters, you want the setters here! In general, Qt uses methods named set... for every property you can set.

            M Offline
            M Offline
            MokJ
            wrote on last edited by MokJ
            #5

            @JonB thanks , I've got it, that problem has just been solved.
            Now I'm trying to display a new image to that particular block by selecting it ,is there a way ? I mean the point is ....the rest of the Table must not get affected .

            bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) ;
            

            can it be used or should I go for

            void updateEditorGeometry(QWidget*editor,const QStyleOptionViewItem &option,const QModelIndex &index)const;
            
            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by VRonin
              #6
              • Where does m_image come from?
              • Are you subclassing QItemDelegate or QStyledItemDelegate?
              • Are you implementing myDelegate::sizeHint?

              LEAKFEST!

              The view does not own either the model or the delegate. You need to pass a parent to them or they are leaked

              "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

              M 1 Reply Last reply
              1
              • VRoninV VRonin
                • Where does m_image come from?
                • Are you subclassing QItemDelegate or QStyledItemDelegate?
                • Are you implementing myDelegate::sizeHint?

                LEAKFEST!

                The view does not own either the model or the delegate. You need to pass a parent to them or they are leaked

                M Offline
                M Offline
                MokJ
                wrote on last edited by MokJ
                #7

                thank you @VRonin , It was a mistake of mine ,that I didn't provide the complete information.
                I'm subclassing QItemDelegate....on this note how its different from subclassing QStyledItemDelegate?? I mean Qt document says that

                QStyledItemDelegate uses the current style to paint its items

                whats the current style here !!

                QSize myDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const{
                          return  QSize newSize(70,30);
                }
                
                QImage m_image(":/new/prefix1/d.jpg");
                

                m_image is in private of myDelegate.

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

                  So m_image does not depend at all on what's in the model?

                  @MokJ said in QTableView and Image:

                  whats the current style here !!

                  http://doc.qt.io/qt-5/style-reference.html

                  Let's go step by step. The below should be an improvement already, correct?

                  void myDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const{
                  (option.widget ? option.widget.style() : QApplication::style())->drawItemPixmap(painter,option.rect(),QPixmap::fromImage(m_image.scaled(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

                  M 1 Reply Last reply
                  2
                  • VRoninV VRonin

                    So m_image does not depend at all on what's in the model?

                    @MokJ said in QTableView and Image:

                    whats the current style here !!

                    http://doc.qt.io/qt-5/style-reference.html

                    Let's go step by step. The below should be an improvement already, correct?

                    void myDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const{
                    (option.widget ? option.widget.style() : QApplication::style())->drawItemPixmap(painter,option.rect(),QPixmap::fromImage(m_image.scaled(option.rect().size())));
                    }
                    
                    M Offline
                    M Offline
                    MokJ
                    wrote on last edited by
                    #9

                    @VRonin Thanks
                    yes, m_image doesn't depend on model at all.
                    I'm clear about current style now.

                    Let's go step by step. The below should be an improvement already, correct?

                    yes , after a small change It has improved.
                    So whats the next step !
                    What method should be used to get desired output !

                    @MokJ said in QTableView and Image:

                    Another thing is , is it possible to create something like, whenever a block from QTableView is selected ,the image on that particular block should get changed?

                    VRoninV 1 Reply Last reply
                    0
                    • M MokJ

                      @VRonin Thanks
                      yes, m_image doesn't depend on model at all.
                      I'm clear about current style now.

                      Let's go step by step. The below should be an improvement already, correct?

                      yes , after a small change It has improved.
                      So whats the next step !
                      What method should be used to get desired output !

                      @MokJ said in QTableView and Image:

                      Another thing is , is it possible to create something like, whenever a block from QTableView is selected ,the image on that particular block should get changed?

                      VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by VRonin
                      #10

                      @MokJ said in QTableView and Image:

                      So whats the next step !

                      We make the image depend on the model.
                      In the constructor add:

                      connect(view->selectionModel(),&QItemSelectionModel::selectionChanged,[view](const QItemSelection &selected, const QItemSelection &deselected)->void{
                      QModelIndexList indexList = selected.indexes();
                      for(auto& index : qAsConst(indexList))
                      view->model()->setData(index,true,Qt::UserRole);
                      indexList = deselected.indexes();
                      for(auto& index : qAsConst(indexList))
                      view->model()->setData(index,QVariant(),Qt::UserRole);
                      });
                      

                      now in myDelegate::paint you can add if(index.data(Qt::UserRole).toBool()) to check if an item is selected or not

                      "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

                      M 1 Reply Last reply
                      3
                      • VRoninV VRonin

                        @MokJ said in QTableView and Image:

                        So whats the next step !

                        We make the image depend on the model.
                        In the constructor add:

                        connect(view->selectionModel(),&QItemSelectionModel::selectionChanged,[view](const QItemSelection &selected, const QItemSelection &deselected)->void{
                        QModelIndexList indexList = selected.indexes();
                        for(auto& index : qAsConst(indexList))
                        view->model()->setData(index,true,Qt::UserRole);
                        indexList = deselected.indexes();
                        for(auto& index : qAsConst(indexList))
                        view->model()->setData(index,QVariant(),Qt::UserRole);
                        });
                        

                        now in myDelegate::paint you can add if(index.data(Qt::UserRole).toBool()) to check if an item is selected or not

                        M Offline
                        M Offline
                        MokJ
                        wrote on last edited by
                        #11

                        @VRonin Thank you
                        I have added that snippet too, now question is , How is it making m_image depend on model ?

                        VRoninV 1 Reply Last reply
                        0
                        • M MokJ

                          @VRonin Thank you
                          I have added that snippet too, now question is , How is it making m_image depend on model ?

                          VRoninV Offline
                          VRoninV Offline
                          VRonin
                          wrote on last edited by VRonin
                          #12

                          @MokJ I made a mistake, if(index.data().toBool()) should have been if(index.data(Qt::UserRole).toBool()) fixed above now

                          P.S.
                          if(index.data(Qt::UserRole).isValid()) does the same and it's probably more efficient

                          "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

                          M H 3 Replies Last reply
                          3
                          • VRoninV VRonin

                            @MokJ I made a mistake, if(index.data().toBool()) should have been if(index.data(Qt::UserRole).toBool()) fixed above now

                            P.S.
                            if(index.data(Qt::UserRole).isValid()) does the same and it's probably more efficient

                            M Offline
                            M Offline
                            MokJ
                            wrote on last edited by MokJ
                            #13

                            @VRonin
                            I'm a bit confused about how its supposed to work. I mean once I select an Index it goes inside

                            if(index.data(Qt::UserRole).isValid()) {}
                            

                            What should get implemented in If?

                            I appreciate your help greatly.

                            1 Reply Last reply
                            0
                            • VRoninV VRonin

                              @MokJ I made a mistake, if(index.data().toBool()) should have been if(index.data(Qt::UserRole).toBool()) fixed above now

                              P.S.
                              if(index.data(Qt::UserRole).isValid()) does the same and it's probably more efficient

                              H Offline
                              H Offline
                              hjohn
                              wrote on last edited by hjohn
                              #14

                              @VRonin
                              can we take the Image as string ?

                              M 1 Reply Last reply
                              0
                              • VRoninV VRonin

                                @MokJ I made a mistake, if(index.data().toBool()) should have been if(index.data(Qt::UserRole).toBool()) fixed above now

                                P.S.
                                if(index.data(Qt::UserRole).isValid()) does the same and it's probably more efficient

                                M Offline
                                M Offline
                                MokJ
                                wrote on last edited by
                                #15

                                @VRonin
                                I got it,
                                in If , I'm drawing the new Image.
                                Thanks very much @VRonin.
                                Now If we want to keep that image in model should we implement

                                void setModelData(QWidget*editor,QAbstractItemModel *model,const QModelIndex &index)const
                                

                                again thanks @VRonin .

                                1 Reply Last reply
                                0
                                • H hjohn

                                  @VRonin
                                  can we take the Image as string ?

                                  M Offline
                                  M Offline
                                  MokJ
                                  wrote on last edited by
                                  #16

                                  @hjohn
                                  I came across this , maybe it could be helpful.

                                  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