Drag and Drop QFrame - qt

I've been looking at drag and drop examples and am still puzzled with them. In particular I don't know what to do in mousePressEvent( ) and dropEvent().
I'm following and modifying example which drags and drops QLabel.
Here is what I think mousePressEvent() should look like for QFrame. I tried it and I can see the frame move but I lose it when I drop it.
void SummaryBar::mousePressEvent(QMouseEvent *event)
{
QFrame *child = static_cast<QFrame*>(childAt(event->pos()));
if (!child)
return;
QPixmap pixmap = QPixmap::grabWidget(child);
QByteArray itemData;
QDataStream dataStream(&itemData, QIODevice::WriteOnly);
dataStream << pixmap << QPoint(event->pos() - child->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() - child->pos());
if (drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction) == Qt::MoveAction) {
child->close();
} else {
child->show();
}
}
Here is dropEvent(). It obviously does not work... not sure why.
void SummaryBar::dropEvent(QDropEvent *event)
{
if (event->mimeData()->hasFormat("application/x-dnditemdata")) {
QByteArray itemData = event->mimeData()->data("application/x-dnditemdata");
QDataStream dataStream(&itemData, QIODevice::ReadOnly);
QPixmap pixmap;
QPoint offset;
dataStream >> pixmap >> offset;
QFrame *newFrame = new QFrame(this);
//newFrame->setPixmap(pixmap);
newFrame->move(event->pos() - offset);
newFrame->show();
newFrame->setAttribute(Qt::WA_DeleteOnClose);
if (event->source() == this) {
event->setDropAction(Qt::MoveAction);
event->accept();
} else {
event->acceptProposedAction();
}
} else {
event->ignore();
}
}
void SummaryBar::dragEnterEvent(QDragEnterEvent *event)
{
if (event->mimeData()->hasFormat("application/x-dnditemdata")) {
if (event->source() == this) {
event->setDropAction(Qt::MoveAction);
event->accept();
} else {
event->acceptProposedAction();
}
} else {
event->ignore();
}
}
I would greatly appreciate any help!!

Related

Dropping images in Qt

I am looking to be able to take a QImage and drop it into an editing program such as Paint, for example. I've tried setting the mime data to the current image with the following code, but the data doesn't move correctly. The pixmap is displayed correctly, but the drop never happens. If anyone has any advice, that would be much appreciated! Thanks!
void LCDWidget::mousePressEvent(QMouseEvent *e) {
if (e->button() == Qt::LeftButton) {
QDrag *drag = new QDrag(this);
QMimeData *mimeData = new QMimeData;
QImage image = renderFramebuffer(lcdState);
QPixmap mymap = QPixmap::fromImage(image);
mimeData->setImageData(image);
drag->setMimeData(mimeData);
drag->setHotSpot(e->pos());
drag->setPixmap(mymap);
drag->exec(Qt::CopyAction | Qt::MoveAction);
e->accept();
} else {
e->ignore();
}
}
I eventually figured this out with the following code. The solution is to copy the image to a local file, and then use the path to the file as the copy data.
void LCDWidget::mousePressEvent(QMouseEvent *e) {
if (e->button() == Qt::LeftButton) {
QDrag *drag = new QDrag(this);
QMimeData *mimeData = new QMimeData;
QImage image = getImage();
QPixmap mymap = QPixmap::fromImage(image);
QString path = QDir::tempPath() + randomString(5) + QStringLiteral(".png");
image.save(path, "PNG", 0);
mimeData->setImageData(image);
mimeData->setUrls(QList<QUrl>() << QUrl::fromLocalFile(path));
drag->setMimeData(mimeData);
drag->setHotSpot(e->pos());
drag->setPixmap(mymap);
drag->exec(Qt::CopyAction | Qt::MoveAction);
e->accept();
} else {
e->ignore();
}
}

Widgets drawing in Qt

