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:rop)
qDebug() << "type --"<< e->type () << endl;
}
/////////////////////////////////////////////////////////////7
DragWidget:ragWidget(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
Bookmarks