首页| 论坛| 消息

回复: 求助关于QGraphicsItem的坐标问题
#6 回 kimtaikee 的帖子 [阿拉克尼 06-27 21:27]
kimtaikee:你添加每个item到scene中都是需要调用setPos设置它的位置的,这时候scenePos返回的是正确的结果。如果你没有调用setPos的话返回的都是(0,0),我上一个回复中提到的左上角是item在scene中的左上角不是整个scene的左上角。你先把创建这些item的代码贴上来。 (2016-06-27 21:19) 
嗯,setPos设置位置是在另一个cpp里面
void DiagramScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
if (mouseEvent->button() != Qt::LeftButton)
return;
DiagramItem *item;
switch (myMode) {
case InsertItem:
item = new DiagramItem(myItemType, myItemMenu);
item->setBrush(myItemColor);
addItem(item);
item->scenePos()=mouseEvent->scenePos();
item->setPos(mouseEvent->scenePos());
emit itemInserted(item);
break;
default:
;
}
QGraphicsScene::mousePressEvent(mouseEvent);
}
我现在开始怀疑我是否用对了那个items()函数,这个函数有好几种重载形式
#7 [firebolt 06-29 19:04]
1、保存到xml文件:
QFile file(fileName);
if (!file.open(QFile::WriteOnly | QFile::Text)) {
QMessageBox::warning(this, tr("Qt Drawing"),
tr("Cannot write file %1:\n%2.")
.arg(fileName)
.arg(file.errorString()));
return false;
}
QXmlStreamWriter xml(&file);
xml.setAutoFormatting(true);
xml.writeStartDocument();
xml.writeDTD("");
xml.writeStartElement("canvas");
xml.writeAttribute("width",QString("%1").arg(scene()->width()));
xml.writeAttribute("height",QString("%1").arg(scene()->height()));
foreach (QGraphicsItem *item , scene()->items()) {
AbstractShape * ab = qgraphicsitem_cast(item);
QGraphicsItemGroup *g = dynamic_cast(item->parentItem());
if ( ab &&!qgraphicsitem_cast(ab) && !g ){
ab->saveToXml( ..
#8 回 firebolt 的帖子 [firebolt 06-29 19:07]
firebolt:1、保存到xml文件:
    QFile file(fileName);
    if (!file.open(QFile::WriteOnly | QFile::Text)) {
        QMessageBox::warning(this, tr("Qt Drawing"),
   & .. (2016-06-29 19:04) 
还忘了一个方法
bool GraphicsItem::readBaseAttributes(QXmlStreamReader *xml)
{
qreal x = xml->attributes().value(tr("x")).toDouble();
qreal y = xml->attributes().value(tr("y")).toDouble();
m_width = xml->attributes().value("width").toDouble();
m_height = xml->attributes().value("height").toDouble();
setZValue(xml->attributes().value("z").toDouble());
setRotation(xml->attributes().value("rotate").toDouble());
setPos(x,y);
return true;
}

<< 1 2 >> (2/2)

回复 发表
主题 版块