Drag and drop in QML [closed] - qt

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
How can I develop drag and drop functionality in QML? I want to drag and drop one image to another.

At this point in time, you will probably need to use C++, especially if you want to accept drops from outside the QML application (e.g. the user drags a file from a file manager to your app). Here's an example component class to implement a DropArea item:
DropArea.h:
#ifndef DropArea_H
#define DropArea_H
#include <QDeclarativeItem>
/**
An oversimplified prototype Item which accepts any drop that includes
data with mime type of text/plain, and just emits the text.
*/
class DropArea : public QDeclarativeItem
{
Q_OBJECT
Q_PROPERTY(bool acceptingDrops READ isAcceptingDrops WRITE setAcceptingDrops NOTIFY acceptingDropsChanged)
public:
DropArea(QDeclarativeItem *parent=0);
bool isAcceptingDrops() const { return m_accepting; }
void setAcceptingDrops(bool accepting);
signals:
void textDrop(QString text);
void acceptingDropsChanged();
protected:
void dragEnterEvent(QGraphicsSceneDragDropEvent *event);
void dragLeaveEvent(QGraphicsSceneDragDropEvent *event);
void dropEvent(QGraphicsSceneDragDropEvent *event);
private:
bool m_accepting;
};
#endif
DropArea.cpp:
#include <QGraphicsSceneDragDropEvent>
#include <QMimeData>
#include "DropArea.h"
DropArea::DropArea(QDeclarativeItem *parent)
: QDeclarativeItem(parent),
m_accepting(true)
{
setAcceptDrops(m_accepting);
}
void DropArea::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
{
event->acceptProposedAction();
setCursor(Qt::DragMoveCursor);
}
void DropArea::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
{
unsetCursor();
}
void DropArea::dropEvent(QGraphicsSceneDragDropEvent *event)
{
emit textDrop(event->mimeData()->text());
unsetCursor();
}
void DropArea::setAcceptingDrops(bool accepting)
{
if (accepting == m_accepting)
return;
m_accepting = accepting;
setAcceptDrops(m_accepting);
emit acceptingDropsChanged();
}
your QML:
DropArea {
onTextDrop: ...
}
And you can implement a DragSourceArea similarly.

I know it's been a while but I struggled so much with this I wanted to share the Qt5 version:
Based on ecloud an Michael's example:
DropArea.h:
#ifndef DropArea_H
#define DropArea_H
#include <QQuickItem>
/**
An oversimplified prototype Item which accepts any drop that includes
data with mime type of text/plain, and just emits the text.
*/
class DropArea : public QQuickItem
{
Q_OBJECT
Q_PROPERTY(bool acceptingDrops READ isAcceptingDrops WRITE setAcceptingDrops NOTIFY acceptingDropsChanged)
public:
DropArea(QQuickItem *parent=0);
bool isAcceptingDrops() const { return m_accepting; }
void setAcceptingDrops(bool accepting);
signals:
void textDrop(QString text);
void acceptingDropsChanged();
protected:
void dragEnterEvent(QDragEnterEvent *event);
void dragLeaveEvent(QDragLeaveEvent *event);
void dropEvent(QDropEvent *event);
private:
bool m_accepting;
};
#endif
DropArea.cpp:
#include <QGraphicsSceneDragDropEvent>
#include <QMimeData>
#include "DropArea.h"
DropArea::DropArea(QQuickItem *parent)
: QQuickItem (parent),
m_accepting(true)
{
setFlag(QQuickItem::ItemAcceptsDrops, m_accepting);
}
void DropArea::dragEnterEvent(QDragEnterEvent *event)
{
event->acceptProposedAction();
}
void DropArea::dragLeaveEvent(QDragLeaveEvent *event)
{
unsetCursor();
}
void DropArea::dropEvent(QDropEvent *event)
{
qDebug() << event->mimeData()->text();
unsetCursor();
}
void DropArea::setAcceptingDrops(bool accepting)
{
if (accepting == m_accepting)
return;
m_accepting = accepting;
setFlag(QQuickItem::ItemAcceptsDrops, m_accepting);
emit acceptingDropsChanged();
}
Qml:
Drop2 {
id: myDropArea
}

