我想知道如何在QGraphicsScene上移动QDeclarativeItem。似乎这个项目抓住焦点或类似的东西。其他GraphicsItem的类型(如QGraphicsTextItem等)不会发生这种情况。有一些焦点政策或一些标志需要改变? Thxs !!!
这是代码:
QGraphicsScene * m_scene = new QGraphicsScene(this);
QGraphicsView * m_view = new QGraphicsView(this);
m_view->setScene(m_scene);
QDeclarativeEngine m_engine;
QDeclarativeComponent component(&m_engine, QUrl::fromLocalFile("hello.qml"));
//With this item, it doesn't work!!!
QDeclarativeItem *item = qobject_cast<QDeclarativeItem *>(component.create());
//With this item, it works!!!
//QGraphicsTextItem * item = m_scene->addText("Hello!!!");
item->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
m_scene->addItem(item);
这是QML文件(hello.qml):
import QtQuick 1.0
Rectangle {
id: page
width: 100; height: 20
color: "lightgray"
Text {
id: helloText
text: "Hello world!"
anchors.horizontalCenter: page.horizontalCenter
font.pointSize: 12; font.bold: true
}
}