setTextAlliange()什么的好像不行,只能设置文字,我的item中只有icon,没有文字。
------解决方案--------------------
class IconDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
IconDelegate(QObject *parent = 0): QStyledItemDelegate(parent) { }
void paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex & index ) const
{
//判断是不是icon所在的index
if(index.column()!=0 && index.row()!=0){
QStyledItemDelegate::paint(painter,option,index);
return;
}
QPixmap pixmap = QPixmap("icon.png");
qApp->style()->drawItemPixmap(painter, option.rect, Qt::AlignCenter, QPixmap(pixmap));
}
};
然后
tableWidget->setItemDelegate(new IconDelegate(this));
tableWidget->setItemDelegateForColumn(colum, new IconDelegate(this));
tableWidget->setItemDelegateForRow(row, new IconDelegate(this));
------解决方案--------------------
item大小设定固定不变你认为合适吗?你的图片难道也都是一样大小的。。。我感觉图片保持原始大小即可,即使改变大小也是会变形的!