Related

enable/disable widget using mousePressEvent/mouseReleaseEvent

I have a set of widgets to control a parameter in five similar places (or channels).
Now I can enable/disable each of these widgets using a pushbuttons, as follows.
connect(ui->pushButton_currOnOne, &QPushButton::clicked, ui->widget_currentOne, &CurrentButtonOne::setEnabled);
connect(ui->pushButton_currOnTwo, &QPushButton::clicked, ui->widget_currentTwo, &CurrentButtonOne::setEnabled);
connect(ui->pushButton_currOnThree, &QPushButton::clicked, ui->widget_currentThree, &CurrentButtonOne::setEnabled);
connect(ui->pushButton_currOnFour, &QPushButton::clicked, ui->widget_currentFour, &CurrentButtonOne::setEnabled);
connect(ui->pushButton_currOnFive, &QPushButton::clicked, ui->widget_currentFive, &CurrentButtonOne::setEnabled);
Can I use mousePressEvent/mouseReleaseEvent instead of using &QPushButton::clicked in the above scenario?
It will be very helpful if you could show me an example.
Thanks in advance
Some code to get you started.
mybutton.h:
#include <QObject>
#include <QPushButton>
#include <QMouseEvent>
class MyButton : public QPushButton
{
public:
explicit MyButton(QWidget *parent = nullptr);
protected:
void mousePressEvent(QMouseEvent *e) override;
void mouseReleaseEvent(QMouseEvent *e) override;
private:
bool isPressed = false;
signals:
void myButtonPressed();
};
mybutton.cpp:
#include "mybutton.h"
MyButton::MyButton(QWidget *parent) : QPushButton(parent)
{
}
void MyButton::mousePressEvent(QMouseEvent *e)
{
isPressed = true;
emit myButtonPressed();
QPushButton::mousePressEvent(e);
}
void MyButton::mouseReleaseEvent(QMouseEvent *e)
{
isPressed = false;
QPushButton::mouseReleaseEvent(e);
}
I'm not sure, what exactly you are trying to achieve, but this idea should meet your needs.
You create a class, which inherits from QPushButton (or QAbstractButton) and override its mousePressEvent and mouseReleaseEvent. If you want some other widgets to react on this events, you emit signal and connect other widgets to that signal.

How to implement custom boolean widget for use with QDataWidgetMapper?

What do I have to implement to make my own boolean widget for use with QDataWidgetMapper?
I have created a USER property that the mapper is, to my understanding, supposed to use.
The interaction with the database via QSqlTableModel using the (same) QDataWidgetMapper works with qt's own widgets, but does not seem to work with this widget I created.
Even though the widget works and displays database data, changing widget value does not change the value in the database. [correction: data from database is read to the widget state ok.]
Current header file:
#ifndef COMPENSATIONCONTROLCHECKABLE_H
#define COMPENSATIONCONTROLCHECKABLE_H
#include <QAbstractButton>
#include <QWidget>
namespace Ui {
class CompensationControlCheckable;
}
class CompensationControlCheckable : public QAbstractButton
{
Q_OBJECT
Q_PROPERTY(bool checked READ isChecked WRITE setChecked NOTIFY compensationChanged USER true )
public:
explicit CompensationControlCheckable(QWidget *parent = 0);
~CompensationControlCheckable();
void setChecked(bool checkd);
const bool isChecked() const;
void paintEvent(QPaintEvent *e);
void resizeEvent(QResizeEvent *e);
private slots:
void on_startCompensation_clicked();
void on_cancelCompensation_clicked();
signals:
void compensationChanged(bool);
void checked(bool);
private:
bool checkedValue;
Ui::CompensationControlCheckable *ui;
};
#endif // COMPENSATIONCONTROLCHECKABLE_H
Version 2, now I have removed checked overrides and instead call the QAbstractButton setChecked and isChecked in setCompensation and isCompensation, respectively. The constructor calls setCheckable(true);. This works exactly as above; reading from db ok, writing to db does not work:
#ifndef COMPENSATIONCONTROLCHECKABLE_H
#define COMPENSATIONCONTROLCHECKABLE_H
#include <QAbstractButton>
#include <QWidget>
namespace Ui {
class CompensationControlCheckable;
}
class CompensationControlCheckable : public QAbstractButton
{
Q_OBJECT
Q_PROPERTY(bool compensation READ isCompensation WRITE setCompensation NOTIFY compensationChanged USER true )
public:
explicit CompensationControlCheckable(QWidget *parent = 0);
~CompensationControlCheckable();
void setCompensation(bool checkd);
bool isCompensation() const;
void paintEvent(QPaintEvent *e);
void resizeEvent(QResizeEvent *e);
private slots:
void on_startCompensation_clicked();
void on_cancelCompensation_clicked();
signals:
void compensationChanged(bool);
private:
bool compensationValue;
Ui::CompensationControlCheckable *ui;
};
#endif // COMPENSATIONCONTROLCHECKABLE_H
QDataWidgetMapper prefers to use the property with USER true for its mapping but there already one called checked in QAbstractButton use that property instead. You may need to call isCheckable(true) in the constructor;

