#include <QtGui>
#include "test.h"
{
pb->installEventFilter(this);
cb->installEventFilter(this);
chb->installEventFilter(this);
rb->installEventFilter(this);
sb->installEventFilter(this);
dsp->installEventFilter(this);
grid->addWidget(pb, 0, 0);
grid->addWidget(cb, 0, 1);
grid->addWidget(chb, 0, 2);
grid->addWidget(rb, 1, 0);
grid->addWidget(sb, 1, 1);
grid->addWidget(dsp, 1, 2);
setCentralWidget(widget);
setAcceptDrops(true);
}
{
processDrag(this, event);
}
{
painter.setPen(Qt::red);
painter.drawText(0, pixmap.rect().center().y(), "hello Qt");
dataStream << pixmap <<
QPoint(event
->pos
());
mimeData->setData("application/x-dnditemdata", itemData);
drag->setMimeData(mimeData);
drag->setPixmap(pixmap);
drag->setHotSpot(event->pos());
drag->exec();
}
{
if (e
->type
() == QEvent::MouseButtonPress) { QWidget *widget
= qobject_cast<QWidget
*>
(o
);
if (widget && me->button() == Qt::LeftButton) {
processDrag(widget, me);
return true;
}
}
}
#include <QtGui>
#include "test.h"
Test::Test(QWidget *parent)
: QMainWindow(parent)
{
QWidget *widget = new QWidget;
QGridLayout *grid = new QGridLayout(widget);
QPushButton *pb = new QPushButton(tr("Button"));
pb->installEventFilter(this);
QComboBox *cb = new QComboBox;
cb->installEventFilter(this);
QCheckBox *chb = new QCheckBox(tr("Check box"));
chb->installEventFilter(this);
QRadioButton *rb = new QRadioButton(tr("Radio button"));
rb->installEventFilter(this);
QSpinBox *sb = new QSpinBox;
sb->installEventFilter(this);
QDoubleSpinBox *dsp = new QDoubleSpinBox;
dsp->installEventFilter(this);
grid->addWidget(pb, 0, 0);
grid->addWidget(cb, 0, 1);
grid->addWidget(chb, 0, 2);
grid->addWidget(rb, 1, 0);
grid->addWidget(sb, 1, 1);
grid->addWidget(dsp, 1, 2);
setCentralWidget(widget);
setAcceptDrops(true);
}
void Test::mousePressEvent(QMouseEvent *event)
{
processDrag(this, event);
}
void Test::processDrag(QWidget *widget, QMouseEvent *event)
{
QPixmap pixmap = QPixmap::grabWidget(widget);
QPainter painter(&pixmap);
painter.setPen(Qt::red);
painter.drawText(0, pixmap.rect().center().y(), "hello Qt");
QByteArray itemData;
QDataStream dataStream(&itemData, QIODevice::WriteOnly);
dataStream << pixmap << QPoint(event->pos());
QMimeData *mimeData = new QMimeData;
mimeData->setData("application/x-dnditemdata", itemData);
QDrag *drag = new QDrag(this);
drag->setMimeData(mimeData);
drag->setPixmap(pixmap);
drag->setHotSpot(event->pos());
drag->exec();
}
bool Test::eventFilter(QObject *o, QEvent *e)
{
if (e->type() == QEvent::MouseButtonPress) {
QMouseEvent *me = static_cast<QMouseEvent *>(e);
QWidget *widget = qobject_cast<QWidget *>(o);
if (widget && me->button() == Qt::LeftButton) {
processDrag(widget, me);
return true;
}
}
return QMainWindow::eventFilter(o, e);
}
To copy to clipboard, switch view to plain text mode
Bookmarks