有多种方法:
用图标的方式:
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))
1. int QMap::size () const 可得 QMap的数据数目2. 根据它可以建table: new QTableWidget(size, 1, this)
3. 遍历QMap:
QMap<QString, int>::const_iterator i = map.constBegin()
while (i != map.constEnd()) {
cout <<i.key() <<": " <<i.value() <<endl
++i
}
4. 把每一个iterator所指的那条数据变成一个QTableWidgetItem
QTableWidgetItem ( const QString &text, int type = Type )
5. 把QTableWidgetItem插入到对应的table行中
QTableWidget::setItem ( int row, int column, QTableWidgetItem * item )
tableWidget_itemClicked(QTableWidgetItem *item)这里的item就是单击的项
还有获取当前项: ui->tableWidget->currentItem()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)