I have been looking all over the Internet and I don't find any solution to my problem so I have a bar like this:
(Scroll bar with pictures for handlers)
But when I scroll the pictures through the other what happen is:
Is there anyway to stop over painting the sad face over the happy one?
This is the paintEvent method
void QSlider::paintEvent(QPaintEvent* e)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
if (ori_==Qt::Horizontal)
{
if(index>=0)
{
this->setFixedSize(42,57);
printf("\n");
QString dir="C:\\Qt\\Qt5.2.1\\Tools\\QtCreator\\bin\\pluginss\\";
QString emo=QString::fromStdString(emotions.at(index));
dir.append(emo);
dir.append(".png");
QPixmap pixmap(dir);
painter.drawPixmap(QPoint(0,0), pixmap.scaled(40,40, Qt::KeepAspectRatio));
QRect rec(0,22,40,40);
painter.drawText(rec,Qt::AlignCenter,emo);
}
else
{
this->setFixedSize(10,20);
QRect rec(0,7,8,8);
painter.drawRect(rec);
QPolygon pp;
pp << QPoint(0,7) << QPoint(4,0) << QPoint(8,7);
painter.drawPolygon(pp, Qt::OddEvenFill);
}
}
}
This is my mouseMoveEvent:
void QSliderWidget::mouseMoveEvent(QMouseEvent* e)
{
if (activeSlider_>=0)
{
QRect crec = contentsRect();
qreal pos;
crec.adjust(rampeditor_->bspace_,0,-rampeditor_->bspace_,0);
pos = 1.0*(e->pos().x()-rampeditor_->bspace_)/(crec.width());
if (pos<0.0 || pos>1.0)
{
delete(rampeditor_->sliders_[activeSlider_]);
rampeditor_->sliders_.removeAt(activeSlider_);
activeSlider_ = -1;
rampeditor_->updateRamp();
}
else
{
rampeditor_->sliders_[activeSlider_]->move(0,e->pos().y());
seconds.replace(activeSlider_-1,e->pos().x());
qSort(rampeditor_->sliders_.begin(), rampeditor_->sliders_.end(), QColorRampEditor::SliderSort);
index=activeSlider_-1;
rampeditor_->mergeSort(0,seconds.size());
}
}
}

Qt: Reordering ListView Items using drag and drop

I have implemented the following methods for drag and drop
void mousePressEvent(QMouseEvent *event);
void dragEnterEvent(QDragEnterEvent *event);
void dragMoveEvent(QDragMoveEvent *event);
void dropEvent(QDropEvent *event);
void startDrag(Qt::DropActions supportedActions);
I have a QListView and have used setIndexWidget() to show a custom widget at each modelindex. I am trying to arrange the items in the listview using drag and drop, However the items in the listview are not arranged properly, I have written the following code:-
MyListView::MyListView(QWidget *parent) :
QListView(parent)
{
setSelectionMode(QAbstractItemView::SingleSelection);
setDragEnabled(true);
setAcceptDrops(true);
setDragDropMode(QAbstractItemView::InternalMove);
setDefaultDropAction(Qt::MoveAction);
setDropIndicatorShown(true);
setDragDropOverwriteMode(true);
setMovement(QListView::Snap);
setFlow(QListView::TopToBottom);
// setStyleSheet("QListView::item {"
// "height: 50px;"
// "border: 1px solid black;}");
}
void MyListView::setModel(QAbstractItemModel *model)
{
QListView::setModel(model);
for (int i=0; i<model->rowCount(); i++)
{
QModelIndex index = model->index(i,0);
ListWidgetItem *widget = new ListWidgetItem;
widget->setIndex(i);;
setIndexWidget(index,widget);
}
}
void MyListView::mousePressEvent(QMouseEvent *event)
{
QModelIndex i = indexAt(event->pos());
if (event->button() == Qt::LeftButton)
{
ListWidgetItem *item = dynamic_cast<ListWidgetItem *>(childAt(event->pos()));
if (!item)
return;
y = event->pos().y()-item->pos().y();
QModelIndex indx = indexAt(event->pos());
currentPixmap = QPixmap::grabWidget(item);
dragPoint = event->pos();
if (event->pos().x() < 25 && indx.row() >= 0)
startDrag(Qt::MoveAction);
}
}
void MyListView::dragEnterEvent(QDragEnterEvent *event)
{
if (event->mimeData()->hasFormat("application/x-QListView-DragAndDrop"))
{
event->accept();
}
else
event->ignore();
}
void MyListView::dragMoveEvent(QDragMoveEvent *event)
{
ListWidgetItem *item = dynamic_cast<ListWidgetItem *>(childAt(event->pos()));
if(!item)
return;
itemList.append(item);
foreach (ListWidgetItem *widget,itemList)
{
if (widget == item)
widget->setStyleSheet("#ListWidget { border-top: 2px solid red; }");
else
widget->setStyleSheet("#ListWidget { border-top: 0px solid black; }");
}
if (event->mimeData()->hasFormat("application/x-QListView-DragAndDrop"))
{
event->setDropAction(Qt::MoveAction);
event->accept();
} else
event->ignore();
}
void MyListView::dropEvent(QDropEvent *event)
{
if (event->mimeData()->hasFormat("application/x-QListView-DragAndDrop")) {
QByteArray itemData = event->mimeData()->data("application/x-QListView-DragAndDrop");
QDataStream dataStream(&itemData,QIODevice::ReadOnly);
QPoint itemPoint;
dataStream >> itemPoint;
ListWidgetItem *item = dynamic_cast<ListWidgetItem *> (childAt(itemPoint));
if(!item)
return;
QModelIndex index = indexAt(event->pos());
int row = index.row();
model()->insertRows(row,1);
QModelIndex idx =model()->index(row,0);
setIndexWidget(idx,item);
if (event->source() == this) {
event->setDropAction(Qt::MoveAction);
foreach (ListWidgetItem *widget,itemList)
{
widget->setStyleSheet("#ListWidget { border-top: 0px solid black; }");
}
itemList.clear();
// model()->removeRow(n+1);
event->accept();
}
} else{
event->ignore();
}
}
void MyListView::startDrag(Qt::DropActions supportedActions)
{
QByteArray itemData;
QDataStream dataStream(&itemData, QIODevice::WriteOnly);
ListWidgetItem *item = dynamic_cast<ListWidgetItem *>(childAt(dragPoint));
dataStream << dragPoint;
QMimeData *mimeData = new QMimeData;
mimeData->setData("application/x-QListView-DragAndDrop", itemData);
drag = new QDrag(this);
drag->setMimeData(mimeData);
drag->setHotSpot(mapToParent(QPoint(item->x(),y)));
drag->setPixmap(currentPixmap);
QModelIndex indx = indexAt(dragPoint);
if (drag->exec(Qt::MoveAction | Qt::CopyAction) == Qt::MoveAction)
{
model()->removeRow(indx.row());
}
}
I have searched but didnt get any proper solution.
Thanks
Have you looked at QAbstractItemView's DragDropMode ? http://qt-project.org/doc/qt-4.8/qabstractitemview.html#dragDropMode-prop
It sounds as though this ius what you are trying to achieve.

