有多种方法:
用图标的方式:
QTableWidgetItem *it = ui->tableWidget->item(row, col)if (it==0)
{
it=new QTableWidgetItem()
setItem(row, col, it)
}
it->setIcon(QIcon(pixMap))
用setCellWidget:
QLabel* label=new QLabellabel->setPixmap(pixMap)
ui->tableWidget->setCellWidget(row, col, label)
用代理:
class QPixmapItemDele : public QStyledItemDelegate{
public:
QPixmapItemDele(QObject* parent = 0) : QStyledItemDelegate(parent) {}
virtual void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
if (index.data(Qt::DisplayRole).canConvert<QPixmap>())
{
QPixmap pm = index.data(Qt::DisplayRole).value<QPixmap>()
painter->drawPixmap(0,0,pm)
}
else
{
QStyledItemDelegate::paint(painter, option, index)
}
}
}
......
ui->tableWidget->setItemDelegate(new QPixmapItemDele)
ui->tableWidget->item(row,col)->setData(Qt::DisplayRole, QVariant::fromValue<QPixmap>(pixMap))
void ScrollArea::set_image(char *filename){
QImage *ppm = new QImage(filename)
label = new QLabel()
printf("ppm->width()=%d, ppm->height()=%d\n",ppm->width(), ppm->height())//获取图片的宽度和高度
label->setPixmap(QPixmap::fromImage(*ppm))
this->setWidget(label)
/*设置窗口最大高度和宽度为1024*768*/
this->setMaximumHeight(768)
this->setMaximumWidth(1024)
this->resize(QSize( ppm->width()+5, ppm->height() +5))
return
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)