prashant
7th September 2009, 11:05
I am creating a custom color picker by sub classing QFrame. There are three main functionality I would be needing.
1. A color picker
2. dragdrop color to other picker
3. right click menu
There won't be any problem in 1 and 3. How ever for 2 there are:
When you drop color to target, a menu appears with two options: swap & replace
If user select "swap", it should swap the colors of source and target
If user select "replace", it should replace the color of target with source.
drag & drop is having some problem:
void DragWidget::dragEnterEvent(QDragEnterEvent *event)
{
if (event->mimeData()->hasColor()) {
if (event->source() == this) {
event->setDropAction(Qt::MoveAction);
event->accept();
}
else {
event->acceptProposedAction();
}
}
else {
event->ignore();
}
}
void DragWidget::dragMoveEvent(QDragMoveEvent *event)
{
if (event->mimeData()->hasColor()) {
if (event->source() == this) {
event->setDropAction(Qt::MoveAction);
event->accept();
}
else {
event->acceptProposedAction();
}
}
else {
event->ignore();
}
}
void DragWidget::dropEvent(QDropEvent *event)
{
if (event->mimeData()->hasColor()) {
myColor = qvariant_cast<QColor>(event->mimeData()->colorData());
if (event->source() == this) {
event->setDropAction(Qt::MoveAction);
event->accept();
} else {
event->acceptProposedAction();
}
} else {
event->ignore();
}
}
void DragWidget::mousePressEvent(QMouseEvent *event)
{
QMimeData *mimeData = new QMimeData;
mimeData->setColorData(QVariant(myColor));
QDrag *drag = new QDrag(this);
drag->setMimeData(mimeData);
}
1. A color picker
2. dragdrop color to other picker
3. right click menu
There won't be any problem in 1 and 3. How ever for 2 there are:
When you drop color to target, a menu appears with two options: swap & replace
If user select "swap", it should swap the colors of source and target
If user select "replace", it should replace the color of target with source.
drag & drop is having some problem:
void DragWidget::dragEnterEvent(QDragEnterEvent *event)
{
if (event->mimeData()->hasColor()) {
if (event->source() == this) {
event->setDropAction(Qt::MoveAction);
event->accept();
}
else {
event->acceptProposedAction();
}
}
else {
event->ignore();
}
}
void DragWidget::dragMoveEvent(QDragMoveEvent *event)
{
if (event->mimeData()->hasColor()) {
if (event->source() == this) {
event->setDropAction(Qt::MoveAction);
event->accept();
}
else {
event->acceptProposedAction();
}
}
else {
event->ignore();
}
}
void DragWidget::dropEvent(QDropEvent *event)
{
if (event->mimeData()->hasColor()) {
myColor = qvariant_cast<QColor>(event->mimeData()->colorData());
if (event->source() == this) {
event->setDropAction(Qt::MoveAction);
event->accept();
} else {
event->acceptProposedAction();
}
} else {
event->ignore();
}
}
void DragWidget::mousePressEvent(QMouseEvent *event)
{
QMimeData *mimeData = new QMimeData;
mimeData->setColorData(QVariant(myColor));
QDrag *drag = new QDrag(this);
drag->setMimeData(mimeData);
}