MyTetra Share
Делитесь знаниями!
Вставка картинок в QTextEdit
Время создания: 20.03.2016 22:41
Текстовые метки: Qt
Раздел: Компьютер - Программирование - Язык C++ (Си++) - Библиотека Qt - Принципы написания кода
Запись: xintrea/mytetra_syncro/master/base/0000001173/text.html на raw.github.com

QTextEdit also supports custom drag and drop behavior. By default, QTextEdit will insert plain text, HTML and rich text when the user drops data of these MIME types onto a document. Reimplement canInsertFromMimeData() and insertFromMimeData() to add support for additional MIME types.

For example, to allow the user to drag and drop an image onto a QTextEdit, you could the implement these functions in the following way:


bool TextEdit::canInsertFromMimeData( const QMimeData *source ) const

{

if (source->hasImage())

return true;

else

return QTextEdit::canInsertFromMimeData(source);

}



We add support for image MIME types by returning true. For all other MIME types, we use the default implementation.



void TextEdit::insertFromMimeData( const QMimeData *source )

{

if (source->hasImage())

{

QImage image = qvariant_cast<QImage>(source->imageData());

QTextCursor cursor = this->textCursor();

QTextDocument *document = this->document();

document->addResource(QTextDocument::ImageResource, QUrl("image"), image);

cursor.insertImage("image");

}

}



We unpack the image from the QVariant held by the MIME source and insert it into the document as a resource.


Так же в этом разделе:
 
MyTetra Share v.0.59
Яндекс индекс цитирования