当前位置: 代码迷 >> QT开发 >> QTableWidget中如何把item中的icon居中或者充满整个item
  详细解决方案

QTableWidget中如何把item中的icon居中或者充满整个item

热度:38   发布时间:2016-04-25 03:44:22.0
QTableWidget中怎么把item中的icon居中或者充满整个item?
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));
------解决方案--------------------
引用:
Quote: 引用:

Quote: 引用:

使用代理 setCellWidget,代理是一个QLabel,QLabel里放你要设置的图标即可!这个做法比较简单

这样是可行的,不过我好像还搞麻烦了,我是用QWidget装了一个label,再用label装了一个Qpixmap。

还有一个小问题,我的qpixmap只能是固定大小,不能变大变小,因此要写出充满item的效果只有把item的大小设成和qpixmap大小一样,我是不是哪里搞错了?

item大小设定固定不变你认为合适吗?你的图片难道也都是一样大小的。。。我感觉图片保持原始大小即可,即使改变大小也是会变形的!
  相关解决方案