Qml 如何通知焦点?

Qml 如何通知焦点?,qml,Qml,此问题已编辑: 我和QML一起工作 我有一个名为polygon的自定义类型,它是QDeclarativeItem的子类 当鼠标单击多边形(具有焦点)时,我希望收到通知 我知道QDeclarativeItem有一个功能:聚焦 我在Polygon.cpp中覆盖它,这里是Polygon.cpp #include "polygon.h" #include "point.h" #include <QPainter> #include <stdio.h> #include <i

此问题已编辑:

我和QML一起工作

我有一个名为polygon的自定义类型,它是QDeclarativeItem的子类

当鼠标单击多边形(具有焦点)时,我希望收到通知

我知道QDeclarativeItem有一个功能:聚焦

我在Polygon.cpp中覆盖它,这里是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);


}
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);
     }
 }
我覆盖了shape,boundingRect,正如您所看到的,它定义了多边形对象,并成功地在单击时收到通知,焦点出现在ti中

但我看不到屏幕上聚焦功能的cout输出

我应该在项目的main.cpp上添加一些内容吗

如果C++代码知道对象有焦点,该怎么办

有联系吗?
谢谢你的建议。

我试过了,似乎问题出在输出“cout”上,试一下“qDebug”,看看你有好的代码。 你所做的一切都是正确的

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}
         ]


     }




 }