qt tablewidget设置图片

qt tablewidget设置图片,第1张

有多种方法:

用图标的方式:

    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 QLabel

   label->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

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11419999.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-16
下一篇 2023-05-16

发表评论

登录后才能评论

评论列表(0条)

保存