×

Discussion Board

Results 1 to 1 of 1
  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    10

    focusInEvent and dragEnterEvent in QML for a custom generated type

    Hello,

    I defined a new type polygon. Some functions from polygon.cpp is below:

    Code:
    Polygon::Polygon(QDeclarativeItem *parent)
        : QDeclarativeItem(parent)
    {
        // need to disable this flag to draw inside a QDeclarativeItem
        setFlag(QDeclarativeItem::ItemHasNoContents, false);
        setFlags(ItemIsSelectable|ItemIsMovable|ItemIsFocusable);
        setAcceptDrops(true);
        setFlag(ItemSendsGeometryChanges);
        setCacheMode(DeviceCoordinateCache);
    
        setZValue(-1);
    
    }
    
    void Polygon::dragEnterEvent(QGraphicsSceneDragDropEvent *event){
        event->acceptProposedAction();
    
        qDebug("drag");
        update();
        QDeclarativeItem::dragEnterEvent(event);
    }
    void Polygon::focusInEvent ( QFocusEvent * event ){
        cout<<"focusin"<<endl;
        update();
    
        QDeclarativeItem::focusInEvent(event);
    }
    
    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);
    
    
    
    }
    My QML file is here:

    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}
             ]
    
         }
         
    
         Text {
    
             anchors { bottom: parent.bottom; horizontalCenter: parent.horizontalCenter; bottomMargin: 20 }
             text: aPolygon.name
         }
     }
    I override paint, focusInEvent, dragEnterEvent, shape, boundingRectangle functions as you can see above, but only paint works as expected. I set flags in constructor too.

    When i run the application, I can see polygons those i declared in the QML file, but how to be notified when the polygon is focused in or dragged into it? I can not see cout result on console. What to do in QML side or C++ side?
    Thanks for any idea.
    Last edited by tuxier; 2011-08-10 at 12:34.

Similar Threads

  1. [QML file] is not a type error
    By Sasler in forum [Archived] Qt Quick
    Replies: 0
    Last Post: 2011-08-02, 20:54
  2. C++ signal and custom type variable use in QML
    By penguin_chris in forum [Archived] Qt Quick
    Replies: 9
    Last Post: 2011-05-24, 15:16
  3. Theme based frame and center for latched item in custom listbox
    By astarter in forum Symbian User Interface
    Replies: 0
    Last Post: 2010-03-07, 09:54
  4. Replies: 0
    Last Post: 2006-07-06, 09:31
  5. Why a class 2 type of message generated.
    By skshambhu in forum General Messaging
    Replies: 1
    Last Post: 2002-10-23, 10:37

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •