I implement drag&drop feature to my project. I can drag&drop.It works fine but override method not getting called.
Am I missing something ?
protected:
void mousePressEvent(QMouseEvent *event)override;
void mouseMoveEvent(QMouseEvent *event)override;
void dragEnterEvent(QDragEnterEvent *event)override;
void dragMoveEvent(QDragMoveEvent *event)override;
void dropEvent(QDropEvent *event)override;
void InformationMusteriDialog::dropEvent(QDropEvent *event) {
QMessageBox::information(this,"dropEvent","dropEvent");
}
void InformationMusteriDialog::mousePressEvent(QMouseEvent *event)
{
QMessageBox::information(this,"mousePressEvent","mousePressEvent");
}
void InformationMusteriDialog::mouseMoveEvent(QMouseEvent *event)
{
QMessageBox::information(this,"mouseMoveEvent","mouseMoveEvent");
}
void InformationMusteriDialog::dragEnterEvent(QDragEnterEvent *event)
{
QMessageBox::information(this,"dragEnterEvent","dragEnterEvent");
}
void InformationMusteriDialog::dragMoveEvent(QDragMoveEvent *event)
{
QMessageBox::information(this,"dragMoveEvent","dragMoveEvent");
}
when i drag or drop nothing pops up.
(I'm reading these links contents for 2 days. Please dont say read the doc again)
https://doc.qt.io/qt-5/dnd.html
https://www.informit.com/articles/article.aspx?p=1405546
I read this but mine still not working
dropEvent() not getting called
Related
I have created a Qwidget with a QRect. Now I would like to move this Rect with my keyboard. Which does not work. When trying with a mousePressEvent instead of the keyPressEvent, everything is working fine though. There are no Focuses yet set, which probably is the problem here. But it's the first time I have to work with those and I just can't figure out what to do and how to use them, even after searching online for the last hours.
I tried putting setFocusPolicy(Qt::StrongFocus) in my widgets constructor amongst other things, but nothing has worked so far. Maybe I'm just blind to an obvious solution, but please help? Thanks alot already
my paint event:
void zeichenFeld::paintEvent(QPaintEvent *event)
{
QPainter painter;
painter.begin (this);
//Avatar:
QRect rectAv=QRect(xAv, yAv, 20,20);
painter.fillRect(rectAv, Qt::BrushStyle::SolidPattern);
painter.fillRect(rectAv, colorAv);
painter.drawRect(rectAv);
painter.end();
}
void zeichenFeld::keyPressEvent(QKeyEvent *event)
{
if(event->key()==Qt::Key_A){
xAv-=10;
}
if(event->key()==Qt::Key_D){
xAv+=10;
}
update();
}
void zeichenFeld::mousePressEvent(QMouseEvent *event){
if(event->button()==Qt::LeftButton)
{
xAv-=25;
}
if(event->button()==Qt::RightButton)
{
xAv+=25;
}
update();
}
header:
protected:
void paintEvent(QPaintEvent *event);
void keyPressEvent(QKeyEvent *event);
void mousePressEvent(QMouseEvent *event);
Edit: i simply added the setFocusPolicy(Qt::StrongFocus); to the wrong contructor. Working now
a QRect is not a visual class, so if you used the PaintEvent to draw a rectangle, then you need to come up with your special way to handle that in the mouse press event.
something like:
YourClass.h
protected:
void mousePressEvent(QMouseEvent *ev) override;
void mouseMoveEvent(QMouseEvent *ev) override;
void mouseReleaseEvent(QMouseEvent *ev) override;
private:
QRect _myRect;
YourClass.cpp
void YourClass::mousePressEvent(QMouseEvent *ev) {
if (_myRect.contains(ev->localPos())){
_isRectSelected = true;
}
}
void YourClass::mouseMoveEvent(QMouseEvent *ev) {
if (_isRectSelected) {
_myRect->setX(ev->localPos().x());
_myRect->setY(ev->localPos().y());
}
}
void YourClass::mouseReleaseEvent(QMouseEvent *ev) {
if (_isRectSelected) {
_isRectSelected = false;
}
}
but I wouldn't reccomend that. Try to use QGraphicsView with QGraphicsRectItem as it's a visual class. it knows how to paint itself and it also has mouse handling.
Or even better, Qml.
I got simple class MyStackedWidget but looks like it doesn't fires drag and drop events - what am I doing wrong?
#ifndef MYSTACKEDWIDGET_H
#define MYSTACKEDWIDGET_H
#include <QStackedWidget>
class MyStackedWidget : public QStackedWidget
{
Q_OBJECT
public:
MyStackedWidget();
signals:
void dragEnterSignal(QDragEnterEvent *event);
void dropSignal(QDropEvent *event);
void dragMoveSignal(QDragMoveEvent *event);
void dragLeaveSignal(QDragLeaveEvent *event);
public slots:
void dragEnterEvent(QDragEnterEvent *event);
void dropEvent(QDropEvent *event);
void dragMoveEvent(QDragMoveEvent *event);
void dragLeaveEvent(QDragLeaveEvent *event);
};
#endif // MYSTACKEDWIDGET_H
#include "mystackedwidget.h"
#include <QMimeData>
#include <QDragEnterEvent>
MyStackedWidget::MyStackedWidget()
{
setMouseTracking(true);
}
void MyStackedWidget::dragEnterEvent(QDragEnterEvent *event)
{
const QMimeData *mimeData = event->mimeData();
if(mimeData->hasUrls() && mimeData->urls().size() == 1) {
event->acceptProposedAction();
}
emit dragEnterSignal(event);
}
void MyStackedWidget::dropEvent(QDropEvent *event)
{
emit dropSignal(event);
}
void MyStackedWidget::dragMoveEvent(QDragMoveEvent *event)
{
emit dragMoveSignal(event);
}
void MyStackedWidget::dragLeaveEvent(QDragLeaveEvent *event)
{
emit dragLeaveSignal(event);
}
I have read this How to distinguish between mouseReleaseEvent and mousedoubleClickEvent on QGrapnhicsScene and this Distinguish between single and double click events in Qt in order to solve my problem. But they do not work. I created a class QLabel:
mouselabel.h
#ifndef MOUSELABEL_H
#define MOUSELABEL_H
#include <QLabel>
#include <QMouseEvent>
#include <QEvent>
#include <QDebug>
class MouseLabel : public QLabel
{
Q_OBJECT
public:
explicit MouseLabel(QWidget *parent = 0);
void mouseMoveEvent(QMouseEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
void mouseDoubleClickEvent(QMouseEvent *event);
void leaveEvent(QMouseEvent *);
int x, y;
signals:
void Mouse_Pressed();
void Mouse_Position();
void Mouse_Left();
void Mouse_Release();
void Mouse_DoubleClick();
public slots:
};
#endif // MOUSELABEL_H
mouselabel.cpp
#include "mouselabel.h"
MouseLabel::MouseLabel(QWidget *parent) :
QLabel(parent)
{
}
void MouseLabel::mouseMoveEvent(QMouseEvent *event) {
this->x = event->x();
this->y = event->y();
emit Mouse_Position();
}
void MouseLabel::mousePressEvent(QMouseEvent *event)
{
emit Mouse_Pressed();
}
void MouseLabel::mouseReleaseEvent(QMouseEvent *event)
{
emit Mouse_Release();
}
void MouseLabel::mouseDoubleClickEvent(QMouseEvent *event)
{
emit Mouse_DoubleClick();
}
void MouseLabel::leaveEvent(QMouseEvent *)
{
emit Mouse_Left();
}
As described in the two links, in order to distinguish between a click and a double click, I created a timer and it starts when a mouserelease event happens and it stops when there is a double click mouse. But, this does not work. Can you help me to solve the problem?
The lightest possible solution is using a QBasicTimer, just have those 2 methods overloaded and the timer in your widget:
void mouseReleaseEvent(QMouseEvent *) {
if (timer.isActive()) {
timer.stop();
qDebug() << "double click";
}
else {
timer.start(300, this);
}
}
void timerEvent(QTimerEvent *) {
timer.stop();
qDebug() << "single click";
}
QBasicTimer timer;
The downside is a timer approach delays the single click, which is kind of annoying, but you could mask that, for example for selecting objects, using the mousePressEvent for immediate response.
I dont understand what`s going on: when i create QGraphicsView object directly and adding scene with a pixmap, all is ok, pixmap appears on the screen:
scene.addPixmap(pix);
QGraphicsView graphicsView;
graphicsView.setScene(&scene);
But when i try to inherit QGraphicsView class with purpose of reimplementing events, nothing happend and i got white screen without pixmap, but events like changing cursor is working:
scene.addPixmap(pix);
DrawArea graphicsView;
graphicsView.setScene(&scene);
.h file:
class DrawArea : public QGraphicsView
{
Q_OBJECT
public:
DrawArea(QWidget *parent = 0);
~DrawArea();
signals:
public slots:
void mousePressEvent(QMouseEvent * e);
void paintEvent(QPaintEvent *);
void enterEvent(QEvent *e);
private:
QPoint coord;
};
.cpp file:
DrawArea::DrawArea(QWidget *parent)
: QGraphicsView(parent){
}
DrawArea::~DrawArea(){
}
void DrawArea::mousePressEvent(QMouseEvent * event){
}
void DrawArea::paintEvent(QPaintEvent *event){
}
void DrawArea::enterEvent(QEvent *event){
viewport()->setCursor(Qt::CrossCursor);
}
Tell me if something missed, Thanks in advance.
You should process your events. Try this:
void DrawArea::mousePressEvent(QMouseEvent * event)
{
//some actions
QGraphicsView::mousePressEvent(event);
}
void DrawArea::paintEvent(QPaintEvent *event)
{
//some actions
QGraphicsView::paintEvent(event);
}
Also I think that you don't need paintEvent at all, do all needed things in the scene.
Ok, I've been able to get get Drag and Drop working on QGraphicsScene, but not QGraphicsView. My code looks something like this:
class GraphicsView : public QGraphicsView
{
public:
GraphicsView(QGraphicsScene *scene) : QGraphicsView(scene) {}
protected:
void dragEnterEvent(QGraphicsSceneDragDropEvent *event)
{
event->setAccepted(true);
update();
}
void dragMoveEvent(QGraphicsSceneDragDropEvent *event)
{
event->setAccepted(true);
update();
}
void dropEvent(QGraphicsSceneDragDropEvent *event)
{
//stuff that never runs...
update();
}
};
Although, this code works perfectly fine with QGraphicsScene inherited instead of QGraphicsView. I also ran the setAcceptDrops(true) function. What am I doing wrong?