嗯哈。
QWidget *container = new QWidget
container->addLayOut(xxxx)
container->addWidget(xxxWidget)
item->setItemWidget(containter)
//这个container完全可以用Desinger去做哈。高效快捷~
QTableWidget * tvDecoderItemArea = new QTableWidget(this)
tvDecoderItemArea->setObjectName("tvDecoderItemArea")
tvDecoderItemArea->setColumnCount(1)
tvDecoderItemArea->horizontalHeader()->setVisible(false)
tvDecoderItemArea->verticalHeader()->setVisible(false)
tvDecoderItemArea->horizontalHeader()->setStretchLastSection(true)
int index = tvDecoderItemArea->rowCount()
tvDecoderItemArea->setRowCount(index+1)
QPushButton* pItem = new QPushButton(tr("text_%1").arg(index+1))
tvDecoderItemArea->setCellWidget(index,0,pItem)
QListWidget::setItemWidget
QWidget是一个包裹,它占据Item的所有空间
可能是你的布局没有调整边距所致,在你这两个布局上设置setContentsMargins(0, 0, 0, 0)
似乎是使用QStyledItemDelegate这个来实现?求教如何使用?有没有例子?
使用QListWidget::setItemWidget时,如何对item进行布局?现在控件加进去了,但是都重叠在一起了。
这个QWidget在Item里面的位置如何调整?
,,这样那我把所有的东西,包括item的text、ICON都放入这个QWidget了?
listWidget->setResizeMode(QListView::Adjust)
listWidget->setGridSize(QSize(165,176))
listWidget->setIconSize(QSize(158,117))
listWidget->setViewMode(QListView::IconMode)
listWidget->setWordWrap(true)
listWidget->setAutoScroll(true)
mItem = new QListWidgetItem(listWidget)
mLayoutWidget = new QWidget(listWidget)
mVLayout = new QVBoxLayout(mLayoutWidget)
mHLayout = new QHBoxLayout()
QLabel *showPic = new QLabel(mLayoutWidget)
QPixmap pixMap(QString(":/b_1243700182001.jpg"))
showPic->setPixmap(pixMap)
mVLayout->addWidget(showPic)
QLabel *showName = new QLabel(text,mLayoutWidget)
mVLayout->addWidget(showName)
mDeleteBt = new QPushButton(mLayoutWidget)
mDetailBt = new QPushButton(mLayoutWidget)
mUploadBt = new QPushButton(mLayoutWidget)
mHLayout->addWidget(mDeleteBt)
mHLayout->addWidget(mDetailBt)
mHLayout->addWidget(mUploadBt)
mVLayout->addLayout(mHLayout)
mLayoutWidget->setLayout(mVLayout)
mLayoutWidget->show()
有多种方法:
用图标的方式:
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))
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)