PDA

View Full Version : QTextBrowser



sreedhar
14th March 2006, 08:11
Hi,
I am using QT4.1.1. I cannot get the drop event in QTextBrowser.
Here is my code
#include <QtGui/QLabel>
#include <QtGui/QPixmap>
#include <QtGui/QDragEnterEvent>
#include <QtGui/QDropEvent>
#include <QtGui/QMouseEvent>
#include <QtCore/QMimeData>
#include <QtGui/QDrag>
#include <QtGui/QPainter>
#include <QtCore/QDebug>
#include <QtGui/QTextBrowser>

class QColorGroup;

#include "dragwidget.h"


ListView::ListView( QWidget* parent)
:QTextBrowser (parent)
{
setFocusPolicy (Qt::StrongFocus);
setAcceptDrops(true);
}

void ListView::mouseMoveEvent ( QMouseEvent * me)
{
QTextBrowser::mouseMoveEvent(me);

if (!(me->buttons() & Qt::LeftButton))
return;

QByteArray itemData;
QDataStream dataStream(&itemData, QIODevice::WriteOnly);
dataStream << QString("abcd");

QMimeData *mimeData = new QMimeData;
mimeData->setData("application/x-dnditemdata", itemData);

QDrag *drag = new QDrag(this);
drag->setMimeData(mimeData);
drag->start();

return;
}


void ListView::dragEnterEvent(QDragEnterEvent *event)
{
qDebug() << "In ListView::dragEnterEvent";
if (event->mimeData()->hasFormat("application/x-dnditemdata")) {
if (event->source() == this) {
// event->setDropAction(Qt::MoveAction);
event->accept();
} else {
event->acceptProposedAction();
}
} else {
event->ignore();
}
}

void ListView::dropEvent(QDropEvent *event)
{
qDebug() << "DragWidget::dropEvent" ;
if (event->mimeData()->hasFormat("application/x-dnditemdata")) {
QByteArray itemData = event->mimeData()->data("application/x-dnditemdata");
QDataStream dataStream(&itemData, QIODevice::ReadOnly);

QString str;
dataStream >> str;

if (event->source() == this) {
event->accept();
} else {
qDebug() << "--- dropped item :: "<< str << endl;
event->acceptProposedAction();
}
} else {
event->ignore();
}
}


bool ListView::viewportEvent ( QEvent * e )
{
QTextBrowser::viewportEvent(e);
// if (e->type() == QEvent::Drop)
qDebug() << "type --"<< e->type () << endl;
}


/////////////////////////////////////////////////////////////7

DragWidget::DragWidget(QWidget *parent)
: QFrame(parent)
{
setMinimumSize(200, 200);
setFrameStyle(QFrame::Sunken | QFrame::StyledPanel);

mList = new ListView(this);
QString str = "<html><body>";
str += "<font color=\"gray\"> 1. this is <a href=\"xxx.html\"><font color=\"gray\">abc</font></a> a gray link </font><br>";
str += "<font color=\"gray\"> 2. this is <a href=\"xxx.html\" style= \"color:gray\">abc</a> a gray link </font><br>";
str += "<font color=\"gray\"> 3. this is <a href=\"xxx.html\" style= \" color:gray\">abc</a> a gray link</font> <br>";
str += "<font color=\"gray\"> 4. this is <a href=\"xxx.html\"><span style=\" text-decoration: underline; color:#808080;\">abc</span></a> a gray link </font><br>";
str += "<font color=\"gray\"> 1. this is <a href=\"xxx.html\">abc</a> a gray link </font><br>";
str += "</body></html>";

mList->setHtml(str);

}

Please do reply me.

Thank you

bye,
sree

zlatko
14th March 2006, 09:29
Have you declare your dropEvent as protected?

sreedhar
14th March 2006, 11:09
Thanks for replying me

I declared my dropEvent as protected

jpn
14th March 2006, 11:54
Try overriding the dragMoveEvent() also.
I got it working that way..



void ListView::dragMoveEvent(QDragMoveEvent *event)
{
if (event->mimeData()->hasFormat("application/x-dnditemdata"))
event->acceptProposedAction();
}

sreedhar
14th March 2006, 13:27
Thanks a lot,
it's working if i implement dragMoveEvent.

I think this is a bug in QT4.1.1 as i implemented drag and drop in other widgets (eg. QListView, QWidget, etc) all of them work without implementing dragMoveEvent.

Is it necessary that we implement dragEvent, dragMoveEvent, dragEnterEvent, and dropEvent for all the widgets for drag and drop

regards,
sree