当前位置: 代码迷 >> QT开发 >> qml 保存一张图片的有关问题
  详细解决方案

qml 保存一张图片的有关问题

热度:406   发布时间:2016-04-25 03:13:03.0
qml 保存一张图片的问题
void MainWindow::save(QObject *imageObj, const QString &path)
{
    QGraphicsObject *item = qobject_cast<QGraphicsObject*>(imageObj);
    if (!item) {
        qDebug() << "Item is NULL";
        return;
    }
    QImage img(item->boundingRect().size().toSize(), QImage::Format_RGB32);
    img.fill(QColor(255, 255, 255).rgb());
    QPainter painter(&img);
    QStyleOptionGraphicsItem styleOption;
    item->paint(&painter, &styleOption);
    img.save(path);
}

Rectangle {
    width: 640
    height: 480
    color: "white"
 
    Image {
        id: imgItem
        fillMode: Image.PreserveAspectFit
        source: "file:///D:/13.jpg"
    }
 
    MouseArea {
        anchors.fill: parent
        onClicked: {
            myObject.save(imgItem, "file:///E:/13.jpg");
        }
    }
}

通过上面的代码保存一张图片时,代码执行到
QGraphicsObject *item = qobject_cast<QGraphicsObject*>(imageObj);
时得到的 item 为空,imageObj不为空
请问是什么原因
------解决方案--------------------
Image对应C++类为QQuickImage,QQuickImage是QQuickImageBase的派生类,QQuickImageBase有个方法image(),可以返回QImage对象。
不过这些C++类SDK里看不到,只能看源码。
你可以从这里研究下,应该能找到方法。
  相关解决方案