Mein Problem sieht folgendermaßen aus:
Ich lade von der Festplatte eine 2D-Bitmap aus float-Werten, diese wandle ich in ein 8-Bit-Indiziertes QImage um und verpasse ihm eine Farbpalette für die Anzeige. Da die Umrechnung einiges an Rechenaufwand erfordert und ich auch verschiedene Varianten dafür habe, möchte ich diesen Teil in C++ schreiben. Die Frage ist nun, wie zeige ich das QImage in QML an.
Ich hab dafür bislang folgende Dateien angelegt:
Code: Alles auswählen
#ifndef BITMAPVIEWER_HPP
#define BITMAPVIEWER_HPP
#include <QImage>
class BitmapViewer : public QObject{
Q_OBJECT
Q_PROPERTY(QImage image READ image NOTIFY imageChanged)
public:
explicit BitmapViewer(QObject *parent = 0);
QImage image()const;
signals:
void imageChanged(QImage const& image);
public slots:
void display(QImage const& image);
void load(QString const& filename);
private:
QImage image_;
};
#endif // BITMAPVIEWER_HPP
Code: Alles auswählen
#include "bitmapviewer.hpp"
#include "bin.hpp"
#include <algorithm>
BitmapViewer::BitmapViewer(QObject *parent) :
QObject(parent)
{}
QImage BitmapViewer::image()const{
return image_;
}
void BitmapViewer::display(QImage const& image){
image_ = image;
imageChanged(image_);
}
void BitmapViewer::load(QString const& filename){
bin::bitmap< float > bitmap;
bin::read(bitmap, filename.toStdString());
auto minmax = std::minmax_element(bitmap.cbegin(), bitmap.cend());
float min = *minmax.first;
float max = *minmax.second;
QImage image(bitmap.width(), bitmap.height(), QImage::Format_Indexed8);
std::transform(bitmap.begin(), bitmap.end(), image.bits(), [&min, &max](float value){
return static_cast< char >((value - min) * 256 / max);
});
display(image);
}
Code: Alles auswählen
import QtQuick 2.0
import BitmapViewer 1.0
Rectangle {
width: 600
height: 400
BitmapViewer {
id: bitmap
}
Image {
id: image
source: bitmap.image // <-- Error: Unable to assign QImage to QUrl
}
MouseArea {
anchors.top: parent.top
anchors.left: parent.left
anchors.right: parent.right
anchors.bottom: parent.bottom
onClicked: bitmap.load("/path/to/image.bin")
}
}