2012-06-13 32 views
6

我如何保存QML圖片到手機內存?節約QML圖像

如果保存圖像是適用的,我有這種情況下,我需要添加一些文字到圖像(我們可以想象它,因爲我們有一個透明的圖像[保存文本],並把它放在第二個圖像,所以最後我們有一個形象,我們可以把它保存到手機內存)

回答

7

不從Image直接。 QDeclarativeImagepixmapsetPixmappixmapChange方法,但由於某些原因沒有申報財產。所以你不能使用它。不幸的是,它不能從C++中使用 - 這是一個私人問題。

你可以做什麼是繪製的圖形項目到您的像素圖,並將其保存到文件中。

class Capturer : public QObject 
{ 
    Q_OBJECT 
public: 
    explicit Capturer(QObject *parent = 0); 
    Q_INVOKABLE void save(QDeclarativeItem *obj); 
}; 

void Capturer::save(QDeclarativeItem *item) 
{ 
    QPixmap pix(item->width(), item->height()); 
    QPainter painter(&pix); 
    QStyleOptionGraphicsItem option; 
    item->paint(&painter, &option, NULL); 
    pix.save("/path/to/output.png"); 
} 

註冊 「俘獲」 上下文變量:

int main() 
{ 
    // ... 
    Capturer capturer; 
    QmlApplicationViewer viewer; 
    viewer.rootContext()->setContextProperty("capturer", &capturer); 
    // ... 
} 

而在你的QML使用它:

Rectangle { 
    // ... 
    Image { 
     id: img 
     source: "/path/to/your/source" 
    } 
    MouseArea { 
     anchors.fill: parent 
     onClicked: { 
      capturer.save(img) 
     } 
    } 
} 
+0

感謝親愛的,它的工作原理:)。現在我搜索如何合併Qt中的2張圖片,你能幫助我嗎? – Bayan

+0

如何覆蓋/合併2'QQuickItem'並將其保存到圖像? –

3

有了Qt 5.4+,你可以直接從您與設爲Qml做到這一點: grabToImage