Paste operation in QGraphicsView

I am implenting copy paste operation in QGraphicsView to different entites like circle, point, ellipse etc. Applied copy operation but unable to get how to apply paste operation for it. Please help me out solve the problem. My slot for copy appears as follows:
cadgraphicsscene.cpp
void CadGraphicsScene::copy()
{
selectItems();
}
void CadGraphicsScene::selectItems()
{
// refresh record of selected items and their starting positions
selectedItems.clear();
foreach (QGraphicsItem *item, itemList)
{
if (item->isSelected())
{
if (dynamic_cast<QGraphicsItem *>(item))
{
selectedItems.append(qMakePair(
dynamic_cast<QGraphicsItem *>(item),
item->scenePos()));
}
}
}
}
cadgraphicsscene.h
#ifndef CADGRAPHICSSCENE_H
#define CADGRAPHICSSCENE_H
#include <QGraphicsScene>
#include <QXmlStreamWriter>
#include <QUndoStack>
#include "cadcommandadd.h"
#include "cadcommanddelete.h"
#include "cadcommandmove.h"
class CadGraphicsScene : public QGraphicsScene
{
Q_OBJECT
public:
explicit CadGraphicsScene(QObject *parent, QUndoStack *);
enum Mode { NoMode, PointMode, LineMode, CircleMode, EllipseMode, TextMode };
QFont font() const
{
return myFont;
}
QColor textColor() const
{
return myTextColor;
}
void setTextColor(const QColor &color);
void setFont(const QFont &font);
void deleteItems();
void copy();
void writeStream(QXmlStreamWriter *stream);
void readStream(QXmlStreamReader *stream);
public slots:
void setMode(Mode mode);
void selectItems();
void editorLostFocus(mText *item);
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent);
void setFlags();
void areItemsSelectable(bool);
signals:
void textInserted(QGraphicsTextItem *item);
void itemSelected(QGraphicsItem *item);
private:
Mode entityMode;
QUndoStack *mUndoStack;
bool mFirstClick;
bool mSecondClick;
bool mThirdClick;
bool mPaintFlag;
QVector<QPointF> stuff;
QPointF start_p, mid_p, end_p, move_p, check_p;
QPen paintpen, linePen;
QList<QGraphicsItem *> itemList;
Point *pointItem;
Line *lineItem;
Circle *circleItem;
Ellipse *ellipseItem;
mText *textItem;
QColor myTextColor;
QFont myFont;
typedef QPair<QGraphicsItem *, QPointF> itemPos;
QList<itemPos> selectedItems;
};
#endif // CADGRAPHICSSCENE_H

Cannot implement Drag and Drop for TextItem

