PDA

View Full Version : How to be notified on dragEnterEvent?



tuxit
9th August 2011, 20:03
Hello,

I have a custom type called polygon, which is a subcalss of QDeclarativeItem.

I want to be notified when a mouse clicked out of it, then dragged into it, (before mouse released!!!).

I have found out that QDeclarativeItem(super class of my custom type) has a function: dragEnterEvent.

I can override it in C++ class, but then everything will be all in C++.

I want to ask if it is possible to write something like


Polygon {
id: aPolygon
anchors.centerIn: parent
width: 100; height: 100
name: "A simple polygon"
color: "blue"
vertices:[

Point{x:20.0; y:40.0},
Point{x:40.0; y:40.0},
Point{x:20.0; y:20.0}
]

dragEnterEvent: aPolygon.doSomething


}

How can it be possible? Using mouse area, but it does not have dragEnterEvent. How can i do this?
Thanks for any idea.

laszlo.gosztola
10th August 2011, 16:14
Hi!

Try to create a signal at C++ side and a handler on QML side with on prefix.
Like this:
c++
signals:
void dragStarted();

QML:
onDragStarted()

http://doc.qt.nokia.com/4.7-snapshot/qml-extending.html

tuxit
11th August 2011, 17:47
Hi, thanks for reply.
I tried to add signal but could not run.

Here is my polygon.cpp code:




#include "polygon.h"
#include "point.h"
#include <QPainter>
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <QGraphicsSceneDragDropEvent>
#include <QFocusEvent>
#include "DeclarativeDragDropEvent.h"

using namespace std;
using namespace Qt;

Polygon::Polygon(QDeclarativeItem *parent)
: QDeclarativeItem(parent)
{
// need to disable this flag to draw inside a QDeclarativeItem
setFlag(QDeclarativeItem::ItemHasNoContents, false);
setFlags(ItemIsSelectable|ItemIsMovable|ItemIsFocu sable);
setAcceptDrops(true);


}
QVariant Polygon::itemChange(GraphicsItemChange change, const QVariant &value)
{

return QGraphicsItem::itemChange(change, value);
}


void Polygon::focusInEvent ( QFocusEvent * event ){
cout<<"focusin"<<endl;
}

QRectF Polygon::boundingRect() const{

QVector<QPointF> vPnt=listToVector(m_vertices);
return QPolygonF(vPnt).boundingRect();

}

QPainterPath Polygon::shape () const
{
QPainterPath path;
QVector<QPointF> vPnt=listToVector(m_vertices);
path.addPolygon(QPolygonF(vPnt));
return path;
}
...

void Polygon::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
QPen pen(m_color, 2);
painter->setPen(pen);
painter->setRenderHints(QPainter::Antialiasing, true);


QVector<QPointF> vPnt=listToVector(m_vertices);
painter->setBrush(QBrush(m_color,Qt::SolidPattern));
painter-> drawPolygon(QPolygonF(vPnt),Qt::OddEvenFill);



}





Here is main.qml code:


import MyTypes 1.0
import QtQuick 1.0
import Qt 4.7

Item {
id:container
width: 300; height: 200

Polygon {
id: aPolygon
anchors.centerIn: parent
width: 100; height: 100
name: "A simple polygon"
color: "blue"
vertices:[

Point{x:20.0; y:40.0},
Point{x:40.0; y:40.0},
Point{x:40.0; y:20.0},
Point{x:20.0; y:20.0}
]


}
}



I can see a polygon, but why focusInEvent is not called?
As you can see, i override boundingRect ans shape but no result.
I was doing nearly same things in Qt with C++ without QML.
Thanks for any idea.

laszlo.gosztola
11th August 2011, 20:41
Hello,

maybe we have some misundersanding.
I thought, You want to notify the QML side about a signal which happened at c++ side.

For an example of this, define a new signal in your header file, and in the cpp file somewher emit this signal.

e.g. in poligon.h


public slots:
void testSignal();


in poligon.cpp somewhere


emit testSignal();


in the qml file


Polygon {
....
onTestSignal: {
console.log("TestSignal was emitted")
}
}


it's important, that the signal is lowercase in the header, but go to uppercase when gets the 'on' prefix

tuxit
12th August 2011, 03:57
hi,
your post is so useful but i have given up to try


onFocusIn:{ //something}

in QML side. It is the later problem.

for the moment, i only want my focusInEvent and focusOutEvent in polygon.cpp to be called when i click the polygon shape on QML viewer.

I succeed to make focusInEvent work by


void Polygon::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
forceActiveFocus();
}

but now the problem have changed. focusOutEvent is not called when i click out of polygon.
And i could not find a mouseOutEvent.

If focusOutEvent do not work why it is there?

laszlo.gosztola
12th August 2011, 12:37
Ok, but it's the another direction.

Read FocusScope documentation in QML doc.
You can try something like:


FocusScope {
id: scope
Polygon {
id: poly
focus = true
}
}


But as far as I know, focus is for keyboard handling. So why do You mix it with mouse handling?

tuxit
12th August 2011, 13:29
i was able to do what i want when i was working with Qt (without QML) and focusInEvent was working as i expected but in QML does not work. Anyway i will try FocusScope, but now i have another problem. Thanks