1、设置setStyleSheet的样式
Btn->setStyleSheet("QToolButton{border:Opx}")
2、设置按钮的尺寸自适应图片大小
Btn->setIconSize(QSize(Btn->width(),Btn->height()))
3、新建QPixmap类,并加载按钮的图片背景图片
QPixmap pixmap(path_image)
Btn->setIcon(QIcon(pixmap))
程序:
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include "QImage"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this)
QImage image
image.load(":/image/6631506470840007614.jpg")
QPalette palette
palette.setBrush(this->backgroundRole(),QBrush(image))
this->setPalette(palette)
setBtnIcon(ui->open, ":/image/open.png")
setBtnIcon(ui->large, ":/image/big.png")
setBtnIcon(ui->litte, ":/image/litte.png")
setBtnIcon(ui->next, ":/image/next.png")
setBtnIcon(ui->previous, ":/image/previous.png")
setBtnIcon(ui->normal, ":/image/normal.png")
setBtnIcon(ui->recovery, ":/image/recover.png")
setBtnIcon(ui->start, ":/image/start.png")
setBtnIcon(ui->stop, ":/image/stop.png")
setBtnIcon(ui->rotate, ":/image/zhuan.png")
setBtnIcon(ui->open, ":/image/open.png")
}
Widget::~Widget()
{
delete ui
}
void Widget::setBtnIcon(QToolButton *Btn, const QString &path_image)
{
Btn->setStyleSheet("QToolButton{border:Opx}")
Btn->setIconSize(QSize(Btn->width(),Btn->height()))
QPixmap pixmap(path_image)
Btn->setIcon(QIcon(pixmap))
}
2、widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include "QToolButton"
#include "QLabel"
#include "QTimer"
#include "QString"
#include "QPixmap"
#include "QPalette"
#include "QMatrix"
namespace Ui {
class Widget
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0)
~Widget()
private:
void setBtnIcon(QToolButton *Btn, const QString &path_image)
private:
Ui::Widget *ui
}
#endif // WIDGET_H
先创建一个QWidget,然后对这个QWidget设置布局,然后往布局里添加你的按钮,最后把这个QWidget设置到item上嗯哈。
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()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)