Qt 4.7 - Drawing a 2 point line with dynamic feedback using paintEvent

so I am trying to draw a line between two points. Left mouse click starts the line then I would like the line to by dynamically drawn as the mouse moves (almost like a preview of the line). Left mouse click again and the line will be permanently drawn. I know there are a lot of other posts about QPaintEvents and I have combined some of the techniques used, but for some reason nothing is being drawn to the canvas. Below is the code:
void Main::mousePressEvent(QMouseEvent * event)
{
if (event->button() == Qt::LeftButton) {
QPointF pos = event->pos();
if( mStartPoint.isNull() ) {
if(josh.contains(pos))
mStartPoint = pos;
} else {
canvas.addLine(mStartPoint.x(),mStartPoint.y(),pos.x(),pos.y());
mStartPoint = QPointF();
}
}
}
bool Main::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::MouseMove)
{
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
if (!mStartPoint.isNull()) {
m_targetImage = QImage(canvas.width(),canvas.height(),QImage::Format_ARGB32);
QPainter p;
p.begin(&m_targetImage);
p.drawLine(mStartPoint, mouseEvent->pos());
p.end();
}
statusBar()->showMessage(QString("Mouse move (%1,%2)").arg(mouseEvent->pos().x()).arg(mouseEvent->pos().y()));
}
return false;
}
void Main::paintEvent(QPaintEvent *pe)
{
QPainter painter(this);
QPen pen(Qt::red);
pen.setWidth(10);
painter.setPen(pen);
painter.drawImage(0, 0, m_targetImage);
}
Any help is appreciated! Thanks! Josh
I think this is what you want. Change parameters according to your requirements.
//In your constructor
m_nInitialX = 0;
m_nInitialY = 0;
m_nFinalX = 0;
m_nFinalY = 0;
m_nPTargetPixmap = 0;
m_nPTargetPixmap = new QPixmap(400,400);
m_nbMousePressed = false;
void Main::mousePressEvent(QMouseEvent* event)
{
m_nbMousePressed = true;
m_nInitialX = event->pos().x();
m_nInitialY = event->pos().y();
}
void Main::mouseReleaseEvent(QMouseEvent *event)
{
m_nbMousePressed = false;
}
void Main::paintEvent(QPaintEvent *e)
{
if(m_nbMousePressed)
{
QPainter PixmapPainter(m_nPTargetPixmap);
QPen pen(Qt::green);
PixmapPainter.setPen(pen);
PixmapPainter.drawLine(m_nInitialX, m_nInitialY, m_nFinalX, m_nFinalY);
}
QPainter painter(this);
painter.drawPixmap(0, 0, *m_nPTargetPixmap);
}
void Main::mouseMoveEvent(QMouseEvent *event)
{
if (event->type() == QEvent::MouseMove)
{
QPainter PixmapPainter(m_nPTargetPixmap);
QPen pen(Qt::black);
PixmapPainter.setPen(pen);
PixmapPainter.drawLine(m_nInitialX, m_nInitialY, m_nFinalX, m_nFinalY);
update(); // update your view
m_nFinalX = event->pos().x();
m_nFinalY = event->pos().y();
}
update(); // update your view
}
This piece of code will draw a 2 point line that you want.
Here's my example of how to paint lines in your way directly on the widget.
Declaration:
private:
void drawLines(QPainter *p);
QPoint startPos;
QPoint endPos;
bool inDrawing;
QVector<QLine> lines;
Setting initial values in constructor:
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
startPos = QPoint();
endPos = QPoint();
inDrawing = false;
setMouseTracking(true);
}
Starting or ending the line drawing:
void Widget::mousePressEvent(QMouseEvent *event)
{
if (event->buttons() & Qt::LeftButton)
{
if (!inDrawing)
{
startPos = event->pos();
}
else
{
endPos = event->pos();
QLine line = QLine(startPos, event->pos());
lines.append(line);
}
inDrawing = !inDrawing;
}
}
void Widget::mouseMoveEvent(QMouseEvent *event)
{
if (inDrawing)
{
endPos = event->pos();
update();
}
}
Drawing current and saved lines:
void Widget::drawLines(QPainter *p)
{
if (!startPos.isNull() && !endPos.isNull())
{
p->drawLine(startPos, endPos);
}
p->drawLines(lines);
}
Drawing:
void Widget::paintEvent(QPaintEvent *event)
{
QPainter p(this);
QPen pen;
pen.setColor(Qt::red);
pen.setWidth(4);
p.setPen(pen);
drawLines(&p);
}

