[QTextEdit] How get images from document resources?



  • Hi all.

    I write simply WYSIWYG text editor. And need save images from document to file. Images inserted to document from clipboard.

    In insert image code, i set unique name for image, in format "imageXXXXX". The XXXXX - is random digit (we shall not consider conflicts for random).

    @
    void EditorTextArea::insertFromMimeData(const QMimeData *source)
    {
    QTextCursor cursor = this->textCursor();
    QTextDocument *document = this->document();

    // Insert image
    if(source->hasImage())
    {
        // Convert mime data to QImage
        QImage image=qvariant_cast<QImage>(source->imageData());
        
        // Generate resource name
        QString image_name="image"+QString::number(qrand());
        
        // Add image to document resource
        document->addResource(QTextDocument::ImageResource, QUrl(image_name), image);
        
        // Insert image to text
        cursor.insertImage(image_name);
        
        return;
    }
    

    }
    @

    I want to get images from document for save this to separate single files. Exaple, if in document inserted two images with name image12345 and image54389, saved this image to file image12345.png and image54389.png. But I can not do this.

    I try next code:
    (textarea - it is object of class QTextEdit)

    @
    void Editor::save_textarea_images(QString dirname)
    {
    qDebug() << "Save images, blocks count " << textarea->document()->blockCount() << "n";

    // Foreach all block in document
    QTextBlock bl = textarea->document()->begin();
    while(bl.isValid())
    {
        QTextBlock::iterator it;
        
        for(it = bl.begin(); !(it.atEnd()); ++it)
        {
            QTextFragment currentFragment = it.fragment();
            if(currentFragment.isValid())
            {
                if(currentFragment.charFormat().isImageFormat())
                {
                    // Block with image found
                    
                    // Get image format
                    QTextImageFormat imgFmt = currentFragment.charFormat().toImageFormat();
                    
                    // Get image name from format
                    QString image_name=imgFmt.name();
                    qDebug() << "Image " << image_name << "n"; // имя файла
                    
                    QString image_file_name=dirname+"/"+image_name;
                    qDebug() << "Save image data to file " << image_file_name;
                    
                    // Get image from document resource
                    QVariant image_data=textarea->document()->resource(QTextDocument::ImageResource, image_name);
                    qDebug() << "Data length " << image_data.toByteArray().length();
                    qDebug() << "Image data as string" << image_data.toString();
                    
                    // Write picture to file
                    QFile imgfile&#40;image_file_name&#41;;
                    if (!imgfile.open(QIODevice::WriteOnly(&#41;)) {
                        qDebug() << "Cant open file " << image_file_name << " for write.";
                        exit(1);
                    }
                    QTextStream out(&imgfile);
                    out << image_data.toByteArray();
                }
            }
        }
        bl = bl.next();
    }
    

    }
    @

    But in console i see log:

    @
    Save images, blocks count 27

    Image "image476707713"
    Save image data to file "./base/0000000842/image476707713"
    Data length 0
    Image data as string ""

    Image "image1186278907"
    Save image data to file "./base/0000000842/image1186278907"
    Data length 0
    Image data as string ""
    @

    And image files is zero size.

    QUEST: How get image(s) data from document resource and save this data to files?

    -----8<-----

    UPD: Unscramble it.

    The QVariant can't convert to QByteArray, if QImage data contained in value. And return empty array.

    For convert from QVariant to QImage must have use recasting type with construction:

    @
    qvariant_variable.value<QImage>
    @

    Next, from QImage possible saved image with method save().

    [edit: enclosed code in @ tag / chetankjain]



  • Please write, that you solved this problem "here":http://www.linux.org.ru/forum/development/5325251?lastmod=1284317250215

    Updated: Oh, sorry, not saw that.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.