this is my first try trying to use Drag&Drop feature of Qt. I'm a beginner, I made my first subclassing this week although I have made other 2 Qt programs in the past.
I need a movable by Drag&Drop QGraphicsTextItem to show on a QGraphicsView that is connected to the corresponding QGraphicScene. So I can retrieve the new position of the item.
I have looked at the animated robot example and this link: http://www.qtcentre.org/threads/50028-Drag-and-Drop-QGraphicsTextItem
The code of the link above, looked well for my. So I reimplemented it but when building, the compiler shows all kind of errors I´m not sure how to overcome. I don't know where to start, and don't know what piece of code is incorrect...
examples of errors appearing:
error: no matching function for call to 'GraphicsTextItem::setCursor(Qt::CursorShape)'
setCursor(Qt::OpenHandCursor);
^
error: invalid use of incomplete type 'class QGraphicsSceneDragDropEvent'
if(event->mimeData()->hasText())
^
error: forward declaration of 'class QGraphicsSceneDragDropEvent'
class QGraphicsSceneDragDropEvent;
^
I'll leave the code:
Header:
#ifndef GRAPHICSTEXTITEM_H
#define GRAPHICSTEXTITEM_H
#include <QGraphicsTextItem>
class GraphicsTextItem : public QGraphicsTextItem
{
Q_OBJECT
public:
GraphicsTextItem(QGraphicsItem *parent = 0);
protected:
void dragEnterEvent(QGraphicsSceneDragDropEvent *event);
void dragLeaveEvent(QGraphicsSceneDragDropEvent *event);
void dropEvent(QGraphicsSceneDragDropEvent *event);
void mousePressEvent(QGraphicsSceneMouseEvent *);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *);
void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
private:
bool dragOver;
};
//! [0]
#endif // GRAPHICSTEXTITEM_H
and the Implementation:
#include <graphicstextitem.h>
#include <QDrag>
//#include <QGraphicsScene>
GraphicsTextItem::GraphicsTextItem(QGraphicsItem *parent)
:QGraphicsTextItem(parent)
{
//setFlag(QGraphicsItem::ItemIsSelectable);
setFlag(QGraphicsItem::ItemIsMovable);
setTextInteractionFlags(Qt::TextEditorInteraction);
setAcceptedMouseButtons(Qt::LeftButton);
setAcceptDrops(true);
setCursor(Qt::OpenHandCursor);
}
void GraphicsTextItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
{
if(event->mimeData()->hasText())
{
event->setAccepted(true);
update();
dragOver = true;
}
else
event->setAccepted(false);
}
void GraphicsTextItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
{
Q_UNUSED(event);
dragOver = false;
update();
}
void GraphicsTextItem::dropEvent(QGraphicsSceneDragDropEvent *event)
{
event->setAccepted(true);
//qDebug() << "I dropped it";
dragOver = false;
update();
}
void GraphicsTextItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
Q_UNUSED(event);
setCursor(Qt::ClosedHandCursor);
}
void GraphicsTextItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
if (QLineF(event->screenPos(), event->buttonDownScreenPos(Qt::LeftButton)).length()
< QApplication::startDragDistance())
return;
QDrag *drag = new QDrag(event->widget());
QMimeData *mime = new QMimeData;
mime->setText(this->toPlainText());
drag->setMimeData(mime);
drag->exec();
setCursor(Qt::OpenHandCursor);
}
void GraphicsTextItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
Q_UNUSED(event);
setCursor(Qt::OpenHandCursor);
}
The error message is telling you that there is a forward declaration somewhere in your code, although that is not true based on the code you have shown so far.
Either way, for accessingt members of your heap object, you need more than just forward declaration. You are missing this include:
#include <QGraphicsSceneDragDropEvent>

QLineEdit mouseEnter() and mouseExit() events

I want to subclass QLineEdit to add it some signals like mouseEnter() and mouseExit() to become informed when the mouse courser is over the widget and when leaves it.
I could write mouseEnter() like below.
class MyLineEdit:public QLineEdit{
//
// ..
//
protected:
void mouseMoveEvent(QMouseEvent *e);
signals:
void mouseEnter();
}
void MyLineEdit::mouseMoveEvent(QMouseEvent *e)
{
emit mouseEnter();
QLineEdit::mouseMoveEvent(e);
}
It works correctly.
How can I write mouseExit()?
leaveEvent is not a very good name!
#include <QtGui>
#include <QEvent>
class Editor : public QLineEdit
{
Q_OBJECT
public:
void leaveEvent(QEvent *);
signals:
void mouseLeave();
};
void Editor::leaveEvent(QEvent *e);
{
qDebug() << "Mouse has left the building..";
emit mouseLeave();
}

Resources