Can I get mouse events in a QGraphicsItem?

I have subclassed QGraphicsRectItem, and it's not receiving any mouse events. I've seen other questions similar to this say I need to enable mouse tracking, but setMouseTracking is in QWidget, and QGraphicsItem does not appear to be a QWidget.
I've implemented paint, and that's working. In my subclassed QGraphicsView I am getting mouse events.
The docs seem to think I should just override the mousePressEvent function (for example) and I should start getting the events. Whether I forward the mousePressEvent to the superclass of my QGraphicsView or not doesn't seem to make any difference.
In your subclassed QGraphicsView, you need to call the default implementations of overridden mouse event methods if you want them to propagate down to the items. For example:
CustomView::mousePressEvent(QMouseEvent *event)
{
// handle the event as you like
QGraphicsView::mousePressEvent(event); // then call default implementation
}
If you want to accept hover events, you need to call QGraphicsItem::setAcceptHoverEvents(true);. Otherwise you do not need to enable any particular mouse tracking.
EDIT: Here is a full working example:
#include <QtGui>
class CustomView : public QGraphicsView
{
protected:
void mousePressEvent(QMouseEvent *event)
{
qDebug() << "Custom view clicked.";
QGraphicsView::mousePressEvent(event);
}
};
class CustomItem : public QGraphicsRectItem
{
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event)
{
qDebug() << "Custom item clicked.";
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
CustomItem item;
item.setRect(20, 20, 60, 60);
QGraphicsScene scene(0, 0, 100, 100);
scene.addItem(&item);
CustomView view;
view.setScene(&scene);
view.show();
return a.exec();
}
I went through the same problems you have encountered and I wanted to add some insights on top of Anthony's really good answer. Here is an example I wrote showing some features that can be implemented using the mouse events and the keyboard events.
Note that the events do not propagate to QGraphicsItems in a QGraphicsItemGroup or in a QList<QGraphicsItem> (it took me a while to figure that out).
#include <QtGui>
#include <QGraphicsRectItem>
#include <QGraphicsView>
#include <QApplication>
#include <QGraphicsSceneMouseEvent>
class CustomItem : public QGraphicsEllipseItem
{
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if(event->button() == Qt::LeftButton) {
if(event->modifiers() == Qt::ShiftModifier) {
qDebug() << "Custom item left clicked with shift key.";
// add the item to the selection
setSelected(true);
} else if(event->modifiers() == Qt::AltModifier){
qDebug() << "Custom item left clicked with alt key.";
// resize the item
double radius = boundingRect().width() / 2.0;
_center = QPointF(boundingRect().topLeft().x() + pos().x() + radius, boundingRect().topLeft().y() + pos().y() + radius);
QPointF pos = event->scenePos();
qDebug() << boundingRect() << radius << this->pos() << pos << event->pos();
double dist = sqrt(pow(_center.x()-pos.x(), 2) + pow(_center.y()-pos.y(), 2));
if(dist / radius > 0.8) {
qDebug() << dist << radius << dist / radius;
_isResizing = true;
} else {
_isResizing = false;
}
} else {
qDebug() << "Custom item left clicked.";
QGraphicsItem::mousePressEvent(event);
event->accept();
}
} else if(event->button() == Qt::RightButton) {
qDebug() << "Custom item right clicked.";
event->ignore();
}
}
void mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
if(event->modifiers() == Qt::AltModifier && _isResizing){
QPointF pos = event->scenePos();
double dist = sqrt(pow(_center.x()-pos.x(), 2) + pow(_center.y()-pos.y(), 2));
setRect(_center.x()-this->pos().x()-dist, _center.y()-this->pos().y()-dist, dist*2, dist*2);
} else if(event->modifiers() != Qt::AltModifier) {
qDebug() << "Custom item moved.";
QGraphicsItem::mouseMoveEvent(event);
qDebug()<<"moved"<<pos();
}
}
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
if(event->modifiers() == Qt::AltModifier && _isResizing) {
_isResizing = false;
} else if(event->modifiers() != Qt::ShiftModifier) {
QGraphicsItem::mouseReleaseEvent(event);
}
}
int type() const
{
// Enable the use of qgraphicsitem_cast with this item.
return UserType+1;
}
private:
QPointF _center;
bool _isResizing;
};
class CustomScene : public QGraphicsScene
{
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event)
{
qDebug() << "Custom scene clicked.";
QGraphicsScene::mousePressEvent(event);
if(!event->isAccepted()) {
if(event->button() == Qt::LeftButton) {
// add a custom item to the scene
QPointF pt = event->scenePos();
CustomItem * item = new CustomItem();
item->setRect(pt.x()-25, pt.y()-25, 50, 50);
item->setFlags(QGraphicsItem::ItemIsSelectable|
QGraphicsItem::ItemIsMovable);
addItem(item);
} else if(event->button() == Qt::RightButton) {
// check whether there is an item under the cursor
QGraphicsItem * itemToRemove = NULL;
foreach(auto item, items(event->scenePos())) {
if(item->type() == QGraphicsItem::UserType+1) {
itemToRemove = item;
break;
}
}
if(itemToRemove) {
// remove the item from the graphicsScene
removeItem(itemToRemove);
}
}
}
}
void mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
qDebug() << "Custom scene moved.";
QGraphicsScene::mouseMoveEvent(event);
}
void keyPressEvent(QKeyEvent * event) {
if(event->key() == Qt::Key_Backspace) {
// remove all selected items
qDebug() << "selected items" << selectedItems().size();
while(!selectedItems().isEmpty()) {
removeItem(selectedItems().front());
}
} else {
QGraphicsScene::keyPressEvent(event);
}
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
CustomItem item;
item.setRect(20, 20, 60, 60);
item.setFlags(QGraphicsItem::ItemIsSelectable|
QGraphicsItem::ItemIsMovable);
CustomScene scene;
scene.setSceneRect(0, 0, 500, 500);
scene.addItem(&item);
QGraphicsView view;
view.setScene(&scene);
view.show();
return a.exec();
}
Hope it helps too!
I had a similar problem with a view not accepting mouse clicks. The problem was that I needed to enable the view ( ui->view->setEnabled(true) ).

Resources