Results 1 to 7 of 7

Thread: How to be notified on dragEnterEvent?

  1. #1
    Join Date
    Jul 2011
    Posts
    22
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to be notified on dragEnterEvent?

    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
    Qt Code:
    1. Polygon {
    2. id: aPolygon
    3. anchors.centerIn: parent
    4. width: 100; height: 100
    5. name: "A simple polygon"
    6. color: "blue"
    7. vertices:[
    8.  
    9. Point{x:20.0; y:40.0},
    10. Point{x:40.0; y:40.0},
    11. Point{x:20.0; y:20.0}
    12. ]
    13.  
    14. dragEnterEvent: aPolygon.doSomething
    15.  
    16.  
    17. }
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    Join Date
    Dec 2010
    Location
    Veszprém, Hungary
    Posts
    34
    Thanks
    2
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to be notified on dragEnterEvent?

    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

  3. #3
    Join Date
    Jul 2011
    Posts
    22
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to be notified on dragEnterEvent?

    Hi, thanks for reply.
    I tried to add signal but could not run.

    Here is my polygon.cpp code:

    Qt Code:
    1. #include "polygon.h"
    2. #include "point.h"
    3. #include <QPainter>
    4. #include <stdio.h>
    5. #include <iostream>
    6. #include <sstream>
    7. #include <QGraphicsSceneDragDropEvent>
    8. #include <QFocusEvent>
    9. #include "DeclarativeDragDropEvent.h"
    10.  
    11. using namespace std;
    12. using namespace Qt;
    13.  
    14. Polygon::Polygon(QDeclarativeItem *parent)
    15. : QDeclarativeItem(parent)
    16. {
    17. // need to disable this flag to draw inside a QDeclarativeItem
    18. setFlag(QDeclarativeItem::ItemHasNoContents, false);
    19. setFlags(ItemIsSelectable|ItemIsMovable|ItemIsFocusable);
    20. setAcceptDrops(true);
    21.  
    22.  
    23. }
    24. QVariant Polygon::itemChange(GraphicsItemChange change, const QVariant &value)
    25. {
    26.  
    27. return QGraphicsItem::itemChange(change, value);
    28. }
    29.  
    30.  
    31. void Polygon::focusInEvent ( QFocusEvent * event ){
    32. cout<<"focusin"<<endl;
    33. }
    34.  
    35. QRectF Polygon::boundingRect() const{
    36.  
    37. QVector<QPointF> vPnt=listToVector(m_vertices);
    38. return QPolygonF(vPnt).boundingRect();
    39.  
    40. }
    41.  
    42. QPainterPath Polygon::shape () const
    43. {
    44. QVector<QPointF> vPnt=listToVector(m_vertices);
    45. path.addPolygon(QPolygonF(vPnt));
    46. return path;
    47. }
    48. ...
    49.  
    50. void Polygon::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
    51. {
    52. QPen pen(m_color, 2);
    53. painter->setPen(pen);
    54. painter->setRenderHints(QPainter::Antialiasing, true);
    55.  
    56.  
    57. QVector<QPointF> vPnt=listToVector(m_vertices);
    58. painter->setBrush(QBrush(m_color,Qt::SolidPattern));
    59. painter-> drawPolygon(QPolygonF(vPnt),Qt::OddEvenFill);
    60.  
    61.  
    62.  
    63. }
    To copy to clipboard, switch view to plain text mode 

    Here is main.qml code:

    Qt Code:
    1. import MyTypes 1.0
    2. import QtQuick 1.0
    3. import Qt 4.7
    4.  
    5. Item {
    6. id:container
    7. width: 300; height: 200
    8.  
    9. Polygon {
    10. id: aPolygon
    11. anchors.centerIn: parent
    12. width: 100; height: 100
    13. name: "A simple polygon"
    14. color: "blue"
    15. vertices:[
    16.  
    17. Point{x:20.0; y:40.0},
    18. Point{x:40.0; y:40.0},
    19. Point{x:40.0; y:20.0},
    20. Point{x:20.0; y:20.0}
    21. ]
    22.  
    23.  
    24. }
    25. }
    To copy to clipboard, switch view to plain text mode 


    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.

  4. #4
    Join Date
    Dec 2010
    Location
    Veszprém, Hungary
    Posts
    34
    Thanks
    2
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to be notified on dragEnterEvent?

    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
    Qt Code:
    1. public slots:
    2. void testSignal();
    To copy to clipboard, switch view to plain text mode 

    in poligon.cpp somewhere
    Qt Code:
    1. emit testSignal();
    To copy to clipboard, switch view to plain text mode 

    in the qml file
    Qt Code:
    1. Polygon {
    2. ....
    3. onTestSignal: {
    4. console.log("TestSignal was emitted")
    5. }
    6. }
    To copy to clipboard, switch view to plain text mode 

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

  5. #5
    Join Date
    Jul 2011
    Posts
    22
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to be notified on dragEnterEvent?

    hi,
    your post is so useful but i have given up to try

    Qt Code:
    1. onFocusIn:{ //something}
    To copy to clipboard, switch view to plain text mode 

    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

    Qt Code:
    1. void Polygon::mousePressEvent(QGraphicsSceneMouseEvent *event)
    2. {
    3. forceActiveFocus();
    4. }
    To copy to clipboard, switch view to plain text mode 

    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?

  6. #6
    Join Date
    Dec 2010
    Location
    Veszprém, Hungary
    Posts
    34
    Thanks
    2
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to be notified on dragEnterEvent?

    Ok, but it's the another direction.

    Read FocusScope documentation in QML doc.
    You can try something like:
    Qt Code:
    1. FocusScope {
    2. id: scope
    3. Polygon {
    4. id: poly
    5. focus = true
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

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

  7. The following user says thank you to laszlo.gosztola for this useful post:

    tuxit (14th August 2011)

  8. #7
    Join Date
    Jul 2011
    Posts
    22
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to be notified on dragEnterEvent?

    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

Similar Threads

  1. Replies: 11
    Last Post: 25th July 2010, 13:01
  2. Not getting dragEnterEvent - sometimes
    By derekn in forum Qt Programming
    Replies: 0
    Last Post: 30th October 2009, 06:41
  3. Replies: 1
    Last Post: 28th October 2008, 12:14
  4. How to be notified when a child changes?
    By mooreaa in forum Qt Programming
    Replies: 1
    Last Post: 3rd July 2008, 13:04

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.