Tuesday, September 4, 2007

Now we can drag and drop images to the journal.


qOrganizer got another nice feature, now we can just open konqueror (explorer) or any other program that deals with local files and just drag an image to the QTextEdit, it will get displayed and stored.

Drag and drop looks pretty nice and here's the code to do it:

void CQTextEdit::insertFromMimeData( const QMimeData *source )
{

if (source->hasFormat("text/uri-list"))
{
QTextCursor cursor = this->textCursor();
QTextDocument *document = this->document();
QString origin = source->urls()[0].toString();
#ifdef Q_OS_LINUX //toLocalFile() doesn't work
origin.remove("file://");
#endif
#ifdef Q_OS_WIN32
origin.remove("file:///");
#endif
QImage *image = new QImage(origin);
if(!image->isNull())
{
document->addResource(QTextDocument::ImageResource,origin,image);
cursor.insertImage(origin);
}
delete image;
}
}

We have to subclass QTextEdit and reimplement insertFromMimeData()

No comments: