It seems there is something wrong here and it's creating another problem.
{
// Pack mime data
mimeData
->setColorData
(QVariant(myColor
));
// Prepare cursor image
painter.begin(&pixmap);
painter.fillRect(pixmap.rect(), myColor);
painter.end();
// We establish QDrag object, draw cursor and embed data
drag->setMimeData(mimeData);
drag->setPixmap(pixmap);
drag->setHotSpot(event->pos());
sourcePicker = drag->source();
// We send it
drag->exec(Qt::CopyAction);
}
{
if (event
->type
() == QEvent::MouseButtonRelease){ if (color.isValid()) {
setAutoFillBackground(true);
myColor = color;
}
}
else
event->ignore();
}
void DragWidget::mousePressEvent(QMouseEvent *event)
{
// Pack mime data
QMimeData *mimeData = new QMimeData;
mimeData->setColorData(QVariant(myColor));
// Prepare cursor image
QPixmap pixmap(this->size());
QPainter painter;
painter.begin(&pixmap);
painter.fillRect(pixmap.rect(), myColor);
painter.end();
// We establish QDrag object, draw cursor and embed data
QDrag *drag = new QDrag(this);
drag->setMimeData(mimeData);
drag->setPixmap(pixmap);
drag->setHotSpot(event->pos());
sourcePicker = drag->source();
// We send it
drag->exec(Qt::CopyAction);
}
void DragWidget::mouseReleaseEvent(QMouseEvent *event)
{
if (event->type() == QEvent::MouseButtonRelease){
QColor color = QColorDialog::getColor(QColor(myColor), this);
if (color.isValid()) {
setPalette(QPalette(color));
setAutoFillBackground(true);
myColor = color;
}
}
else
event->ignore();
}
To copy to clipboard, switch view to plain text mode
With this line in "mousePressEvent":
drag->exec(Qt::CopyAction);
drag->exec(Qt::CopyAction);
To copy to clipboard, switch view to plain text mode
drag n drop is working fine but "mouseReleaseEvent" is not getting called. If I comment this
line "releaseEvent" is getting called.
Secondly, I have to add the functionality to make this widget as color picker also, I added color picking dialog in "releaseEvent". This won't work as expected because the "dropping"
event is actually a "releaseEvent" and as you drop on target picker dialog shows up.
A hack to this problem is to show color dialog when there is "down" and "up" on same widget. Using a bool var, you set state true, when there is a mouse down and in mouse up
event, check the state of this var, if it's true, show color dialog and set it again to false.
Am I right here or there is a better way of doing this?
How do I setup a menu, which appears at dropping? "swap color" or "replace color"
Prashant
Bookmarks