代码拉取完成,页面将自动刷新
#include "UICanvasImageItem.h"
// #include "NDNodeBase.h"
#include <QPainter>
#include <QFileDialog>
#include <functional>
#include <QApplication>
UICanvasImageItem::UICanvasImageItem(QGraphicsItem* parentItem)
:UICanvasItemBase(parentItem)
{
initAttribute();
QString curAppPath = qApp->applicationDirPath() + "/";
setImage(curAppPath + "./t3.jpg");
}
UICanvasImageItem::~UICanvasImageItem()
{
}
void UICanvasImageItem::customPaint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{
painter->save();
QRectF customRect = this->getCustomRect();
QRect pixRect(customRect.x(), customRect.y(), customRect.width(), customRect.height());
painter->drawPixmap(pixRect, m_pixmap);
painter->restore();
}
void UICanvasImageItem::setImage(const QString& filePath)
{
QImage image(filePath);
m_pixmap = QPixmap::fromImage(image);
if (!m_pixmap.isNull())
{
// 设置 size
m_size.setWidth(m_pixmap.width());
m_size.setHeight(m_pixmap.height());
// 设置改变大小属性
this->setItemResizeable(true);
this->setItemResizeRatio(true, m_size.width() * 1.0 / m_size.height());
// 设置属性
// QObject::disconnect(m_pFilePathAttribute, &NDStringAttribute::valueChanged, this, &UICanvasImageItem::onAttributeValueChanged);
// m_pFilePathAttribute->setValue(filePath);
// QObject::connect(m_pFilePathAttribute, &NDStringAttribute::valueChanged, this, &UICanvasImageItem::onAttributeValueChanged);
}
this->update();
}
void UICanvasImageItem::initAttribute(void)
{
// 添加属性组
QString attributeGroupName = tr("Image Attribute");
// m_pNode->addAttributeGroup(attributeGroupName);
// 创建属性
// m_pFilePathAttribute = new NDStringAttribute;
// m_pFilePathAttribute->setDisplayName(tr("Image Path: "));
// m_pFilePathAttribute->setName("imagePath");
// m_pFilePathAttribute->setShowButton(true);
// m_pFilePathAttribute->setButtonString(tr("Browse"));
// m_pFilePathAttribute->setButtonFunction(std::bind(&UICanvasImageItem::showFileBrowseDialo, \
// this, std::placeholders::_1));
// m_pNode->addAttribute(attributeGroupName, m_pFilePathAttribute);
// // 连接信号和槽函数
// QObject::connect(m_pFilePathAttribute, &NDStringAttribute::valueChanged, \
// this, &UICanvasImageItem::onAttributeValueChanged);
}
bool UICanvasImageItem::showFileBrowseDialo(QString& str)
{
QString curAppPath = qApp->applicationDirPath() + "/";
QString fileName = QFileDialog::getOpenFileName(nullptr, tr("Open Image File"), curAppPath + "./", \
tr("Image Files (*.bmp *.png *.jpg)"));
if (fileName.isEmpty())
return false;
str = fileName;
return true;
}
void UICanvasImageItem::onAttributeValueChanged(const QVariant& value)
{
this->setImage(value.toString());
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。