How to set icon center in QTableView?

后端 未结 2 1903
轻奢々
轻奢々 2021-01-03 10:22
NetworkPageForm::NetworkPageForm(QWidget *parent) :
      QWidget(parent),
      ui(new Ui::NetworkPageForm),
      devicesModel(NULL)
{
      ui->setupUi(this);
         


        
2条回答
  •  暖寄归人
    2021-01-03 10:40

    It's no need to do it in delegate. The delegate will make your style sheet unavailable. You can paint the icon in the middle of a rectangle pixmap which is as same size as the cell, and return it in the QAbstractItemModel::data() function with Qt::DecorationRole. Like this:

    Qt::DecoratoinRole:
        QPixmap pixmap(w, h);  //w=cell width, h=cell
        pixmap.fill(Qt::transparent); // draw a transparent rectangle
        QPixmap iconPixmap(":/xx.png");
        QPainter painter(&pixmap);
        //Calculate the center coordinate x,y for iconPixmap
        painter.draw(x, y, iconWidth, iconHeight, iconPixmap);
        return pixmap;
    

提交回复
热议问题