Что нужно сделать, чтобы "onEntered" работала? - PullRequest
1 голос
/ 14 августа 2011

У меня есть собственный подкласс QDeclarativeItem с именем Polygon.Я добавляю MouseArea, но onEntered или onPressed не работает, или я ожидаю, что произойдет что-то неправильное?Я вижу свои полигоны в окне, но на консоли onPressed или onEntered ничего не пишется.

Вот файл QML:

import MyTypes 1.0
import QtQuick 1.0
import Qt 4.7

Item {
    id: container
    width: 350; height: 250

     Polygon {
         id: aPolygon

         width: 20; height: 20
         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}
         ]

         MouseArea{
             anchors.fill: parent
             drag.target: aPolygon
             drag.axis: Drag.XandYAxis
             drag.minimumX: 0
             drag.maximumX: container.width - parent.width
             drag.minimumY: 0
             drag.maximumY: container.height - parent.width
             onPressed:console.log("==============   ==onPressed")

         }


     }

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

         Point{x:60.0; y:80.0},
         Point{x:80.0; y:80.0},
         Point{x:80.0; y:60.0},
         Point{x:60.0; y:60.0}
         ]

         MouseArea{
             //hoverEnabled: false
             enabled: visible
             hoverEnabled: visible
             anchors.fill: parent
             acceptedButtons: Qt.LeftButton | Qt.RightButton
             onEntered: {
                 console.log("==============   ==onEntered")

             }
         }


     }
}

Спасибо за любую идею.

Редактировать:

polygon.cpp

#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|ItemIsFocusable);
    setAcceptDrops(true);
    setAcceptedMouseButtons( Qt::LeftButton );



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

void Polygon::focusOutEvent(QFocusEvent *event)
{
    cout<<"focusout"<<endl;
    this->setSelected( false );
}*/


/*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;
}

QString Polygon::name() const
{
    return m_name;
}

void Polygon::setName(const QString &name)
{
    m_name = name;
}

QColor Polygon::color() const
{
    return m_color;
}

void Polygon::setColor(const QColor &color)
{
    m_color = color;
}

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);



}

QVector<QPointF> Polygon:: listToVector(QList<Point *> lpnt) const{
    QVector<QPointF> vPnt;
        for(int i=0;i<lpnt.length();i++){
            vPnt.append(QPointF(lpnt.at(i)->x(),lpnt.at(i)->y()));

        }
        return vPnt;
}

QDeclarativeListProperty<Point> Polygon::vertices()
 {
     return QDeclarativeListProperty<Point>(this, 0, &Polygon::append_vertex);
 }

 void Polygon::append_vertex(QDeclarativeListProperty<Point> *list, Point *vertex)
 {
     Polygon *polygon = qobject_cast<Polygon *>(list->object);
     if (polygon) {
         vertex->setParentItem(polygon);
         polygon->m_vertices.append(vertex);
     }
 }

1 Ответ

0 голосов
/ 16 июля 2012

Я полагаю, вы должны установить hoverEnabled на true, или это намеренно установлено на visible? visible - это логическое свойство, которое может быть ложным для MouseArea.

MouseArea {
    hoverEnabled: true
}

То же самое относится к enabled: visible

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...