Adding button to QTableview

前端 未结 6 1677
遥遥无期
遥遥无期 2020-12-14 10:57

I have created one table by using QTableview and QAbstractTableModel . In one of the cells, I want to add one help button in the r

6条回答
  •  一个人的身影
    2020-12-14 11:46

    I got the solution .. Old paint method :

    void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
     {
         QStyleOptionButton button;
         QRect r = option.rect;//getting the rect of the cell
         int x,y,w,h;
         x = r.left() + r.width() - 30;//the X coordinate
         y = r.top();//the Y coordinate
         w = 30;//button width
         h = 30;//button height
         button.rect = QRect(x,y,w,h);
         button.text = "=^.^=";
         button.state = QStyle::State_Enabled;
    
         QApplication::style()->drawControl( QStyle::CE_PushButton, &button, painter);
     }
    

    here is the updated paint() method .

    void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
     {
             QItemDelegate::paint(painter, option, index);
      if(index.row()==8)//since i have to make it display only at (8,0) position .
     {
      if(index.column()==0)
      {
         QStyleOptionButton button;
         QRect r = option.rect;//getting the rect of the cell
         int x,y,w,h;
         x = r.left() + r.width() - 20;//the X coordinate
         y = r.top();//the Y coordinate
         w = 15;//button width(based on the requirement)
         h = 15;//button height(based on the requirement)
      button.icon= QIcon(QString::fromUtf8("Resources/HelpIcon.png"));
      button.iconSize = QSize(20,20);
         button.rect = QRect(x,y,w,h);
         button.text = "";//no text . since if text will be given then it will push the icon to left side based on the coordinates .
         button.state = QStyle::State_Enabled;
    
         //QApplication::style()->drawControl( QStyle::CE_PushButton, &button, painter);
    
      QApplication::style()->drawControl( QStyle::CE_PushButtonLabel, &button, painter);//To make the Button transparent .
    
      }
      }
    }
    

提交回复
热议问题