Skip to content

Commit cb8c8de

Browse files
devmilpauldotknopf
authored andcommitted
adds "paintToImage" to QmlNetPaintedItem
1 parent 13a41c0 commit cb8c8de

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

QmlNet/controls/QmlNetPaintedItem.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "NetQPainter.h"
22
#include "QmlNetPaintedItem.h"
3+
#include <QBuffer>
34
#include <QPainter>
45
#include <stdexcept>
56

@@ -87,6 +88,18 @@ void QmlNetPaintedItemBase::paint(QPainter *painter) {
8788
}
8889
}
8990

91+
QImage QmlNetPaintedItemBase::paintToImage() {
92+
QImage result(width(), height(), QImage::Format_ARGB32);
93+
QPainter painter;
94+
95+
painter.begin(&result);
96+
painter.fillRect(0, 0, width(), height(), this->fillColor());
97+
paint(&painter);
98+
painter.end();
99+
100+
return result;
101+
}
102+
90103
void QmlNetPaintedItemBase::onHeightChanged() {
91104
if(sharedCallbacks.heightChanged != nullptr) {
92105
sharedCallbacks.heightChanged(m_netReference->getObjectId(), height());
@@ -117,4 +130,17 @@ Q_DECL_EXPORT int qqmlnetpainteditembase_getWidth(QmlNetPaintedItemBase* painted
117130
return paintedItem->width();
118131
}
119132

133+
Q_DECL_EXPORT uchar* qqmlnetpainteditembase_paintToImage(QmlNetPaintedItemBase* paintedItem, int64_t* sizeOut, QChar* format) {
134+
auto img = paintedItem->paintToImage();
135+
QByteArray arr;
136+
QBuffer buffer(&arr);
137+
buffer.open(QIODevice::WriteOnly);
138+
img.save(&buffer, QString(format).toStdString().c_str());
139+
140+
uchar* result = new uchar[buffer.data().size()];
141+
memcpy(result, buffer.data().data(), buffer.data().size());
142+
*sizeOut = buffer.data().size();
143+
return result;
144+
}
145+
120146
}

QmlNet/controls/QmlNetPaintedItem.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ class QmlNetPaintedItemBase : public QQuickPaintedItem {
3939

4040
void paint(QPainter *painter) override;
4141

42+
/**
43+
* @brief paintToImage mainly intended for testing the paint operations.
44+
* Calls paint() and returns the resulting image
45+
* @return a new image containing what paint() produced
46+
*/
47+
QImage paintToImage();
48+
4249
Q_SLOT void onHeightChanged();
4350
Q_SLOT void onWidthChanged();
4451

0 commit comments

Comments
 (0)